表单之间的交互——如何从另一个表单更改一个表单的控件? [英] Interaction between forms — How to change a control of a form from another form?

查看:24
本文介绍了表单之间的交互——如何从另一个表单更改一个表单的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在第一个表单上选择 dataGridView 中的行以在另一个表单上使用该值填充 comboBox 时,我想设置 comboBox.SelectedValue,

I would like to set comboBox.SelectedValue when I select the row in my dataGridView on first form to populate comboBox with that value on another form,

在我的加载事件中的第二个表单中,我有 comboBox.DataSourceDisplayMemberValueMember 正确设置它,但是当我设置时什么也没有发生selectedValue 首先.当我在一种形式上做时,一切都很好

On second form in my load event I have comboBox.DataSource, DisplayMember, ValueMember set it correctly but nothing is happening when I set selectedValue on first. Everything works great when I do it on one form

推荐答案

Form 在 Windows 窗体中与其他 C# 类一样是一个类.表单之间的通信方式与类相同.您可以在类之间进行通信时考虑此选项:

Form in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:

从第一个表单操作第二个表单

  • 您可以向第二种形式的构造函数添加合适的参数.然后,您可以在创建第二种形式的实例时将值传递给构造函数.在第二种形式中,将参数存储在成员字段中,并在需要时使用它们.

  • You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.

您可以在第二种形式中创建公共属性或方法,并在创建第二种形式的实例后设置这些属性.这样您就可以在需要时以第二种形式使用它们.此选项不限于在创建第二个表单时传递值.您甚至可以在第二个 Form 的执行期间使用该属性.此外,它对于从中获取价值也很有用.

You can create public property or method in the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form. This option is not limited to passing values when creating the second form. You can even use that property during the execution of second Form. Also it's useful for getting a value from it.

作为另一种选择,您可以将要操作的控件设为公开,这样您就可以从其他表单访问它.使用方法是更推荐的方法.

As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.

从第二个表单操作第一个表单

  • 您可以在第一种形式中创建公共方法或属性,并将第一种形式的实例传递给第二种形式.然后在传递的实例上使用该方法/属性,您可以操作第一个表单.

  • You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.

您可以在第二个表单中创建一个事件,并在创建第二个表单的实例后以第一个表单订阅它,并将更改表单的代码放在处理程序中.然后以第二种形式引发事件就足够了.

You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.

您可以在第二种形式中定义 Action 类型的公共属性或某些其他委托类型,然后在创建第二种形式的实例后,使用自定义操作分配属性.然后在第二种形式中,当您需要操作第一种形式时调用操作就足够了.

You can define a public property of type Action or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.

此外,您可以在此处将第一个表单的控件设为公开,然后如果您将第一个表单的实例传递给第二个表单,您就可以操作该控件.建议使用其他解决方案.这就像创建公共属性或方法一样,但是在控件上执行特定任务的方法比公开整个控件更好.但有时您可能需要此解决方案.

Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.

以下是有关上述解决方案的一些有用示例.

Here are some useful examples about above solutions.

示例 1 - 使用第二个表单的构造函数

在创建第二个表单时需要将一些数据传递给第二个表单时使用此示例.

Use this example when you need to pass some data to second form, when creating the second form.

public partial class Form2 : Form
{
    int selectedValue;
    public Form2(int value)
    {
        InitializeComponent();
        selectedValue = value;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        //Load data
        this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
        this.comboBox1.DisplayMember = "Name";
        this.comboBox1.ValueMember = "Id";
        this.comboBox1.SelectedValue = selectedValue;
    }
}

然后在您的第一个表单中,当您创建它的新实例时,将值传递给 Form2 就足够了:

Then in your first form, it's enough to pass the value to Form2 when you create a new instance of it:

var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();

示例 2 - 使用公共属性或第二种形式的方法

当您需要将一些数据传递给第二个表单时,在创建或什至在创建第二个表单之后使用此示例.

