如何从第三种形式改变两种不同形式的文本框的价值? [英] How Do I Change The Value Of Textbox In Two Different Forms From Third Form?

查看:72
本文介绍了如何从第三种形式改变两种不同形式的文本框的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三种形式。

Form1,Form2和Form3。

我在表格1中有一个按钮。

Form2和form3有标签。

当我点击form1中的按钮时。

我希望Form2显示Hi this is form2,form3显示hi this is form 3。在标签上。

I have three forms.
Form1 , Form2 and Form3.
I have a button in form 1.
Form2 and form3 has labels.
When i click on the button in form1.
I want Form2 to show " Hi this is form2" and form3 to show "hi this is form three". on the labels.

推荐答案

我将在这里草拟单向实现多个次要表单更新,但是,请记住有很多方法可以解决这个问题,而且,如果不了解应用程序的总体目的和设计,选择最佳/更好的方法可能只是猜测。



架构的一个关键标准是你是否想要实现一种交换机架构,其中你的主表格是交换机,而辅助表格类似于电话:,换句话说,主表格是中心,其中消息从二级表格收到,消息发送到二级表格。



在此模型中,二级表格<我>不直接互相攻击:他们只能触发主表单中的代码执行的操作。



另一个模型是对象/表单之间的硬编码消息传递:在这个模型中,Form1(带有Button)将直接更新Form2和Form3上的标签。



我将在这里展示的草图是交换机型号。它使用类型'Action的代理('Action和'Func与.NET Framework 2.0一起到达)来传递消息,而不是使用Event / EventHandler约定。 'Action和'Func为C#提供了一个强大的语法,用于处理C / C ++或其他语言中的内容,并传递对可执行代码的引用(函数指针):'Action和'Func可以处理任意数量的强类型参数(好吧,我最后一次看,最多十七次没有出汗)。



1.主要表格:
I'm going to sketch out one way of achieving multiple secondary Form updates here, but, keep in mind there are many ways you could approach this, and, without really knowing the overall purpose and design of your Application, selecting the best/better way(s) may be just a guess.

A key criterion here for architecture is whether you want to implement a "switchboard" kind of architecture where your Main Form is the "switchboard" and the secondary Forms are analogous to "telephones:" in other words the Main Form is a "hub" where "messages" are received from the secondary Forms, and "messages" are sent to the secondary Forms.

In this model, the secondary Forms do not act on each other directly: they can only trigger actions that are carried out by code in the Main Form.

Another model would be hard-coding messaging between objects/Forms: in this model, Form1 (with the Button) would update the Labels on Form2, and Form3, directly.

The sketch I'll show here is of a "switchboard" type model. It uses Delegates of Type 'Action ('Action and 'Func arrived with .NET Framework 2.0) to pass "messages," rather than using the Event/EventHandler convention. 'Action and 'Func give to C# a powerful syntax for dealing with what in C/C++, or other languages, with passing references to executable code ("function pointers"): 'Action and 'Func can deal with any number of strongly Typed parameters (well, last time I looked, up to seventeen without breaking a sweat).

1. The Main Form:
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    
    private Form1 f1 = new Form1();
    private Form2 f2 = new Form2();
    private Form3 f3 = new Form3();
    
    // just to give each change in the Label content on Form2, Form3
    // a different message ... so you can see if the code is working
    public int count = 0;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        f1.Show();

        // this is where we inject a reference to code in MainForm
        // into the instance of 'Form1
        f1.SendMessage = HandleMessage;
    
        f2.Show();
        f3.Show();
    }
    
    private void HandleMessage(Form form, Control control)
    {
        switch (form.Name)
        {
            case "Form1":
                // got a message from Form1
                switch (control.Name)
                {
                    case "button1":
                        // Button1 on Form1 calling ...
                        // update the Labels on Form2, Form3
                        f2.MsgLabelUpdate("message #" + (++count).ToString());
                        f3.MsgLabelUpdate("message #" + count.ToString());
                        break;
                }
                break;
            // from here on ... just stubbed in to give you ideas
            case "Form2":
                switch (control.Name)
                {
                    case "button1":
                        break;
                    case "label1":
                        break;
                }
                break;
            case "Form3":
                switch (control.Name)
                {
                    case "button1":
                        break;
                    case "label1":
                        break;
                }
                break;
        }
    }
}

这里需要注意的要点是:



