简体   繁体   English

将ComboBox选定的值从Form1获取到另一个Form2

[英]Get ComboBox selected value form Form1 to another Form2

I have a C# windows app that has two forms Form1 which is the main form and Form2. 我有一个C#Windows应用程序,它具有两个窗体Form1,即主窗体和Form2。 Form1 has a combobox on it and Form2 has a textbox. Form1上有一个组合框,Form2上有一个文本框。

I want to put the value selected in the Form1.ComboBox1 into Form2.TextBox1. 我想将在Form1.ComboBox1中选择的值放入Form2.TextBox1。

I am trying this: 我正在尝试:

        Form1 Form1Object = new  Form1();
        string fff = Form1Object.ComboBox1.SelectedItem.ToString(); //not working
        TextBox1.Text = fff;

Problem is that when I run this Form1 is reinitialized and i don't want that. 问题是,当我运行此Form1时,它会被重新初始化,而我不希望这样。 (I have a splash screen that runt when the application starts so when i run my code the splashscreen starts all over again. (我有一个启动屏幕,该启动屏幕在应用程序启动时不可用,因此当我运行代码时,启动屏幕会重新开始。

Is there a way to read ComboBox1 value without restarting the first form? 有没有办法在不重新启动第一个表格的情况下读取ComboBox1值? If I try it directly it does not work, it sees the Form1 as calss instead of object. 如果我直接尝试它不起作用,它将Form1视为calss而不是object。

    Form1.ComboBox1.SelectedItem.ToString(); //does not work

I am also trying to add the value to the textbox when opening the second form: 我还尝试在打开第二个表单时将值添加到文本框中:

        Form2 form2 = new Form2();
        form2.TextBox1.Text = ComboBox1.SelectedValue.ToString();
        form2.Show();  

This gives me the following error: "Object reference not set to an instant of an object." 这给我以下错误:“对象引用未设置为对象的瞬间。”

EDIT: It works using this code: 编辑:它使用此代码工作:

        Form2 form2 = new Form2();
        form2.TextBox1.Text = ComboBox1.Text;
        form2.Show();

Now my question still remains: If i am in Form2 can i still get the value from form1? 现在我的问题仍然存在:如果我在Form2中,我是否仍然可以从Form1中获取值? If not, that is ok. 如果没有,那没关系。 I will post this as a solution. 我将其发布为解决方案。

While this is not the most proper answer, it is one way to solve the problem. 尽管这不是最正确的答案,但这是解决问题的一种方法。

Form1 Form1中

Add a method to get value 添加获取价值的方法

  public string TransmitSelectedValue()
  {
    return ComboBox1.SelectedItem.ToString();
  }

Form2 窗体2

 var myvalue = ((Form1)ParentForm.Controls.Find(Form1Name,true)).TransmitSelectedValue();

This type of question has been asked and answered many times, and in different versions. 这种类型的问题已被问过很多次,并以不同的版本回答。

I would suggest looking at a few of the following I have posted in the past... 我建议您看一下我过去发布的以下内容...

This example shows two forms where second form is passed as a parameter the first form's instance. 此示例显示了两种形式,其中第二种形式作为参数传递给第一种形式的实例。 Then, from public methods exposed on the first, the second can call them to get the values. 然后,从第一个公开的公共方法中,第二个可以调用它们以获取值。 It is your discretion on if you want to allow setting from alternate source, or just allowing a get method... could be done as a property public get; 由您决定是否允许从备用源进行设置,还是只允许使用get方法...可以作为属性公共获取来完成; protected set; 保护集;

This stackoverflow search will show several links to posts I've done in the past with slightly altering versions between different forms. 这个stackoverflow搜索将显示几个指向我过去所做的帖子的链接,不同形式之间的版本略有不同。

FEEDBACK FROM COMMENT 意见反馈

There has to be something done in your FIRST form to call the second.. is it from a click button, or based on the actual combobox selection being changed. 必须以FIRST形式完成操作才能调用第二个..是通过单击按钮,还是基于更改的实际组合框。 Whatever it is, the first Example I provided SHOULD be what you need. 无论是什么,我提供的第一个示例都应该是您所需要的。 You are not having the second form call the first. 您没有第二个表单调用第一个。

Without a full copy\\paste of the first example, all you would need to really do is in the form 2 constructor is set your text as pulled from the first... 如果没有第一个示例的完整复制\\粘贴,则您真正需要做的就是在表单2构造函数中将您的文本设置为从第一个示例拉出...

public Form2(Form1 viaParameters) : this()
{
    this.textBox1.Text = viaParameters.Combobox1.SelectedItem;
}

however, I don't know how your items are defined.. dictionary, list, array, whatever.. so you may need to typecast to get the selected item via 但是,我不知道您的项目是如何定义的..字典,列表,数组等等。因此您可能需要进行类型转换以通过

if( viaParameters.Combobox1.SelectedIndex > -1 )
  this.textBox1.Text = viaParameters.Combobox1.Items[ viaParameters.Combobox1.SelectedIndex ].WhateverStringValue;

This way, the start of form 2 from form 1 can grab the value directly. 这样,从表格1开始的表格2可以直接获取值。

If you expose the method from the first form via a property or method, your text value could be as simple as 如果通过属性或方法从第一种形式公开该方法,则文本值可能会很简单

this.textBox1.Text = viaParameters.YourForm1sMethodToGetStringFromCombobox();

i am not sure where the problem is 我不确定问题出在哪里

while starting/opening form2 在启动/打开form2时

like 喜欢

        Form2 f2 = new Form2();
        f2.Show(this);

you have a reference to form1 as 'owner' 您引用form1作为“所有者”

on form2 you can just put this on any event you want or on a button or whatever 在form2上,您可以将其放在您想要的任何事件上或按钮上

        Form1 f1 = Owner as Form1;
        textBox1.Text = f1.comboBox1.SelectedItem.ToString();

converted to C# ... 转换为C#...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM