How to populate listbox with a list of all open forms - vb.net

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()

Related

How to access DataGridView properties on an already designed Windows Form

I had already designed a windows form (Form1) with a DataGridView on it, I also have a ribbon with a button on it in which I want when I click on it, it run a code like bellow:
Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles
Button2.Click
Dim f As Form1
f = New Form1
f.Show()
'(Room for my question)
end sub
I want to be able to access my datagridview user control to add columns and rows programmatically.
How should I do that?
You can create a Public Sub on form one which where you will put the changes that you want make on the the Datagridview then call it on the Button2_Click event of your Form2.
Example:
Form 1:
Public Sub AddToDGV()
With DataGridView1
.Rows.Add(Form2.TextBox1.Text, Form2.TextBox2.Text, Form2.TextBox3.Text)
End With
End Sub
Form2:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Form1.AddToDGV()
End Sub

How do I add items to a listbox inside a user control?

I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.

vb.net combobox hide item

I have a combobox that when the item is selected and it's the same as a button.text in other form the button.text changes for the name that the user typed in a textbox. But if the item is different of the button.text I want to hide it, so the user can't select it or see it.
cafetariacombo is the combobox Form3.cafetaria2.Text is the button I'm changing
If cafetariacombo.SelectedItem = "cafetaria2" Then
Form3.cafetaria2.Text = TextBox1.Text
My.Settings.cafetaria2guardar = Form3.cafetaria2.Text
My.Settings.Save()
end if
I use this to name the button, I just need to know if I can hide the combobox item.
Help me please :)
Update with some code
I inserted the list of items myself in the combobox.
I solved my earlier problem, but now i need to save the state of the combobox items when I leave the form.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If cafetariacombo.SelectedItem <> Form3.cafetaria1.Text Then
cafetariacombo.Items.Remove("cafetaria1")
End If
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox2.SelectedItem = Form3.cafetaria1.Text Then
Form3.cafetaria1.Text = "cafetaria1"
My.Settings.cafetaria1guardar = Form3.cafetaria1.Text
My.Settings.Save()
adicionarproduto.cafetariacombo.Items.Add("cafetaria1")
end if
end sub
When I remove the item from the combobox I'm in form1 and when I add the item again I'm in form2. Just need to save the combobox with the deleted item when I leave the form1.
You can use a DataTable as data source, then it's as easy as changing DefaultView.RowFilter, please study this example and let me know if you have any questions:
Public Class Form1
Private Sub Button1_Click(sender As System.Object,
e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Rows.Add("1")
dt.Rows.Add("2")
dt.Rows.Add("3")
ComboBox1.DisplayMember = "id"
ComboBox1.DataSource = dt 'show all items by default
End Sub
Private Sub Button2_Click(sender As System.Object,
e As System.EventArgs) Handles Button2.Click
DirectCast(ComboBox1.DataSource, DataTable).DefaultView.
RowFilter = "id <> 2" 'hide item=2 from the view
End Sub
End Class

How to open up a form from another form in VB.NET?

This I thought would be easy. I have not used VB.NET all that much, and I am trying to open up a form from a button click. The form will not show and I get a null exception error.
What is wrong with the code?
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim A
A = AboutBox1
A.Show()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) _
Handles Button3.Click
Dim box = New AboutBox1()
box.Show()
End Sub
You can also use showdialog
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) _
Handles Button3.Click
dim mydialogbox as new aboutbox1
aboutbox1.showdialog()
End Sub
You could use:
Dim MyForm As New Form1
MyForm.Show()
or rather:
MyForm.ShowDialog()
to open the form as a dialog box to ensure that user interacts with the new form or closes it.
You may like to first create a dialogue by right clicking the project in solution explorer and in the code file type
dialogue1.show()
that's all !!!

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