选项卡控件的 ContainsKey() 方法与什么进行比较?
我想创建一个仅打开内容一次的选项卡控件。每个打开的项目都需要检查容器以确保它尚未显示。我相信我想使用的方法是 bool TabControl.TabPages.ContainsKey(string key) 方法,但它始终返回值 false。
我创建了一个解决方法,将对象存储在与选项卡控件保持同步的单独列表中,但感觉非常错误。我在控件的 TabPages 属性中有一个列表,因此我应该能够对其进行查询。
我是否缺少财产?我对这种方法及其执行效果的期望是否正确?如何让它正确识别我打开的选项卡?
这是一些与我正在做的类似的示例代码:
private void _fillTabControl()
{
List<string> keys = new List<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);
}
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<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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该文档说明了一切:
“Name 属性对应于 TabControl.TabPageCollection 中 TabPage 的键。”
您正在使用 Text 属性,您应该设置
然后
将返回 true;
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
and then
will return true;