如何在 Visual Basic 中停止系统堆栈溢出?

问题描述 投票:0回答:1

我正在开发一个旨在在不同数字系统之间进行转换的应用程序。其操作包括用户在一种基数系统中输入一个值,之后其余三个文本框将自动显示其他数制中的等效值。但是,我在代码中遇到了堆栈溢出错误。具体来说,当我尝试将代码添加到 oct2_textbox 时,会发生错误。我不确定如何解决这个问题。

heres the picture i made in visual basic

Imports System.ComponentModel
Imports System.Windows.Forms.VisualStyles.VisualStyleElement

Public Class Form1
    Private Sub Label3_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs)

    End Sub



    Private Sub oct_KeyPress(sender As Object, e As KeyPressEventArgs)

        If Not (e.KeyChar >= "0"c AndAlso e.KeyChar <= "7"c OrElse Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub HEX_TextChanged(sender As Object, e As EventArgs) Handles HEX.TextChanged

        Dim HEX_num As Integer
        If Integer.TryParse(HEX.Text, System.Globalization.NumberStyles.HexNumber, Nothing, HEX_num) Then

            oct2.Text = Convert.ToString(HEX_num, 8)
            DECDEC.Text = Convert.ToString(HEX_num, 10)

            bin.Text = Convert.ToString(HEX_num, 2)

        End If
        bin.Text = bin.Text.ToString()

    End Sub
    Private Sub HEX_KeyPress(sender As Object, e As KeyPressEventArgs) Handles HEX.KeyPress
        ' Remove the event handler temporarily
        RemoveHandler HEX.KeyPress, AddressOf HEX_KeyPress

        Dim inputChar As Char = Char.ToUpper(e.KeyChar)

        If Not (Char.IsDigit(inputChar) OrElse
            (inputChar >= "A"c AndAlso inputChar <= "F"c) OrElse
            Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If

        ' Reattach the event handler
        AddHandler HEX.KeyPress, AddressOf HEX_KeyPress
    End Sub


    Private Sub DECDEC_TextChanged(sender As Object, e As EventArgs) Handles DECDEC.TextChanged
        Dim decdec_num As Integer
        If Integer.TryParse(DECDEC.Text, decdec_num) Then
            oct2.Text = Convert.ToString(decdec_num, 8)
            HEX.Text = Convert.ToString(decdec_num, 16)
            bin.Text = Convert.ToString(decdec_num, 2)
        End If
    End Sub


    Private Sub DECDEC_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DECDEC.KeyPress

        If Not (Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub bin_TextChanged(sender As Object, e As EventArgs) Handles bin.TextChanged
        ' Remove the event handler temporarily
        RemoveHandler bin.TextChanged, AddressOf bin_TextChanged

        Dim bin_str As String = bin.Text.Trim()
        If Not String.IsNullOrEmpty(bin_str) AndAlso bin_str.All(Function(c) c = "0" OrElse c = "1") Then
            Dim bin_num As Integer = Convert.ToInt32(bin_str, 2)
            DECDEC.Text = bin_num.ToString()
            HEX.Text = Convert.ToString(bin_num, 16)
            oct2.Text = Convert.ToString(bin_num, 8)
        End If

        ' Reattach the event handler
        AddHandler bin.TextChanged, AddressOf bin_TextChanged
    End Sub


    Private Sub bin_KeyPress(sender As Object, e As KeyPressEventArgs) Handles bin.KeyPress
        ' Remove the event handler temporarily
        RemoveHandler bin.KeyPress, AddressOf bin_KeyPress

        If Not (e.KeyChar = "0"c OrElse e.KeyChar = "1"c OrElse Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If

        ' Reattach the event handler
        AddHandler bin.KeyPress, AddressOf bin_KeyPress
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        oct2.Text = ""
        HEX.Text = ""
        DECDEC.Text = ""
        bin.Text = ""


    End Sub

    Private Sub GroupBox4_Enter(sender As Object, e As EventArgs) Handles GroupBox4.Enter

    End Sub

    Private Sub oct2_TextChanged(sender As Object, e As EventArgs) Handles oct2.TextChanged
        ' Remove the event handler temporarily
        RemoveHandler oct2.TextChanged, AddressOf oct2_TextChanged

        Dim oct2_num As Integer
        If Integer.TryParse(oct2.Text, oct2_num) Then
            DECDEC.Text = Convert.ToString(oct2_num, 10)
            HEX.Text = Convert.ToString(oct2_num, 16)
            bin.Text = Convert.ToString(oct2_num, 2)
        End If

        ' Reattach the event handler
        AddHandler oct2.TextChanged, AddressOf oct2_TextChanged
    End Sub


End Class

我想要一个动态且响应迅速的系统,可以在值更改时自动转换。

vb.net visual-studio-2010
1个回答
0
投票

您可以使用守卫来防止递归调用

Private m_updating As Boolean

然后在 TextChanged 事件处理程序中检查、设置和重置防护。这里

HEX_TextChanged
举个例子

Private Sub HEX_TextChanged(sender As Object, e As EventArgs) Handles HEX.TextChanged
    If Not m_updating Then
        m_updating = True
        Try
            ' TODO: put your conversion and updating logic here
        Finally
            m_updating = False
        End Try
    End If
End Sub

Try-Finally 语句确保在任何情况下重置防护,即使发生异常或代码过早地带有 Return-语句。

© www.soinside.com 2019 - 2024. All rights reserved.