如何更改从另一种形式表单的控制? [英] How to Change a Control of a Form from Another Form?

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

问题描述

我想设置comboBox.SelectedValue当我选择在我的第一形式的具有另一种形式的该值来填充组合框上的dataGridView行,第二种形式在我的加载事件我有`comboBox.DataSource,DisplayMemeber,ValueMemeber集它正确,但是没有什么是我设置了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, on second form in my load event I have `comboBox.DataSource, DisplayMemeber, ValueMemeber set it correctly but nothing is happening when I set selectedValue on first,everything works great when I do it on one 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.

您可以创建在第二形式的公共属性或方法,并创建第二个窗体的实例后,设置这些属性。这样,当你在第二种形式需要你可以使用它们。此选项不仅限于创建第二个窗体时传递值。你甚至可以秒表格的执行过程中使用该属性。此外,它是用于获取它的值是有用的。

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.

您可以创建第二种形式和创造的第二种形式的实例在第一种形式认购,并把code在处理不断变化的形式之后的事件。然后,它足以提高第二种形式的事件。

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.

您可以定义类型的公共属性动作或第二种形式,然后创建的第二种形式的实例后,一些其他的委托类型,使用分配的财产自定义操作。然后在第二个形式,这是不够的时候,你需要首先处理的形式来调用的行动。

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;
    }
}

然后在你的第一种形式,它足以值传递给窗体2 当你创建它的一个新的实例:

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;}
    }
}

然后在你的第一种形式,它足以值传递给窗体2 当你需要,创建后窗体2 或者当你需要设置窗体2 textBox1的

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);
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.

在你的表格,在设计,选择控件,然后在属性窗口中的修饰符属性设置为公开。另外,还要确保在 GenerateMember 属性是真正。然后你就可以使用其名称来自表格之外即可打开此控制。

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.

在你的 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;
}

然后创建窗体2 一个构造函数,接受类型的参数 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)
{
    f.ChangeTextBox1Text("Some Value");
}

当创建

现在窗体2 你应该通过 Form1中的一个实例吧:

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

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

示例5 - 第一表格第二种形式的使用事件

看看这个<一个href=\"http://stackoverflow.com/questions/37483278/how-do-i-feed-values-to-the-statusstrip-from-a-form-control\">post.这是关于形式和控制之间的沟通,但也形式之间是适用于交际。

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 - 注射第二种形式的动作

看看这个<一个href=\"http://stackoverflow.com/questions/37483278/how-do-i-feed-values-to-the-statusstrip-from-a-form-control\">post.这是关于形式和控制之间的沟通,但也形式之间是适用于交际。

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通过第一种形式的实例第二种形式,并保持在一个领域,当你需要使用它。使用公共方法或属性preferred。

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)
{
    f.textBox1.Text = "Some Value";
}

当创建窗体2 你应该通过 Form1中的一个实例吧:

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

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

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

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