a。我们将可执行代码('HandleMessage)的引用注入到'Form1的实例中的'SendMessage Delegate of Type'动作。



湾当单击Form1上的Button时,它会执行MainForm'HandleMessage中的代码,传递对Form的引用和Button。



c。当'HandleMessage代码检测到消息来自Form1上的Button1时,它然后调用Form2中的可执行方法,以及Form3,它通过类型'Action的公共代表公开。



d。在HandleMessage代码中有一些草绘的其他可能性,只是为了让您了解您可以使用'HandleMessage作为一般来处理来自任何二级表单的多种类型的消息 - 目的发射器。



2. Form1 ...带按钮的表格...

Key points to note here are:

a. we injected a reference to executable code ('HandleMessage) into the 'SendMessage Delegate of Type 'Action in the instance of 'Form1.

b. When the Button on Form1 is clicked, it executes the code in the MainForm 'HandleMessage, passing references to the Form, and the Button.

c. When the 'HandleMessage code detects that the message came from Button1 on Form1, it then calls the executable method in Form2, and Form3, which is exposed through their public Delegates of Type 'Action.

d. There's a bunch of other possibilities that are "sketched in" in the 'HandleMessage code just to give you an idea that you could be handling many types of messages from any secondary Form using 'HandleMessage as a general-purpose "transmitter."

2. Form1 ... the Form with the Button ...

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public Action<Form,Control> SendMessage;

    private void button1_Click(object sender, EventArgs e)
    {
        SendMessage(this, button1);
    }
}

注意,如上所述,主窗体的'HandleMessage事件中的可执行代码的引用是由主窗体注入到Form1的实例中。 />


3. Form2:

Note, as mentioned, that a reference to the executable code in the Main Form's 'HandleMessage Event was injected into the instance of Form1 by the Main Form.

3. Form2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        MsgLabelUpdate = invokeLabelUpdate;
    }

    public readonly Action<string> MsgLabelUpdate;

    private void invokeLabelUpdate(string theText)
    {
        label1.Text = theText;
    }
}

请注意,在这里,在Form3中,我们不会任何内容注入到Form的实例中。我们只是创建一个间接公开引用私有'invokeLabelUpdate方法暴露给Form1



4. Form3:

Note that here, and in Form3, we don't inject anything into the instance of the Form. We simply create an indirect public reference to the private 'invokeLabelUpdate method that is exposed to Form1

4. Form3:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
        MsgLabelUpdate = invokeLabelUpdate;
    }

    public readonly Action<string> MsgLabelUpdate;

    private void invokeLabelUpdate(string theText)
    {
        label1.Text = theText;
    }
}

要问的好问题:



真的是必要为了创造间接对象和方法的暴露,去解决所有这些问题?



我的答案是它不是绝对必要的,但这样做的方式......我可以称之为一种方式实现正式的消息传递......有助于保持我们的对象(在这种情况下为表格)松散耦合,这与OO编程思想一致,即暗示关注点分离和封装是一件好事。 br />


使用此处显示的技术(或其他类似的技术)可以确保曝光......对外部对象产生的副作用最小。表格,类等等。

Good question to ask:

Is it really necessary to go to all this trouble to create indirect exposure of objects and methods ?

My answer would be that it is not absolutely necessary, but that doing things in this way ... a way which I might call "implementing formal message-passing" ... helps keep our objects (Forms in this case) loosely-coupled, and that is consistent with OO programming ideas that suggest that "separation of concerns" and "encapsulation" is a good thing.

Using the techniques shown here (or others, like them) can ensure that there are minimal possible side-effects resulting from the exposure ... to "outside" objects ... of Forms, Classes, etc.


这是一个概念证明。更好的方法是使用公共方法创建Form的子类,可以从父表单调用以更改标签。



Here's a proof of concept. A better way would be to make a subclass of Form with public method that can be called from the parent form to change the labels.

public partial class Form1 : Form
    {
        private List<Form> children;
        private List<Label> lbls;

        public Form1()
        {
            InitializeComponent();
            this.children = new List<Form>();
            this.lbls = new List<Label>();
            OpenForm(2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 1;
            foreach (Label l in lbls)
            {
                l.Text = string.Format("This is form{0}", ++i);
            }
        }

        private void OpenForm(int n)
        {
            while (n-- > 0)
            {
                Form f2 = new Form();
                Label l = new Label();
                l.Text = "new form";
                f2.Controls.Add(l);
                f2.Show();
                this.children.Add(f2);
                this.lbls.Add(l);
                f2.Disposed += f2_Disposed;
            }
        }

        void f2_Disposed(object sender, EventArgs e)
        {
            try
            {
                Form f = sender as Form;
                int i = children.IndexOf(f);
                children.RemoveAt(i);
                lbls.RemoveAt(i);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }


亲爱的,

我想:

表单子声明委托事件。

Dear,
I think:
in Form child declare delegate event.
public delegate void DelegateFunction(string str);
public event DelegateFunction eDelegateFunction;





然后按钮点击事件是



and then button click event is

private void btnClick_Click(object sender, EventArgs e)
{
    eDelegateFunction("Text value");
}



表格1 :(父母表格)




In form 1:(form parent)

FormB frm = new FormB();
frm.eDelegateFunction += new FormB.DelegateFunction(frm_eDelegateFunction);
frm.Show();





并声明功能



and declare funtion

private void frm_eDelegateFunction(string strText)
{
    // set text here
    textBox1.Text = strText;
}


这篇关于如何从第三种形式改变两种不同形式的文本框的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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