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

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

问题描述

当我在第一个窗体上的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.DataSource DisplayMember ValueMember 正确设置它,但是当我第一次设置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

推荐答案

表格 是与其他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 中,在设计师处,选择控件,然后在属性窗口中将修饰符属性设置为公共。还要确保 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-在第一个表单中创建公共方法或属性,并将第一个表单的实例传递给第二个表单的构造器

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

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

在您的<$ c $中c> 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-在第一个表单中使用第二个表单的事件

看看这个帖子

示例6-在第二个表单中注入动作

看看这个帖子

示例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天全站免登陆