Friends,
Right now, when you select any of the namesin combobox (ComboBox_Names), the sametext appears on thelabel.
I just wish that this sameeffect of a label,appearedin FORM2.
How do I? What is thecode?
My greetings.
Image may be NSFW.
Clik here to view.
Option Strict On Option Explicit On Public Class Form1 Private namesList As New List(Of String) Private namesFileLocation As String = Application.StartupPath & "\Names.txt" ' ' ' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Button_AddNewName.Enabled = False ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList ComboBox_Names.DropDownStyle = ComboBoxStyle.DropDownList ' Check to see if the stored text file exists: If My.Computer.FileSystem.FileExists(namesFileLocation) Then ' Now read each line in: Using rdr As New System.IO.StreamReader(namesFileLocation) Do While rdr.Peek() >= 0 Dim itm As String = rdr.ReadLine itm = itm.Trim If itm <> "" Then ' Add it to the list of names: namesList.Add(itm) End If Loop End Using End If ' Test to see if the names list has items in it ' and if so, put them into the items of the names ' combobox: For Each personName As String In namesList ' Note there's no "testing" needed; if the ' list is empty, it never gets this far. ComboBox_Names.Items.Add(personName) ComboBox2.Items.Add(personName) Next ' Now check whether or not there's anything ' to show and, if so, enable the appropriate ' controls: CheckOkToEnableButtons() End Sub ' ' ' Private Sub Form1_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing ' This event is triggered as Form1 is closing. Let's ' save the list to the text file. Since it very well ' may be a large list and the text file may or may ' not yet exist, let's use a stringbuilder, build ' the entire text to write, then create the text ' file (if it does not exist) or overwrite the text ' file (if it does exist). First let's make sure ' that the form is closing because the user has ' closed it, then if that's the case, we'll do the ' rest of it: If e.CloseReason = CloseReason.UserClosing Then Dim sb As New System.Text.StringBuilder For Each personName As String In namesList sb.AppendLine(personName) Next ' Now write the contents of the stringbuilder ' IF the stringbuilder actually has something ' in it: If sb.ToString.Length > 0 Then My.Computer.FileSystem.WriteAllText(namesFileLocation, _ sb.ToString, False) End If End If End Sub ' ' ' Private Sub TextBox_NewName_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles TextBox_NewName.TextChanged ' This event is fired anytime that the text in the textbox ' (TextBox_NewName) has changed. We'll test to see if the ' text "trimmed" is blank. If it's not, then we'll enable ' the button to allow them to add it: If TextBox_NewName.Text.Trim <> "" Then Button_AddNewName.Enabled = True Else Button_AddNewName.Enabled = False End If End Sub ' ' ' ' ' ' Private Sub Button_AddNewName_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button_AddNewName.Click ' Test that the name isn't already in the list and, if not, ' then add the new name: If Not namesList.Contains(TextBox_NewName.Text.Trim) Then ' Add it both to the main list and to the combobox's items namesList.Add(TextBox_NewName.Text.Trim) ComboBox_Names.Items.Add(TextBox_NewName.Text.Trim) ComboBox2.Items.Add(TextBox_NewName.Text.Trim) CheckOkToEnableButtons() Else ' Show a message indicating that the name entered ' is already in the combobox's items: MessageBox.Show("The name that you've entered is already" & vbCrLf & _"in the names combobox.", "Cannot Add This Name", _ MessageBoxButtons.OK, MessageBoxIcon.Warning) End If ' There's a basic fallacy that using the .Contains method won't ' detect. For instance, what if they typed in the following: ' ' Frank Smith ' ' Then later typed in this: ' ' frank smith ' ' See the problem? To the program, they're not the same so ' it will allow it. Another example is this: ' ' Frank Smith ' ' Frank Smith ' ' Again, to the computer they aren't the same. Is there a way ' that we can make the program work so that it will not allow ' these types of dupicates? Yes. LINQ would be a good tool to ' use but if you're not familiar with it, then it will appear ' to have a very odd syntax and it's bound to be more confusing ' than helpful, so instead I'll just iterate through the list ' and test it. The following is commented out so it doesn't ' actually work but if you want to try it, then comment out ' the code above, and uncomment out what follows: ' ' 'If CheckForDuplicates(TextBox_NewName.Text.Trim) Then ' MessageBox.Show("The name that you've entered is already" & vbCrLf & _ ' "in the names combobox.", "Cannot Add This Name", _ ' MessageBoxButtons.OK, MessageBoxIcon.Warning) 'Else ' namesList.Add(TextBox_NewName.Text.Trim) ' ComboBox_Names.Items.Add(TextBox_NewName.Text.Trim) ' CheckOkToEnableButtons() 'End If With TextBox_NewName .Clear() .Focus() End With End Sub ' ' ' Private Sub Button_ShowForm2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button_ShowForm2.Click ' We'll pass the list of names to Form2 after we first ' create a new instance of Form2: Using f2 As New Form2 f2.listOfNames = namesList ' Now show it modally: f2.ShowDialog() End Using End Sub ' ' ' Private Sub Button_ShowForm3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button_ShowForm3.Click ' We'll pass the list of names to Form3 after we first ' create a new instance of Form3: Using f3 As New Form3 f3.listOfNames = namesList ' Now show it modally: f3.ShowDialog() End Using End Sub ' ' ' Private Sub Button_ShowForm4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button_ShowForm4.Click ' We'll pass the list of names to Form4 after we first ' create a new instance of Form4: Using f4 As New Form4 f4.listOfNames = namesList ' Now show it modally: f4.ShowDialog() End Using End Sub ' ' ' Private Sub CheckOkToEnableButtons() ' This sub just checks to see if there's anything ' in the namesList (and subsequently in the ' combobox) and if so, it enables the appropriate ' controls but if not, it disables them: If namesList.Count > 0 Then Button_ShowForm2.Enabled = True Button_ShowForm3.Enabled = True Button_ShowForm4.Enabled = True ComboBox_Names.Enabled = True Else Button_ShowForm2.Enabled = False Button_ShowForm3.Enabled = False Button_ShowForm4.Enabled = False ComboBox_Names.Enabled = False End If End Sub Private Function CheckForDuplicates(ByVal name As String) As Boolean Dim retVal As Boolean = False For i As Integer = 0 To namesList.Count - 1 ' Let's change the value that we actually test ' (both in the parameter passed in and in the ' list of names) to lower case and, if either ' contains a space character, we'll take those ' out also: Dim nameToTest As String = name.ToLower If nameToTest.Contains(" "c) Then nameToTest = nameToTest.Replace(" "c, "") End If ' Now we'll do the same with the name in ' the list that the counter is presently ' referring to: Dim nameInTheList As String = namesList(i).ToLower If nameInTheList.Contains(" "c) Then nameInTheList = nameInTheList.Replace(" "c, "") End If ' Now we can test it: If nameInTheList = nameToTest Then ' Yep! It's a match! retVal = True Exit For End If Next Return retVal End Function Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Let's create a new list, this time the list will hold ' integers: Dim numbersList As New List(Of Integer) ' I'll populate the list just by going through a small ' loop: For i As Integer = 1 To 10 numbersList.Add(i) Next ' Now the list has a count of 10. Let's remove the one ' that has the number 7 in it. For i As Integer = 0 To numbersList.Count - 1 ' First, note that I start at 0 and go through ' the list's quantity (count) minus one. That's ' because it's a zero-based list as are most ' things in .NET so the first item has an ' index of 0, the second has an index of 1, and ' so on. ' ' Let's find the one with 7: If numbersList(i) = 7 Then ' Ah, found it! ' ' Now how do I eliminate it without getting an ' exception? Let's try it and see: numbersList.RemoveAt(i) ' Run into a problem? ;-) Yes ... it just threw ' an "index out of range" exception. So how to ' do it then? Try this now: Exit For ' To make my example work you'll need to comment ' out the "Exit For" above. If you do then you'll ' get the exception, but let me explain why that ' worked: ' ' The only time that it actually throws an exception ' isn't when you remove it; it's when it does that ' then tries to continue on in the loop, so as soon ' as it gets to "Next", jumps back to the top (the ' "For"), bang - exception. Why? Because you just ' changed the count when you removed it, so the way ' to do it is to find it like I showed above, remove ' it, then jump OUT of the loop. ' ' If you have more than one to do then the entire ' thing will have to be inside a do loop or some other ' type loop but this example is a simple one with just ' one being removed at a time. End If Next End Sub Private Sub ComboBox_Names_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ComboBox_Names.SelectedIndexChanged ' The selected index has changed. Now verify that it's ' not at index -1 (which is "nothing selected") and if ' so, enable the button to remove the name. If ComboBox_Names.SelectedIndex > -1 Then Button_RemoveSelectedName.Enabled = True Else Button_RemoveSelectedName.Enabled = False End If End Sub Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ComboBox2.SelectedIndexChanged ' The selected index has changed. Now verify that it's ' not at index -1 (which is "nothing selected") and if ' so, enable the button to remove the name. If ComboBox2.Text = String.Empty Then ComboBox2.SelectedIndex = -1 ComboBox2.Select() End If If ComboBox2.Text = ComboBox_Names.Text Then MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK) ComboBox2.SelectedIndex = -1 ComboBox2.Select() End If End Sub ' ' ' Private Sub Button_RemoveSelectedName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button_RemoveSelectedName.Click ' The button to remove the selected name from ' the list of names. With Button_RemoveSelectedName .Enabled = False .Refresh() End With ' Find the name in the list of names: Dim selectedName As String = ComboBox_Names.Items(ComboBox_Names.SelectedIndex).ToString For i As Integer = 0 To namesList.Count - 1 If namesList(i) = selectedName Then ' Found the name, so remove it and and jump out ' of the loop by using the "Exit For": namesList.RemoveAt(i) Exit For End If Next ' Now clear the control's list: ComboBox_Names.Items.Clear() ComboBox2.Items.Clear() ' And lastly, refill the control's ' items: For Each personName As String In namesList ComboBox_Names.Items.Add(personName) ComboBox2.Items.Add(personName) Next End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_Names.SelectedIndexChanged Label3.Text = ComboBox_Names.SelectedItem.ToString End Sub End Class