I'm working on an application designed to convert between different number systems. Its operation involves users inputting a value in one base number system, after which the remaining three text boxes will automatically display the equivalent values in the other number systems. However, I've encountered a stack overflow error in my code. Specifically, when I attempt to add code to the oct2_textbox, the error occurs. I'm unsure how to resolve this issue.

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
I want a dynamic and responsive system that automatically convert when a value is changed.
You can use a guard to prevent recursive calls
Then in the TextChanged event handlers check, set and reset the guard. Here
HEX_TextChangedas an exampleThe Try-Finally statement makes sure the guard is reset in any case, even if an exception should occur or the code was left prematurely with a Return-statement.
Implement this in
HEX_TextChanged,DECDEC_TextChanged,bin_TextChangedandoct2_TextChanged.The
KeyPressmethods do not require a guard or removing/adding the event handlers, because they are only filtering keys and do not update other TextBoxes (this is what raises the TextChanged events and caused the recursive calls).You can also slightly simplify them by directly assigning the Boolean value:
I didn't check whether your actual filtering or conversion logic is correct.