0

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.

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

I want a dynamic and responsive system that automatically convert when a value is changed.

5
  • 1
    Please don't submit your question if your formatting is a mess. You are provided with a preview for a reason. If you don't know how to format properly, spend some time in the Help Center to learn. Commented Mar 31, 2024 at 4:48
  • 2
    Use the debugger to step through the code to find out where your logic error is located that is causing the endless loop and stack overflow. If you don't know how to use a debugger, now is the perfect time to learn. There's no better tool available to a coder to be able to trace through code execution, examine variable content, and locate issues. Tip for what to look for: If you have an OnChange event connected to a control, don't update the contents of that control from within that event. Using the debugger should show you why that's not possible very quickly. Commented Mar 31, 2024 at 4:50
  • 4
    Actually, your code is an absolute logic mess. You make a change to DEC, which causes a change to HEX and OCT, which causes HEX to update DEC and OCT, which causes OCT to update HEX and DEC, which causes DEC to update HEX and OCT, which causes HEX to update DEC and OCT, which causes... See the problem yet? Commented Mar 31, 2024 at 4:55
  • Please add code that check to see if the textbox needs updating before updating it. Something like SetTextIfChanged(HEX, Convert.ToString(bin_num, 16)). That will prevent recursive event calls. I suspect your problem will go away. Commented Mar 31, 2024 at 4:55
  • Is this a homework assignment because the built-in Windows calculator already does this if one uses the menu and changes to Programmer? Commented Mar 31, 2024 at 16:03

1 Answer 1

0

You can use a guard to prevent recursive calls

Private m_updating As Boolean

Then in the TextChanged event handlers check, set and reset the guard. Here HEX_TextChanged as an example

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.
            ' (Don't remove or add event handlers)
        Finally
            m_updating = False
        End Try
    End If
End Sub

The 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_TextChanged and oct2_TextChanged.

The KeyPress methods 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:

Private Sub bin_KeyPress(sender As Object, e As KeyPressEventArgs) Handles bin.KeyPress
    e.Handled = Not (e.KeyChar = "0"c OrElse e.KeyChar = "1"c OrElse
        Char.IsControl(e.KeyChar))
End Sub

I didn't check whether your actual filtering or conversion logic is correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.