Input Strings into String Arrays(Dynamic) - vb.net

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

Related

How to remove the last item in the list(Of) in vb.net

I have a windows form application which added string into list of collection. This can be done by input the string into the textbox then click 'add' button, and the list will display in a listbox.
Now, I want to delete the last item in the list collection & the listbox.
Below are the code snippett that I have done
Public strList As List(Of String) = New List(Of String)
'add string to list
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TxtBox.Text <> "" Then
strList.Add(TxtBox.Text)
TxtBox.Clear()
End If
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
'Add item into list
Public Sub ListItem(s As String)
lstItem.Items.Add(s)
'lstItem.Sorted = True
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strList.ToList.ForEach(AddressOf DeleteItem)
End Sub
'Delete item
Public Sub DeleteItem(s As String)
For i = 0 To strList.Count
lstItem.Items.RemoveAt(strList.Count - 1)
i = i + 1
Next
End Sub
as you can see, in the sub DeleteItem, i try to delete the last item of the list collection by clicking 'delete button'. but the error says Additional information: InvalidArgument=Value of '1' is not valid for 'index'.
can anyone help me on this? thank you.
What you really ought to do is use a BindingList(Of String) and bind it to the ListBox. That way, you only have to deal with one list. Adding and removing against the underlying BindingList will automatically affect the ListBox:
Private items As New BindingList(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = items
End Sub
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
items.Add(TextBox1.Text)
End Sub
Private Sub deleteSelectedButton_Click(sender As Object, e As EventArgs) Handles deleteSelectedButton.Click
items.RemoveAt(ListBox1.SelectedIndex)
End Sub
Private Sub deleteLastButton_Click(sender As Object, e As EventArgs) Handles deleteLastButton.Click
items.RemoveAt(items.Count - 1)
End Sub

vb.net button visibility based on checkbox

So i have a form as below:
Public Class IPADSOFT
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
IPADSOFTTS.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
Me.Hide()
End Sub
End Class
which has 3 checkboxes labeled IPADSOFTBOX1, IPADSOFTBOX2, IPADSOFTBOX3
So... i have another form as follows:
Public Class IPADSOFTTS
Private Sub onload()
If IPADSOFT.IPADSOFTBOX1.Checked Then
Button1.Visible = True
Button3.Visible = True
Button5.Visible = True
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
IPADSOFT.Hide()
Me.Hide()
End Sub
End Class
Now the idea is that all the buttons on that second form are set to visible-false and i want the page to check which checkboxes are checked on the last form and then make the required buttons on this form visible... but it isnt working
What am i doing wrong?? i apologise im very very new to vb.net
Open the second form with this
Dim newForm As New IPADSOFTTS With
{.MainForm = Me}
newForm .Show()
Set Public MainForm As IPADSOFT below the Public Class of second form
Then use in the Load event
if MainForm.IPADSOFTBOX1.Checked = true then
'Do whatever
End if

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

Combobox plus String name

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

How to store last button clicked and add to listbox

So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub