如何以其他形式访问工具条项目? [英] How Can I Access Toolstrip Items In Another Form?

查看:52
本文介绍了如何以其他形式访问工具条项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有mdiparent控件和多种形式的软件。



基本上我有登录表单,如果登录用户名和密码正确,它将启用所有其他工具条菜单

其他

它会保持不变。



所以我以登录形式开发了代码?

喜欢这个...



 如果 (name == UName.Text&& pass == PWord.Text)
{

ClassEnq.Uname = UName.Text;
MessageBox.Show( 密码已接受);
this .Close();
ParentWindow.enquiryToolStripMenuItem.Enabled = true ;
}



但它会给我一个错误

因保护级别无法访问?



我可以为这个问题做些什么。?

解决方案

具体如何处理这取决于你如何处理你的登录表格。 br />
如果它是一个应用程序模型表单(即一个对话框:apoplication在成功登录之前没有做任何其他事情)那么这很简单 - 只需在对话框关闭时直接在父表单中启用菜单。 br />


如果它是一个正常的形式,其他的事情可能发生但他不能使用工具,直到他登录然后它有点复杂,但仍然很漂亮很简单。

基本上,你在登录表单中创建一个事件,上面写着用户已登录(可能还有一个说他再次登出)并且你在父母那里处理了这个事件 - 父母然后适当地启用和禁用工具。这样,父更改会在不影响登录表单的情况下进行更改(例如,您可以将工具条交换为菜单,或者添加第二个工具条。)

这也很容易做到,它看起来很复杂!这应该有所帮助:在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ]


在表单中设置一个已添加ToolStripMenuItem的属性。



  public  bool enquiryToolStripMenu_Enabled 
{
set {this.enquiryToolStripMenuItem.Enabled = value; }
}





创建一个方法将值传递给上面的属性。您需要检查所有打开的表单,然后访问我们创建属性的表单对象(我们有ToolStripMenuItem)。考虑你的ToolStripMenuItem在From FrmMain上(Id = Frmmain)。



 public void setEnableToolStripMenuItem(bool status)
{
尝试
{
FormCollection fc = Application.OpenForms;
foreach(fc中的表格项目)
{
if(item.Name ==FrmMain)
{
((FrmMain)item).enquiryToolStripMenu_Enabled = status ;
休息;
}
}
}
catch(例外情况)
{

}
}





现在,无论您想要启用还是禁用ToolStripMenuItem,都可以调用此方法。



 setEnableToolStripMenuItem(假); 


i have created a software which has mdiparent control and serveral form.

basically i have login form and if login username and password correct it will enable all other toolstrip menu
else
it will remain same.

so i developed a code in login form?
like this...

if (name == UName.Text && pass == PWord.Text)
                    {

                        ClassEnq.Uname = UName.Text;
                        MessageBox.Show("Password Accepted");
                        this.Close();
                        ParentWindow.enquiryToolStripMenuItem.Enabled = true;
                    }


but it''ll give me an error
inaccessable due to protection level?

what i can do for this issue.?

解决方案

Exactly how you handle this depends on how you handle your Login form.
If it is an application model form (i.e. a dialog: the apoplication does nothing else until he has logged in successfully) then it's easy - just enable the menu directly in teh parent form when the dialog closes.

If it's a "normal" form, and other things can happen but he can't use tools until he does log in then it's a little more complex, but still pretty easy.
Basically, you create an event in the login form which says "user logged in" (and probably one which says "he logged out again") and you handle that in the parent - the parent then enables and disables the tools appropriately. This way, the parent change change without affecting the login form (you could swap the toolstrip for a menu for example, or add a second toolstrip.)
That's also very easy to do, it just looks complex! This should help: Transferring information between two forms, Part 2: Child to Parent[^]


Set a property in your Form on which you have added ToolStripMenuItem.

public bool enquiryToolStripMenu_Enabled
        {
            set { this.enquiryToolStripMenuItem.Enabled = value; }
        }



Create a method to pass value to the above property. You need to check for all open forms and then access the form object where we have created the property(where we have the ToolStripMenuItem). Consider your ToolStripMenuItem is on From FrmMain(Id=Frmmain).

public void setEnableToolStripMenuItem(bool status)
        {
            try
            {
                FormCollection fc = Application.OpenForms;
                foreach (Form item in fc)
                {
                    if (item.Name == "FrmMain")
                    {
                        ((FrmMain)item).enquiryToolStripMenu_Enabled = status;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                
            }
        }



Now call this method wherever you want to enable or disable the ToolStripMenuItem.

setEnableToolStripMenuItem(false);


这篇关于如何以其他形式访问工具条项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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