vendredi 17 avril 2015

Listbox not showing

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.


Aucun commentaire:

Enregistrer un commentaire