如何在一种形式中单击按钮以更改另一种形式的选项? C# [英] How do i make a button click in one form change an option in another form? C#

查看:102
本文介绍了如何在一种形式中单击按钮以更改另一种形式的选项? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在构建一个孩子的Web浏览器,我需要更改主浏览器中某些按钮的视图,具体取决于父母是否希望孩子可以使用该选项.

我已经制作了一个表单,该表单将允许父级显示或隐藏打印机按钮,但是由于它是新表单,因此无法识别主表单中按钮的名称

我知道这真的很简单,但是我不知道如何加入他们

这是我的代码

public partial class printer : Form
    {
        public printer()
        {
            InitializeComponent();
        }
        public void btnShowPrinter_Click(object sender, EventArgs e)
        {
            loggedIn = false;
            btnPrint.Visible = true;
        }
        private void btnHidePrinter_Click(object sender, EventArgs e)
        {
            loggedIn = false;
            btnPrint.Visible = false;
        }
    }





loginIn和btnprint都出现错误

名称"btnPrint"在当前上下文中不存在

我该如何从其他表格中读取它?

预先谢谢您.

解决方案

这里的方法是进行配置的表单将为您的应用程序修改配置模型的引用.您引用的另一种形式将在初始化自身以在其GUI中反映所配置的内容时获得配置模型.如果在后一种形式仍处于打开状态时配置模型发生更改,则应引入一个配置已更改事件,该表单将为此事件连接一个事件处理程序.因此,如果在配置更改时浏览器表单处于开放状态,则将通知该事件,并可以相应地更改其GUI.

最好的问候,

-MRB


请不要!
那是简单的答案.您无法访问其他表单上的按钮,因为默认情况下它们被声明为private.如果不是,那么这两种形式总是联系在一起的-如果更改一种形式,则必须查看该更改如何影响另一种形式.这使重用变得困难,并可能影响可靠性.

而是以第二种形式提供公共属性或方法,并从第一种形式访问它们.该属性或方法对表单控件进行了更改,因此您可以在不影响外界的情况下更改控件.

剩下的就是让您与第二种形式的正确实例进行交互!
显示时,您按照以下方式进行了操作:

MySecondForm secondForm = new MySecondForm();
secondForm.Show();

所有您需要做的就是将secondForm移为私有类级别的变量,您可以在需要时访问它:

secondForm.DisablePrinting();


secondForm.CanPrint = false;


将值存储在注册表中.当选择形成的负荷解析注册表条目听写控制状态..例如,DWORD REG进入CHK1 = 0,复选框选中的负载,= 1,负载检查.
另一种方法是使用应用程序设置页面,创建一个bool值,然后通过表单访问它.
bool Button可见设置
this.btnOption.Visibile = Properties.Settings.Default.ButtonVisible;
注意,您必须调用:Properties.Settings.Default.Save();.更改值后.

这是一个更完整的示例,包括在主窗体上使用PropertyChanged事件来进行更改..

 公共 部分  class  Form1:表格
{
    公共 Form1()
    {
        InitializeComponent();
    }
    私有 无效 Form1_Load(对象发​​件人,EventArgs e)
    {
        //  Properties.Settings.Default.ButtonVisible = true; 
        Properties.Settings.Default.PropertyChanged + =  PropertyChangedEventHandler(Default_PropertyChanged);
    }
    无效 Default_PropertyChanged(对象发​​送者,PropertyChangedEventArgs e)
    {
        button1.Visible = Properties.Settings.Default.ButtonVisible;
         .Refresh();
    }
    私有 无效 button2_Click(对象发​​件人,EventArgs e)
    {
        frmOptions f =  frmOptions();
        f.Show( this );
    }
} 



选项形式:

< pre lang = " >
    公共部分 class  frmOptions:表格
    {
        公共frmOptions()
        {
            InitializeComponent();   
        }
        私人 void  frmOptions_Load(对象发​​送者,EventArgs e)
        {
            Properties.Settings.Default.ButtonVisible = false ;   
            Properties.Settings.Default.Save();   
        }
    } 


hi
I am building a kids web browser and i need to change the view of some of the buttons in the main browser depending on if the parent wants that option available to the child.

I have made a form which will allow the parent to show or hide printer button but since it is in a new form it doesn''t recognise the name of the buttons in the main form

i know this is really simple, but i don''t know how to join them

here is my code

public partial class printer : Form
    {
        public printer()
        {
            InitializeComponent();
        }
        public void btnShowPrinter_Click(object sender, EventArgs e)
        {
            loggedIn = false;
            btnPrint.Visible = true;
        }
        private void btnHidePrinter_Click(object sender, EventArgs e)
        {
            loggedIn = false;
            btnPrint.Visible = false;
        }
    }





loggedIn and btnprint are both getting errors

The name ''btnPrint'' does not exist in the current context

how do i make it read from the other form please?

thank you in advance

解决方案

The way to go here is that the form that does the configuration will modifiy a reference of the configuration model for your application. The other form you referenced will get the configuration model when initializing itself to reflect in its GUI what was configured. If the configuration model changes while the latter form is still open you should introduce a configuration changed event for which said form will hook up an event handler. Thus if the browser form was openend when the configuration changed it will be notified of that event and can change it''s GUI accordingly.

Best Regards,

-MRB


Please don''t!
That''s the simple answer. You can''t access the buttons on a differnt form because they are declared private by default. If they weren''t, then the two forms are always tied together - if you change one, you have to look at how that change might affect the other form. This makes re-use difficult, and can affect reliability.

Instead, provide a public property or method in the second form, and access them from the first. The property or method makes changes to the form controls, so you can change the controls, without affecting the outside world.

All that leaves is for you to interact with the correct instance of the second form!
When you displayed, it you did something along the lines of:

MySecondForm secondForm = new MySecondForm();
secondForm.Show();

All you have to do is move secondForm to be a private class level variable, and you can access it when you need to:

secondForm.DisablePrinting();

or

secondForm.CanPrint = false;


Store the values in the registry. When the options form loads parse registry entries that dictate control state.. eg, dword reg entry chk1 = 0, checkbox loads unchecked, = 1, loads checked.
Another method is using the applications settings page, create a bool value, then access it through the forms.
bool ButtonVisible setting
this.btnOption.Visibile = Properties.Settings.Default.ButtonVisible;
Note you have to call: Properties.Settings.Default.Save(); after changing a value.

Here''s a more complete example, including using the PropertyChanged event on the main form to propogate the change..

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        //Properties.Settings.Default.ButtonVisible = true;
        Properties.Settings.Default.PropertyChanged += new PropertyChangedEventHandler(Default_PropertyChanged);
    }
    void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        button1.Visible = Properties.Settings.Default.ButtonVisible;
        this.Refresh();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        frmOptions f = new frmOptions();
        f.Show(this);
    }
}



options form:

<pre lang="cs">
    public partial class frmOptions : Form
    {
        public frmOptions()
        {
            InitializeComponent();
        }
        private void frmOptions_Load(object sender, EventArgs e)
        {
            Properties.Settings.Default.ButtonVisible = false;
            Properties.Settings.Default.Save();
        }
    }


这篇关于如何在一种形式中单击按钮以更改另一种形式的选项? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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