I'm a student and I'm still new in software programming. I have a listbox in my form and in my listbox box there are item or name listed example "Eric, Molly, Mel" and when I click "Eric", it should redirect me to a new form containing more information about "Eric". How can I call new form using the item listed in the listbox?
Asked
Active
Viewed 2,538 times
0
-
Don;t forget to mark the best suited reply as Answer! – Manish Basantani Dec 27 '11 at 16:07
4 Answers
5
Assuming you are using WinForms to develop your project and you have a form called Form1
with a ListBox called listBox1
on it, you can do like that:
public Form1()
{
InitializeComponent();
listBox1.Click += OnListBoxItemClick;
}
private void OnListBoxItemClick(object sender, EventArgs e)
{
var form2 = new Form2(listBox1.SelectedItem);
form2.ShowDialog();
}
Your class Form2
has to have a constructor which accepts the selected item as parameter.

Fischermaen
- 12,238
- 2
- 39
- 56
-
Since Eric is new to programming and so am I, I think its best to add [this link](http://msdn.microsoft.com/en-us/library/k6sa6h87.aspx) here just in case he's not familiar with overriding the constructor. – Fernando Silva Dec 27 '11 at 17:54
-
About the event created in this line of code `listBox1.Click += OnListBoxItemClick; ` I prefer using Visual Studio to create that stuff in the design partial class, and i cant find that _OnListBoxItemClick event_. So suggest selecting the `listBox1` control, go to the _property pane_, select the _event tab_ and scroll down to the _Click event_ and double click it. Eric, you can also check all the other events for the listBox that might suit you better. – Fernando Silva Dec 27 '11 at 18:02
-
1@FernandoSilva: There is no need to override the constructor. This is the default constructor, a **must** for the designer, because in method `InitializeComponent` the controls are loaded and set with the properties of the property sheet of the designer. – Fischermaen Dec 27 '11 at 19:18
-
Nice to know, i had a similar issue, but i needed to create a handle to the new form, and i created an overriden constructor that allowed to add the mainForm as a paramater, but the original/paramaterless constructor had to remain. I'll test it later to see if there are any errors to my code if i dont override. Either way its good to know i can just adapt my constructor instead of overriding it. – Fernando Silva Dec 27 '11 at 19:58
-
@FernandoSilva: If you write a special constructor for your form, you either have to call the default constructor or method InitializeComponent to have the form running correctly. – Fischermaen Dec 27 '11 at 20:34
-
I just thought you needed to have this constructor `public Form2() {InitializeComponent();}` then add `public Form2(type objectName) {InitializeComponent(); //Do something with objectName}`. I didnt know you could delete the first constructor and just keep the second. Im quite new to programming, so maybe this is obvious, but i didnt know, it worked how i was told so i thought it was the right way. So thks for clearing that up for me ;D – Fernando Silva Dec 27 '11 at 22:49
-
@Eric: So it would be nice, if you accept one of the answers ... see [faq] – Fischermaen Dec 29 '11 at 15:47
-
@Fischermaen If you want to try a diferent way to get that to work, i made a question about it on "programmers", take a look. http://programmers.stackexchange.com/questions/127251/implications-of-handles-between-forms – Fernando Silva Dec 29 '11 at 16:57
1
- Create `Windows Forms Application.
- Put
ListBox
control on the form. - Bind some data source to your
ListBox
control. - Create new
Form
calledPersonDetailsForm
whitch can show details of the person data record. - Subscribe to the
SelectedIndexChanged
event. Put such code to the event handler:
PersonDetailsForm detailsForm = new PersonDetailsForm(); detailsForm.PersonDataItem = listBox1.SelectedItem; // here is your info about person detailsForm.ShowDialog();

Samich
- 29,157
- 6
- 68
- 77
-
by the way can u ask more how can i put the name of the item that i click in the top of the form or it would become the header of the form, example i click "Eric", Eric now will become the name or the header of the newly called form. – Eric Dec 27 '11 at 15:41
-
@Eric `detailsForm.Text = listBox1.SelectedItem;` would change the detailsForm header to the selected item. – Fernando Silva Dec 27 '11 at 17:50
0
I provide an example on my blog entitled
C# Winforms and the Hidden Association Tag
Both which can be used... But the gist of it is that you either load the object Tag with the form and when its selected, launch that or have a specialized dictionary which holds the form as well. For a full example see the above links.
HTH

ΩmegaMan
- 29,542
- 12
- 100
- 122