设计与制作从其他类分配选项卡控件属性 [英] Designing & Assigning tab control properties from other class

查看:75
本文介绍了设计与制作从其他类分配选项卡控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用选项卡控件.我已将选项卡控件放置在表单上,​​我想从我创建的其他类中访问该选项卡控件.我想在我创建的类的选项卡控件上显示标签和面板.我的是一个多线程的应用程序.我想在运行时将文本分配给选项卡控件上的标签,具体取决于类中生成的值.我已经创建了一个函数add_tab()来执行需要的操作,但该控件未以表单形式显示.

I am working with a tab control.I''ve placed a tab control on a form & I want to access that tab control from other class which I''ve create. I want to display labels & panels on that tab control from the class that I''ve created. Mine is a multithreaded application. I want to assign the text to the labels on the tab control at run time depending upon the values generated in the class. I have created a function add_tab() to do the needful but the control is not getting displayed in the form.

推荐答案

对,首先,第一件事:如果您是多人-threaded,则只能从创建它们的线程(GUI线程)访问控件.除非您适当地调用它们,否则尝试从其他任何线程执行都会带来问题.
例如,如果要添加新的选项卡"页面,则可以编写一个通用方法来执行此操作或根据需要取消调用:
Right, first things first: If you are multi-threaded, then you can only access the controls from the thread that created them: the GUI thread. Trying to do so from any other thread will give problems unless you Invoke them appropriately.
For example, if you want to add a new Tab page, you could write a generic method to do it with or wothout the invoke as required:
private void AddNewTab(string tabName)
    {
    if (InvokeRequired)
        {
        Invoke(new MethodInvoker(delegate { AddNewTab(tabName); }));
        }
    else
        {
        TabPage tp = new TabPage(tabName);
        myTabControl.TabPages.Add(tp);
        }
    }



其次,不要尝试从表单类外部访问选项卡控件:它将两个独立类(表单和新类)的设计联系在一起,因此,如果不考虑对另一个类的影响,就无法进行更改.这绝对不是OOP设计!
相反,提供公共方法或属性,使外部世界可以设置新值,并在表单类中进行所有工作.
您还可以使用自定义EventArgs实现事件来处理此问题,但这可能对您要实现的目标有些过大.



Secondly, do not try to access your tab control from outside the form class: It ties the design of two independent classes (the form and your new class) together, so you cannot make changes without considering the effects on the other class. This is definitely not OOP design!
Instead, provide public methods or properties which allow the outside world to set new values, and do all the work inside your form class.
You could also implement events to handle this, with custom EventArgs, but that might be a little overkill for what you are trying to achieve.


这篇关于设计与制作从其他类分配选项卡控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