Use this example when you need to pass some data to second form, when creating or even after creation of second form.

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    public string SomeValue
    {
        get { return textBox1.Text;}
        set { textBox1.Text = value;}
    }
}

然后在您的第一个表单中,在创建Form2 之后或需要设置textBox1 的值时,将值传递给Form2 就足够了Form2 上:

Then in your first form, it's enough to pass the value to Form2 when you need, after creating Form2 or whenever you need to set value of textBox1 on Form2:

var f = new Form2(); //value is not needed here
f.SomeValue = "some value";
f.Show();
//...
f.SomeValue = "some other value";

示例 3 - 公开第二个表单的控件

当您需要更改第二个表单上的控件的属性时,在创建或什至在创建第二个表单之后,请使用此示例.最好使用公共属性或方法,而不是暴露整个控件属性.

Use this example when you need to change a property of a control on second form, when creating or even after creation of second form. It's better to use public property or method instead of exposing whole control properties.

在您的 Form 中,在设计器中选择控件并在属性窗口中将 Modifiers 属性设置为 Public.还要确保 GenerateMember 属性为 true.然后,您可以从 Form 外部使用其名称简单地访问该控件.

In your Form, at designer, select the control and in Properties window set the Modifiers property to Public. Also make sure the GenerateMember property is true. Then you can simply access this control using its name from outside of the Form.

var f = new Form2();
f.textBox1= "some value";

从第二个表单操作第一个表单

示例 4 - 在第一个表单中创建公共方法或属性,并将第一个表单的实例传递给第二个表单的构造函数

当您需要从第二个表单更改第一个 Form 时使用此示例.

Use this example when you need to change first Form from second Form.

在您的 Form1 中,创建一个方法的属性,该属性接受一些参数并将逻辑放入其中:

In your Form1, create a property of a method that accepts some parameters and put the logic in it:

public void ChangeTextBox1Text(string text)
{
    this.textBox1.Text = text;
}

然后在 Form2 中创建一个构造函数,它接受 Form1 类型的参数并将传递的值保存在成员字段中,并在需要时使用:

Then create a constructor in Form2 which accepts a parameter of type Form1 and keep the passed value in a member field and use it when you need:

Form1 form1;
public Form2 (Form1 f)
{
    InitializeComponent();
    form1 = f; 
}
private void button1_Click(object sender, EventArgs e)
{
    form1.ChangeTextBox1Text("Some Value");
}

现在当创建 Form2 时,你应该传递一个 Form1 的实例给它:

Now when creating Form2 you should pass an instance of Form1 to it:

var f = new Form2(this);
f.Show();

示例 5 - 在第一个表单中使用第二个表单的事件

看看这个发布.它是关于窗体和控件之间的通信,但它也适用于窗体之间的通信.

Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.

示例 6 - 以第二种形式注入一个动作

看看这个发布.它是关于窗体和控件之间的通信,但它也适用于窗体之间的通信.

Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.

示例 7 - 公开第一个表单的控件

在此解决方案中,您需要将第一个表单中的控件设为公开,例如示例 3.然后像示例 4 一样将第一个表单的实例传递给第二个表单,并将其保存在一个字段中,并在您需要时使用它.最好使用公共方法或属性.

In this solution you need to make a control in first form public, like example 3. Then like example 4 pass an instance of the first form to second form and keep it in a field and use it when you need. Using a public method or property is preferred.

Form1 form1;
public Form2 (Form1 f)
{
    InitializeComponent();
    form1 = f; 
}
private void button1_Click(object sender, EventArgs e)
{
    form1.textBox1.Text = "Some Value";
}

在创建 Form2 时,您应该将 Form1 的实例传递给它:

when creating Form2 you should pass an instance of Form1 to it:

var f = new Form2(this);
f.Show();

这篇关于表单之间的交互——如何从另一个表单更改一个表单的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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