Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent a second textchanged event from occurring in Visual Basic
    primarykey
    data
    text
    <p>I am writing a Visual Basic program in Visual Studio 2012, and I have ran into a problem that I just cannot fix on my own. I am using two text boxes with two combo boxes. There are no buttons in the application. I am using the textchanged event to trigger the calculation as you type in numbers. It will work using one text box, but when it displays the answer in the other text box, it is triggering the text boxes textchanged event. Therefore, not giving me the correct answer. By the way, this is a unit converter that is converting length such as meter to millimeter, meter to foot, meter to inch, etc...</p> <pre><code>Public Class frmMain ' class-scope variables Dim decUnit1 As Decimal Dim decUnit2 As Decimal Dim intFlag As Integer Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load PopCombo() cboUnitType.SelectedIndex = 0 End Sub Private Sub PopCombo() ' populates the comboBoxes and sets default selection ' populate the comboBox accordingly If cboUnitType.SelectedIndex = 0 Then ClearBox() With Me.cbo1.Items .Add("Meter") .Add("Milimeter") .Add("Foot") .Add("Inch") End With With Me.cbo2.Items .Add("Meter") .Add("Milimeter") .Add("Foot") .Add("Inch") End With ' set default ComboBox index selection cbo1.SelectedIndex = 1 cbo2.SelectedIndex = 3 ElseIf cboUnitType.SelectedIndex = 1 Then ClearBox() With Me.cbo1.Items .Add("Celsius") .Add("Fahrenheit") End With With Me.cbo2.Items .Add("Celsius") .Add("Fahrenheit") End With cbo1.SelectedIndex = 0 cbo2.SelectedIndex = 1 End If End Sub Private Sub ClearBox() ' clears the comboBox cbo1.Items.Clear() cbo2.Items.Clear() End Sub Private Sub cboUnitType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboUnitType.SelectedIndexChanged PopCombo() End Sub Private Sub txtUnit1_TextChanged(sender As Object, e As EventArgs) Handles txtUnit1.TextChanged Decimal.TryParse(txtUnit1.Text, decUnit1) Decimal.TryParse(txtUnit2.Text, decUnit2) If cboUnitType.SelectedIndex = 0 Then ' converts meter to... If cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 0 Then ' meter txtUnit2.Text = txtUnit1.Text ElseIf cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 1 Then ' millimeter txtUnit2.Text = (decUnit1 * 1000).ToString ElseIf cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 2 Then ' foot txtUnit2.Text = (decUnit1 * 3.28084).ToString ElseIf cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 3 Then ' inch txtUnit2.Text = (decUnit1 * 39.3701).ToString End If ' converts millimeter to... If cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 0 Then ' meter txtUnit2.Text = (decUnit1 * 0.001).ToString ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 1 Then ' millimeter txtUnit2.Text = txtUnit1.Text ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 2 Then ' foot txtUnit2.Text = (decUnit1 * 0.00328084).ToString ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 3 Then ' inch txtUnit2.Text = (decUnit1 * 0.0393701).ToString End If ' converts foot to... If cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 0 Then ' meter txtUnit2.Text = (decUnit1 * 0.3048).ToString ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 1 Then ' millimeter txtUnit2.Text = (decUnit1 * 304.8).ToString ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 2 Then ' foot txtUnit2.Text = txtUnit1.Text ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 3 Then ' inch txtUnit2.Text = (decUnit1 * 12).ToString End If ' converts inch to... If cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 0 Then ' meter txtUnit2.Text = (decUnit1 * 0.0254).ToString ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 1 Then ' millimeter txtUnit2.Text = (decUnit1 * 25.4).ToString ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 2 Then ' foot txtUnit2.Text = (decUnit1 * 0.0833333).ToString ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 3 Then ' inch txtUnit2.Text = txtUnit1.Text End If End If End Sub Private Sub txtUnit2_TextChanged(sender As Object, e As EventArgs) Handles txtUnit2.TextChanged Decimal.TryParse(txtUnit1.Text, decUnit1) Decimal.TryParse(txtUnit2.Text, decUnit2) If cboUnitType.SelectedIndex = 0 Then ' converts meter to... If cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 0 Then ' meter txtUnit1.Text = txtUnit2.Text ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 0 Then ' millimeter txtUnit1.Text = (decUnit2 * 1000).ToString ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 0 Then ' foot txtUnit1.Text = (decUnit2 * 3.28084).ToString ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 0 Then ' inch txtUnit1.Text = (decUnit2 * 39.3701).ToString End If ' converts millimeter to... If cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 1 Then ' meter txtUnit1.Text = (decUnit2 * 0.001).ToString ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 1 Then ' millimeter txtUnit1.Text = txtUnit2.Text ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 1 Then ' foot txtUnit1.Text = (decUnit2 * 0.00328084).ToString ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 1 Then ' inch txtUnit1.Text = (decUnit2 * 0.0393701).ToString End If ' converts foot to... If cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 2 Then ' meter txtUnit1.Text = (decUnit2 * 0.3048).ToString ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 2 Then ' millimeter txtUnit1.Text = (decUnit2 * 304.8).ToString ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 2 Then ' foot txtUnit1.Text = txtUnit2.Text ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 2 Then ' inch txtUnit1.Text = (decUnit2 * 12).ToString End If ' converts inch to... If cbo1.SelectedIndex = 0 AndAlso cbo2.SelectedIndex = 3 Then ' meter txtUnit1.Text = (decUnit2 * 0.0254).ToString ElseIf cbo1.SelectedIndex = 1 AndAlso cbo2.SelectedIndex = 3 Then ' millimeter txtUnit1.Text = (decUnit2 * 25.4).ToString ElseIf cbo1.SelectedIndex = 2 AndAlso cbo2.SelectedIndex = 3 Then ' foot txtUnit1.Text = (decUnit2 * 0.0833333).ToString ElseIf cbo1.SelectedIndex = 3 AndAlso cbo2.SelectedIndex = 3 Then ' inch txtUnit1.Text = txtUnit1.Text End If End If End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload