Question

This is the first time I'm trying to build something with a tab control.

At first I made a small application in .NET 4 C#, which had a listbox as a stand alone at its main window and now I want to add a tab control and move the listbox into one of the tabs and a listview into the other tab so I can also present Icons.

My problems I'm facing are as follows:

1) I'm now adding the listbox dynamically to the tab control like this:

private ListBox listBoxMember = new ListBox();
public Form1()
{
  listBoxMember.Size = tab1.Size;
  tab1.Controls.Add(listBoxMember);
}

When starting the application, it seems like the listbox doesn't fit itself inside the entire tab and I can see it's borders inside the tab.. How can I fir the listbox completely into the tab so it would seem to the user that the tab itself is like a listbox?

2) Before my change I made a context menu which was activated with the listbox's mouse down event upon right clicking the mouse. Now after adding the listbox inside the tab, the menu won't open when the mouse is clicked. How can I use the context menu I made and use it the same way as I did when the listbox was a stand alone control?

Was it helpful?

Solution

To fill the tab with your ListBox, use the Fill property.

listBoxMember.Dock = DockStyle.Fill;

Also since you are now creating the ListBox dynamically you'll need to set the ContextMenu dynamically as well.

listBoxMember.ContextMenu = myContextMenu;

Also be sure to give the listBoxMember a name so you can find it in the tab1.Controls collection.

listBoxMember.Name = "listBoxMember";
tab1.Controls["listBoxMember"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top