Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 18, 2009 8:29 AM
Hello Everyone,
I have a problem and have not been able to solve.
I am generating and displaying a ListBox on the fly. Everything goes fine except that the items added to the llist box do not appear in the box.
Code looks like this:
listBoxAvailable.Top = 0;
listBoxAvailable.Left = 0;
listBoxAvailable.FormattingEnabled = true;
if (true == m_dictSomeDictionary.ContainsKey(someText))
{
foreach (String ModuleName in m_dictSomeDictionary[someText])
{
listBoxAvailable.Items.Add(ModuleName);
}
}
else
{
//Do Nothing.
}
The list box appears empty. But I know the items are present in the box because the Items.Count shows me the actual count as 3 (the number of modules I am adding)
I tried this:
foreach (String ModuleName in m_dictSomeDictionary[someText])
{
listBoxAvailable.Items.Add(ModuleName);
MessageBox.Show(listBoxAvailable.Items[index]);
}
and the values are accuretly displayed in the messagebox.
I modified the code to this:
if (true == m_dictSomeDictionary.ContainsKey(someText))
{
foreach (String ModuleName in m_dictSomeDictionary[someText])
{
listBoxAvailable.Items.Add(ModuleName);
}
}
else
{
//Do Nothing.
}
listBoxAvailable.Items.Add("Testing");
and I can see the "Testing" in the the listbox but none of the other items.
Whats wrong?
Thanks in Advance :)
All replies (13)
Friday, September 18, 2009 9:42 AM ✅Answered
Hello,
You have some critical matter to solve there, you constantly creating objects even though you created it once for that node.
Why are you resuming the layout, and then suspend it afterwards ? aside the code seems pretty valid.
Eyal, Regards.
Friday, September 18, 2009 11:24 AM ✅Answered
TabControl tabControl = new TabControl();
TabPage tabPage;
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
tabPage = new TabPage();
ListBox listBoxAvailable = new System.Windows.Forms.ListBox();
tabControl.ResumeLayout(false);
tabControl.SuspendLayout();
tabPage.Padding = new System.Windows.Forms.Padding(3);
tabPage.UseVisualStyleBackColor = true;
tabPage.Text = e.Node.Text;
listBoxAvailable.Top = 0;
listBoxAvailable.Left = 0;
listBoxAvailable.FormattingEnabled = true;
if (true == m_dictSomeDictionary.ContainsKey(e.Node.Text))
{
foreach (String moduleName in m_dictSomeDictionary[e.Node.Text])
{
listBoxAvailable.Items.Add(moduleName);
}
}
else
{
//Do Nothing.
}
tabPage.Controls.Add(listBoxAvailable);
tabControl.Controls.Add(tabPage);
//Add Tab Control to the Form.
this.Controls.Add(tabControl);
}
I solved it.
The problem was that I was creating the object of TabControl inside the method.
The above code works fine. A new tabpage is created everytime and added to the tabcontrol.
Thanks for the help :)
Friday, September 18, 2009 8:32 AM
Hello,
Did you add the ListBox to the form control's collection ? like so.
this.Controls.Add(myListBoxInstance);Eyal, Regards.
Friday, September 18, 2009 8:43 AM
Code Snippet
- public partial class Form1 : Form
- {
- public Form1() {
- InitializeComponent();
- Point point = new System.Drawing.Point(58, 322);
- Size size = new System.Drawing.Size(120, 95);
- ListBox listBox = CreateListBox(point, size);
- listBox.Items.Add("Item A");
- listBox.Items.Add("Item B");
- listBox.Items.Add("Item C");
- }
- public ListBox CreateListBox(Point point, Size size) {
- ListBox listBox = new ListBox();
- Controls.Add(listBox);
- listBox.FormattingEnabled = true;
- listBox.Location = point;
- listBox.Size = size;
- return listBox;
- }
- }
Eyal, Regards.
Friday, September 18, 2009 8:51 AM
Yes I have added it to the Controls.
tabPage.Controls.Add(listBoxAvailable);
tabControl.Controls.Add(tabPage);
this.Controls.Add(tabControl);
If I add like this:
listBox.Items.Add("Item A");
it works fine, But as I have stated above that listBoxAvailable.Items.Add("Testing"); works but none of the items added inside the foreach loop are displayed.
Friday, September 18, 2009 9:01 AM
Here is the complete code.
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
TabControl tabControl = new TabControl();
TabPage tabPage = new TabPage();
ListBox listBoxAvailable = new System.Windows.Forms.ListBox();
tabControl.ResumeLayout(false);
tabControl.SuspendLayout();
tabPage.Padding = new System.Windows.Forms.Padding(3);
tabPage.UseVisualStyleBackColor = true;
tabPage.Text = e.Node.Text;
listBoxAvailable.Top = 0;
listBoxAvailable.Left = 0;
listBoxAvailable.FormattingEnabled = true;
if (true == m_dictSomeDictionary.ContainsKey(e.Node.Text))
{
foreach (String moduleName in m_dictSomeDictionary[e.Node.Text])
{
listBoxAvailable.Items.Add(moduleName);
}
}
else
{
//Do Nothing.
}
tabPage.Controls.Add(listBoxAvailable);
tabControl.Controls.Add(tabPage);
//Add Tab Control to the Form.
this.Controls.Add(tabControl);
}
Friday, September 18, 2009 9:07 AM
Hello,
I'm not quite sure what you're trying to do, but it's weird you actually trying to retrieve a value from the indexer and iterate it, you probably want to do something like this ?
Code Snippet
- foreach (DictionaryEntry ModuleName in m_dictSomeDictionary)
- {
- listBoxAvailable.Items.Add(ModuleName.Value);
- }
Eyal, Regards.
Friday, September 18, 2009 9:14 AM
The Dictionary m_dictSomeDictionary is of the following format:
<String, List<String>>
where the key is the type of a module and the value is all the modules of that perticular type.
Like this.
Type1 - Module1, Module2, Module3
Type2 - Module4, Module 5
and so on...
What I am trying to do is, display all the types (Type1, Type2) in a treeview. Then display all the modules of a type in a listbox depending on what user selects in the treeview. For ex: If user clicks on Type1 I show Module1, Module2 and Module3 in the listbox.
e.Node.Text gives me the type (Type1, Type2) what user selectes.
Friday, September 18, 2009 9:51 AM
I agree to creating the object repeatedly, but that should not hold ListBox from diaplaying Items, and same with Suspend and Resume layout.
Even if I create objects only once and comment out resume and suspend layout code lines it still doesnt work.
Friday, September 18, 2009 9:58 AM
That's why I said the code seems to be valid, as in working, I don't think the problem lies in the code you pasted, check the code through debugger.
Eyal, Regards.
Friday, September 18, 2009 10:53 AM
I see that the things go wrong from the second run.
That is, first time this function runs when the form is loaded because by default the first node gets selected.
But second run onwards( that is when I select nodes mannualy) the function runs without error and Debug mode shows no problems but I think the Controls are not getting added somehow. Is there any specific order to add the controls? Or is it related to the way I am creating the objects in the functions?
Friday, September 18, 2009 11:28 AM
Silly question but did you create an instance of Key/Value pairs for each available Node ? did you check that the dictionary actually returns the appropriate list from the indexer ?Eyal, Regards.
Friday, September 18, 2009 11:33 AM
Yes it returns. And ya it was kinda silly but you dont know that till you solve it and almost all questions seems silly once you solved it!
Regards, Vijay