Reputation: 1549
So this is probably an easy one, but I have just started learning the subtle art of C#, so forgive me if I come across as a little 'green'.
I've been experimenting with boxes. ListBoxes as of late, to be precise. In my class I have the following:
ListBox lb = new ListBox();
lb.Location = new System.Drawing.Point(12, 12);
lb.Name = "ListBox1";
lb.Size = new System.Drawing.Size(245, 200);
lb.BackColor = System.Drawing.Color.Blue;
lb.ForeColor = System.Drawing.Color.Green;
lb.Items.Add("Element One");
lb.Items.Add("Element Two");
lb.Items.Add("Element Two");
lb.Show();
Now, I've been working under the assumption that ListBox would somehow work as MessageBoxes do.
I have a:
var confirmResult = MessageBox.Show("Question asking about " + variable + "?", "TitleHere", MessageBoxButtons.YesNo);
...and I thought the ListBox would work much the same?
Problem is, it does not.
The MessageBox pops up in all its glory, but the ListBox is nowhere to be seen.
Am I missing something?
UPDATE:
Ok, so Form
is the way to go they say.
I tried:
ListBox lb = new ListBox();
lb.Location = new System.Drawing.Point(12, 12);
lb.Name = "ListBox1";
lb.Size = new System.Drawing.Size(245, 200);
lb.BackColor = System.Drawing.Color.Blue;
lb.ForeColor = System.Drawing.Color.Green;
lb.Items.Add("Element One");
lb.Items.Add("Element Two");
lb.Items.Add("Element Two");
Form f = new Form();
f.Controls.Add(lb);
Buuut this still isn't showing my pretty box.
Please advise.
Upvotes: 0
Views: 2478
Reputation: 194
As written on Microsoft msdn, the message box:
Displays a message box that can contain text, buttons, and symbols that inform and instruct the user.
and the ListBox:
Represents a Windows control to display a list of items.
So you don't need a container (like Form) to display a messageBox, but you need one for Controls, like ListBox.
UPDATE:
You should probably also add something like:
form.ShowDialog();
In addition to ListBox microsoft's page you should also check out one about Form class. They have neat examples if you scroll to the bottom of the page.
Upvotes: 1
Reputation: 841
You need to add the listbox to a container on your form (e.g. a panel or the form itself).
e.g.
MyForm.Controls.Add(lb)
or
panel1.Controls.Add(lb)
You don't need to do
lb.Show();
Upvotes: 3