Combobox plus String name - vb.net

Hello i have comboboxes with the name ("Combobox1", "Combobox1a", "Combobox2", "Combobox2a", "combobox3" "Combobox3a"). When the selectedindex in combobox1 changed, combobox1a will add 4 items. The same with combobox2 and 2a, combobox3 and 3a. When i do this i have to code it in combobox1, combobox2 and combobox3. So ill try to make a method so that when the selectedindexchange in combobox it will call this method.
heres my code.
Dim cmbName as String = ""
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
cmbName = "1"
cmbSelectedIndexChanged()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
cmbName = "2"
cmbSelectedIndexChanged()
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
cmbName = "3"
cmbSelectedIndexChanged()
End Sub
Private Sub cmbSelectedIndexChanged()
"combobox" & cmbName.Items.add("data1")
"combobox" & cmbName.Items.add("data2")
"combobox" & cmbName.Items.add("data3")
"combobox" & cmbName.Items.add("data4")
End Sub
I just want to make my code smaller so that everytime my combobox index change it will only call the method.

You can find the other combo in your controls collection like this
combo = CType(Me.Controls.Find("combobox1", True)(0), ComboBox)

You mean something like this?
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
cmbSelectedIndexChanged("1")
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
cmbSelectedIndexChanged("2")
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
cmbSelectedIndexChanged("3")
End Sub
Private Sub cmbSelectedIndexChanged(cmbName as string)
Dim Combo as ComboBox = CType(Me.Controls.Find("combobox" & cmbName), ComboBox)
Combo.Items.add("data1")
Combo.Items.add("data2")
Combo.Items.add("data3")
Combo.Items.add("data4")
End Sub

Related

If ComboBox equals an item on the list then do this?

I'm doing a program on Visual Basic Community 2017, that has a serious of ComboBoxes with various dropdown items. If there is a certain combination of the Comboboxes, then open this form. How would I implement this?
E.g
ComboBox1 items (string) = 1, 2, 3 ,4 ,5
ComboBox2 items (string) = a, b, c, d, e
ComboBox 3 items (string)= A, B, C, D, E
The user picks 1,a,A
Clicks a button
Then Show form 1
Thanks, I hope it makes enough sense.
The code I tried was
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedText.ToString() = "1" And ComboBox2.SelectedText.ToString() = "a" And ComboBox3.SelectedText.ToString() = "A" Then
Then
Form1.Show()
Else
MsgBox("Doesn't Work")
End If
End Sub
End Class
Following is example code based on your case. Hope this can help you.
Public Class Form2
Dim selectedItem1, selectedItem2, selectedItem3 As Object
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
showForm1()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
showForm1()
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
showForm1()
End Sub
Private Sub showForm1()
selectedItem1 = ComboBox1.SelectedItem
selectedItem2 = ComboBox2.SelectedItem
selectedItem3 = ComboBox3.SelectedItem
If selectedItem1 Is Nothing OrElse selectedItem2 Is Nothing OrElse selectedItem3 Is Nothing Then
Exit Sub
End If
If ((selectedItem1.ToString() = "1") AndAlso (selectedItem2.ToString() = "a") AndAlso (selectedItem3.ToString() = "A")) Then
Form1.Show()
End If
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' your code to initialize items of ComboBox1, ComboBox2, ComboBox3
End Sub
End Class

Input Strings into String Arrays(Dynamic)

So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub

AllTextBox event in TextChanged

Hello This is a code of mine
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = Val(TextBox1.Text)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox2.Text = Val(TextBox2.Text)
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
TextBox3.Text = Val(TextBox3.Text)
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
TextBox4.Text = Val(TextBox4.Text)
End Sub
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
TextBox5.Text = Val(TextBox5.Text)
End Sub
Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
TextBox6.Text = Val(TextBox6.Text)
End Sub
How do i make a short code for all textboxes ?
I try with this code i didn't work
Dim i As integr
TextBox(i).text = Val(TextBox(i).text)
Thanks for any help
Use one handler for all:
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged, _
TextBox4.TextChanged, _
TextBox5.TextChanged, _
TextBox6.TextChanged
Dim txt = DirectCast(sender, TextBox)
txt.Text = Val(txt.Text)
End Sub
You only need to create one TextChanged handler and then bind all your Texbox events to it.
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, <...for all your textboxes ..>
Dim target = DirectCast(sender, TextBox)
target.Text = Val(target.Text)
End Sub
If you want to bind all textboxes in code, you can do this programatically once your form has loaded:
Dim allTextBoxes = Me.Controls.OfType(Of TextBox)()
For Each textbox In allTextBoxes =
AddHandler textbox.TextChanged, AddressOf TextBox_TextChanged
Next
In addition to Murray Foxcroft's answer. You could dynamically create the Handler for the textboxes and add the subroutine for their code in one by doing
Dim textboxes = Me.Controls.OfType(Of TextBox)()
For Each txt in textboxes
AddHandler textbox.TextChanged, Sub(sender As Object, e As EventArgs)
... (using sender to find out which textbox sent the command)
End Sub
Next

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

How can I call keyDown event by passing arguments, Winforms Vb.net

The Following is a combo box keydown event
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
I would like to trigger same event from combobox_leave by passing 'enter key' I did as follows but not working, how to achieve this?
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub
Why not just extract the method from the actual event?
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
performAction(e.KeyCode);
End Sub
Private Sub performAction(e as Keys)
If e = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
performAction(Keys.Enter);
End Sub
You could also use the SendKeys.Send Method
When the user leaves the Combobox (like in your example),
You could set back the Focus to the combobox
and then use SendKeys.Send("{ENTER}") to trigger the enter keydown.
much like this:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Focus()
SendKeys.Send("{ENTER}")
End Sub
However this prevents users from focusing to another component. To prevent this, you could use an if statement that if the user clicks or focuses on another component after focusing on the combobox, the user can still "leave" the combobox.
Your kind of approach is not advisable and this leads to a misunderstanding in the part of the user.
try this :
Private Sub ComboBox1_KeyDown(sender As Object, e As
keyEventArgs) Handles ComboBox1.KeyDown
Dim _KeyCode As Short
If e Is Nothing Then
_KeyCode = 13
Else
_KeyCode = Keys.Enter
End If
If _KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs)
Handles ComboBox1.Leave
Dim keypress As System.Windows.Forms.KeyPressEventArgs
ComboBox1_KeyDown(sender, keypress)
End Sub