Home > Article > Computer Tutorials > How to use vb program to find different numbers in two text boxes and display them in another text box
1. How to use VB program to find different numbers in two text boxes and display them in another text box?
To find out the different numbers in two text boxes and display them in another text box in the VB program, you can follow the steps below: 1. Create three text boxes and name them txtBox1, txtBox2 and txtResult respectively. 2. Add a button to the program, name it btnCompare, and associate it with a click event. 3. Write code in the click event handler to compare the numbers in the two text boxes using an If statement. 4. Use the Val function to convert the text in the text box
Design interface:
Write code:
Private Sub btnFindDifference_Click() ' 获取两个文本框中的数字 Dim text1 As String Dim text2 As String text1 = TextBox1.Text text2 = TextBox2.Text ' 转换为数组 Dim array1() As String = text1.Split(" ") Dim array2() As String = text2.Split(" ") ' 使用集合找到不同的数字 Dim uniqueNumbers As New List(Of String) For Each num In array1 If Not array2.Contains(num) AndAlso Not uniqueNumbers.Contains(num) Then uniqueNumbers.Add(num) End If Next ' 将不同的数字显示在第三个文本框 TextBox3.Text = String.Join(" ", uniqueNumbers) End Sub
Test run:
2. I would like to ask you about the text box and corresponding number in VB?
If you encounter the problem of how to use a text box and dynamically generate a corresponding number of text boxes in VB, you can follow the following steps to solve it:
Design interface:
Write code:
Private Sub btnGenerateTextBoxes_Click() ' 获取要生成的文本框个数(这里假设为10个) Dim numberOfTextBoxes As Integer = 10 ' 动态生成文本框 For i As Integer = 1 To numberOfTextBoxes Dim newTextBox As New TextBox newTextBox.Name = "TextBox" & i newTextBox.Text = "TextBox " & i ' 设置位置、大小等属性 newTextBox.Top = (i - 1) * 30 newTextBox.Left = 10 ' 将文本框添加到窗体上 Me.Controls.Add(newTextBox) Next End Sub
Test run:
Summary
In VB, you can find out the different numbers in the two text boxes by writing logical code. In addition, if you need to use text boxes in VB and dynamically generate a corresponding number of text boxes, you can write code in the button click event to achieve dynamic generation of text boxes. In this way, we can solve the puzzle easily.
The above is the detailed content of How to use vb program to find different numbers in two text boxes and display them in another text box. For more information, please follow other related articles on the PHP Chinese website!