Fill ListBox (from Class 'ThisAddIn')

S

Seran

Hello,
I have a sorteddirectory (class ThisAddIn) and would like to populate the
data to a listbox on a winform.
I have created a instance of the form, but how can I get the listbox control
for adding data?

Thanks in advance,
Seran
 
M

Morten Wennevik [C# MVP]

Hi Seran,

I'm not entirely sure I understand your problem, if you have created the
Form, surely you have created the ListBox as well? To populate the ListBox
you can use its DataSource property.

If you wonder how you can display the SortedDictionary you can do so using a
BindingSource.

ThisAddIn addIn = new ThisAddIn();
BindingSource bs = new BindingSource(addIn, "Dictionary");
listBox1.DataSource = bs;
listBox1.DisplayMember = "Value"; // Remove this line to display
both Key and Value

....

public class ThisAddIn
{
public System.Collections.Generic.SortedDictionary<int, string>
Dictionary { get; set; }
public ThisAddIn()
{
Dictionary = new
System.Collections.Generic.SortedDictionary<int, string>();
Dictionary.Add(1, "One");
Dictionary.Add(2, "Two");
Dictionary.Add(3, "Three");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top