What does the Tab Control's ContainsKey() method compare against?
I want to create a Tab Control that only opens content once. Each item that is opened needs to check the container to make sure it isn't already being displayed. I believe the method I want to use is the bool TabControl.TabPages.ContainsKey(string key) method, however it always returns a value of false.
I've created a work-around where I store the object in a separate list that I keep in sync with the tab control, but it feels very wrong. I have a list in the TabPages property of the control, so I should be able to query against it.
Am I missing a property? Is my expectation of this method and what it's performing correct? How do I get it to correctly identify my opened tabs?
Here is some sample code that is similar to what I'm doing:
private void _fillTabControl()
{
List<string> keys = new List&l开发者_运维知识库t;string>() { "one", "two" };
foreach (string key in keys)
_addTab(key);
bool alreadyOpened = tabControl.TabPages.ContainsKey(keys[0]);
}
private void _addTab(string key)
{
TextBox textBox = new TextBox();
textBox.Text = key;
TabPage tab = new TabPage();
tab.Text = key;
tab.Controls.Add(textBox);
tabControl.TabPages.Add(tab);
}
The doc says it all:
"The Name property corresponds to the key for a TabPage in the TabControl.TabPageCollection."
You're using the Text property, you should set
tab.Name = "MyName";
and then
tabControl.TabPages.ContainsKey("MyName");
will return true;
精彩评论