Keep the latest combobox selection when re-opening the application - vb.net

I am working on a VB.Net Visual Studio application. If I select say value "B" from a combobox and then close the application, when I re-open it I want that combobox to have the latest choice selected before closing the app to be selected. How do I do that? Thank you.

In Project Properties select Settings.
Set the Name, Type, Scope and Value as shown below.
I use the SelectedIndex so if nothing is selected, it will still work with -1.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = My.Settings.ComboSelection
End Sub
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
My.Settings.ComboSelection = ComboBox1.SelectedIndex
End Sub

Related

Visual Studio 2019 (vb) - issue with Reading/Writing My.Settings

I'm just starting to develop and so there are some concepts that are not clear to me, I need you to try to understand what I'm doing wrong.
My goal is to have a series of ComboBoxes that contain some strings, for example a string is this one, the ComboBox is called TYPE:
I am storing these strings in My.Settings for my convenience and to edit them directly from the app.exe.config file (these information are stored there right?).
I'm using this code
Private Sub AdminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.TYPE
ComboBoxType.Items.Add(item)
Next
End Sub
My issue is that, I'm unable to read from My.Settings.TYPE and when I try to write into it with the following code I doesn't find any strings added into My.Settings menu neither into app.exe.config.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonTYPEAddValue.Click
ComboBoxType.Items.Add(TextBoxType.Text)
ComboBoxType.Text = TextBoxType.Text
TextBoxType.Clear()
MsgBox("Item added!")
End Sub
what is wrong?
In your code, it seems like you've used String to add/remove the ComboBoxType items. Follow the steps to achieve your requirement:
In this screenshot, you can see I've set the Type as System.Collection.Specialized.StringCollection to separate each time for future use.
Now, you may use the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.tests.Add(TextBox1.Text)
My.Settings.Save()
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub AdminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.tests
ComboBox1.Items.Add(item)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Settings.tests.Remove(TextBox1.Text)
My.Settings.Save()
ComboBox1.Items.Remove(TextBox1.Text)
End
It's noticeable that you can change the variable names, they're differ a bit than yours in my code.
Form structure for the code:
On the MyBase.Load event, all the items containing in My.Settings.tests will be added using For Each loop (benefit of StringCollection).
Working example of the form [adding - removing using My.Settings]:
I hope you've got your answer.

Items inside the combobox not showing

I am trying to add some items to a combo box in VB, but when I add the items to the click event handler of the combo-box, and run the code, the items added do not show.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Refresh()
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Mondad")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("Wenesday")
ComboBox1.SelectedIndex = 1
End Sub
I mean nothing shows inside the combo-box.
I add the items to the click event handler of the combo-box
Pay special attention to the name of the method:
ComboBox1_SelectedIndexChanged()
Note the emphasis. It sure looks like this is NOT the click event. The click event method would look like this:
ComboBox1_Click(object sender, EventArgs e)
Just changing the name of the method will not be enough, because the method still will not be wired up correctly. Create a new empty event handler in Visual Studio for the click event and move the code there.
Just add this to your code i think it will work
Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Mondad")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("Wenesday")
ComboBox1.SelectedIndex = 1
End Sub
hope it helps
Put in form load
` Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Mondad")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("Wenesday")
End Sub`

How do you get to the .enter code automatically in visual basic?

Okay I realize the question is ambiguous, but I didn't know what else to name it. As all of you know if I double click a textbox in visual basic it gives me this code automatically.
Private Sub textBox1_Click(sender As Object, e As EventArgs) Handles textBox1.Click
'do stuff here
End Sub
What do you click or otherwise have to do to get this to show up automatically?
Private Sub textBox1_Enter(sender As Object, e As EventArgs) Handles textBox1.Enter
'do stuff here
End Sub
What about this one as well
Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
'do stuff here
End Sub
Select the textbox. In the Properties pane/window (press F4 if can't see it), click the icon which looks a bit ilke a lightning bolt to get a list of the available handlers. Double-click the one you want and it will create the template for you.

Windows Forms: Keeping Check Beside Checked Box list item and displaying separate message for each

When I run my program currently, Within any checked box list I am unable to actually select an option (i.e. the box beside it, to tick).
I would also like each individual option to display a messagebox when ticked (i.e. "User story one added"), But currently my program only displays a general messagebox ("User Story selected") once the checked box list is clicked on. Any help would be much appreciated!
Current Code:
Public Class Form2
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
MessageBox.Show("User Story Selected")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
You are responding to the wrong event and might be likely to examine the wrong property.
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ItemCheckEventArgs)
MessageBox.Show("User Story Selected")
End Sub
Checks fire the ItemCheck event, and checked items are in the CheckedItems Collection. The SelectedItems collection is literally those selected (highlighted) which might not also be checked. It is not really a list of checkboxes, but a list of items drawn as checks - thats why they look different than regular checks. To see which item:
For n as Integer = 0 to CheckedListBox1.CheckedItems.Count-1
userWants = CheckedListBox1.CheckedItems(n)
Next n
Like a ListBox you can put anything in there, not just strings, so it was a list of stories, you might be able to do:
userWants = CheckedListBox1.CheckedItems(n).StoryName

How to populate listbox with a list of all open forms

I have a form with a listbox, and I want to be able to populate it with all open forms of the same application. However, I want to be able to select an Item from the listbox, and be able to close the form associated with that item in the list box. Is this possible to do?
I found the answer to the issue. The following code works:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myForms As FormCollection = Application.OpenForms
For Each frmName As Form In myForms
ListBox1.Items.Add(frmName.Name.ToString)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If Not ListBox1.SelectedIndex = -1 Then
Dim myForm As Form = Application.OpenForms(ListBox1.Text)
myForm.Close()
End If
End Sub
Where the code under ListBox1_SelectedIndexChanged can very easily be placed in a button.
My.Application.OpenForms is a collection of the open forms in your project. So something like:
For Each f As Form In My.Application.OpenForms
Me.SomeListBox.Items.Add(f)
Next
Then to close the selected item, it’s
DirectCast(Me.SomeListBox.SelectedItem, Form).Close()