Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone. How to in C# WinForms when click a item in ListBox created by user to open a automatically created form?

When user click a item in ListBox, with is created by user (that item), to open a form with is a automatically created (i'am missing to in that form is to have a secound ListBox). In all different item in ListBox, to have a different Form with secound ListBox.
Can help please?

What I have tried:

Tryes some things likely:
void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
    var list = (ListBox)sender;

    int itemIndex = list.IndexFromPoint(e.Location);
    if (itemIndex != -1)
    {
        object item = list.Items[itemIndex];
    }
}
Posted
Updated 6-Jan-22 5:27am
Comments
[no name] 6-Jan-22 10:39am    
Sorry for my spell incorrect.

1 solution

Try this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    int ndx = (sender as ListBox).SelectedIndex;

    // check for no selection
    if (ndx == -1) return;

    switch (ndx)
    {
        case 0:
            // create whatever
            // show whatever
            break;
    }
}
 
Share this answer
 
v2
Comments
[no name] 7-Jan-22 7:43am    
Yes, but wheno go onto next item in listbox, is doesn't works my script
[no name] 7-Jan-22 7:50am    
Otherwise, this is my code:
int ndx = (sender as ListBox).SelectedIndex;
if (ndx == -1) return;

switch (ndx)
{
case 0:
ListBox devicesList = new ListBox();
this.newListBox = devicesList;
devicesList.Location = new Point(200, 78);
devicesList.BorderStyle = BorderStyle.Fixed3D;
//devicesList.Dock = DockStyle.Fill;
devicesList.Size = new Size(600, 372);
this.Controls.Add(devicesList);
break;
}
BillWoodruff 7-Jan-22 8:37am    
1) ListBox Items are Type Object: when you assign them at design-time they will be Strings.

2) assigning other Types will work if their Type implements 'ToString.

3) your code:

a) every time you create a new "something" inside a method (locally), the reference to that something is lost when the method exits (goes out of scope). That is almost always a bad thing. Define variables of Type something outside the method, andassign new whatevers tp them inside the method: that way you don't lose the reference.

b) I can't tell what you are trying to do with this code: revise your original post, and clearly explain it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900