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.
SetTextIfChanged(HEX, Convert.ToString(bin_num, 16)). That will prevent recursive event calls. I suspect your problem will go away.