2013-11-02 129 views
-1

我有一个MainForm和AnotherForm。 AnotherForm通过MainForm的menuItem访问。如何将值从一种形式传递给另一种?

AnotherForm有listView。当用户点击一个项目时,我想获取字符串元素并将其传递给MainForm的文本框,因此该元素在那里显示并且AnotherForm已关闭。到目前为止,AnotherForm关闭,但没有在MainForm的文本框中显示。有什么建议么?

private void listView1_ItemActivate(object sender, EventArgs e) 
{ 
    string input = listView1.SelectedItem[0].ToString(); 
    MainForm mainF = new MainForm(input);// called the constructor 
    this.Close(); //close this form and pass the input to MainForm 
    mainF.inputTextBox.Text = input; 
    mainF.loadThis(input); 
} 

回答

0

我假设你有MainForm一个实例已,这就是创造的AnotherForm一个实例。

在发布的事件中,您实际上创建了一个全新的MainForm实例,从不显示它,然后当AnotherForm关闭时它就会被销毁。

您在文本框中什么也看不到的原因是因为您正在查看MainForm的原始实例,而您实际上并未更改该实例。解决这个的

一个匆匆的方式将被传递给原来MainForm引用到AnotherForm

public class AnotherForm 
{ 
    private MainForm mainF; 

    public AnotherForm(MainForm mainF) 
    { 
     this.mainF = mainF; 
    } 

    ... 
    ... 

    private void listView1_ItemActivate(object sender, EventArgs e) 
    { 
     ... 
     mainF.inputTextBox.Text = input; 
     ... 
    } 
} 

注意:不能有AnotherForm意识到MainForm的,你可能要绕切换后,创建一个公共财产AnotherForm这样的:

public class AnotherForm 
{ 
    public InputValue { get; private set; } 

    private void listView1_ItemActivate(object sender, EventArgs e) 
    { 
     ... 
     InputValue = input; 
     ... 
    } 
} 

然后你就可以从MainForm访问时,其他形式的关闭:

private void SomeMethodInMainForm() 
{ 
    var newAnotherForm = new AnotherForm(); 

    newAnotherForm.ShowDialog(); 

    var inputValueFromAnotherForm = newAnotherForm.InputValue; 

    // do something with the input value from "AnotherForm" 
} 
0

如果您MainForm已经创建,你不能只是建立在以访问和设置属性另一个。你已经创建了两个独立的MainForm(尽管第二个隐藏,因为你从未显示过)。

这听起来像你想要做的是模态对话模式。您的MainForm是您应用程序中的主窗口。当你点击一个菜单链接时,你想要弹出第二个窗体。这被称为对话。然后当你关闭那个对话框时,你想让你的MainForm检索一个值作为对话框的返回结果。

在你的MainForm的事件,其处理该菜单项点击应该是这个样子:

private void pickSomethingMenuItem_Click(object sender, EventArgs e) 
{ 
    using (var picker = new PickerDialog()) 
    { 
     if (picker.ShowDialog(this) == DialogResult.OK) 
     { 
      LoadSomething(picker.SomethingPicked); 
     } 
    } 
} 

然后将下面的代码将是你的对话形式中:

public string SomethingPicked { get; private set; } 

private void somethingListView_ItemActivate(object sender, EventArgs e) 
{ 
    SomethingPicked = somethingListView.SelectedItem[0].ToString(); 
    DialogResult = DialogResult.OK; 
} 

通知我如何用有意义的名字命名所有对象。那么,除了“某些东西”。从代码中不可能告诉你实际使用对话框选择的内容。您应该使用总是为您的对象和变量使用有意义的名称。你的代码几乎完全没有意义。

而且你几乎从来不会像在你的inputTextBox那样对表格公众进行控制。您应该始终将想要共享的值公开为公共属性。

0

在此提出的解决方案,你可以做五个主要的事情,以达到你想要做什么,即:

1) Declare a global object for AnotherForm in MainForm 
2) Initiate a FromClosing event handler for AnotherForm in MainForm 
3) Make a public property or field in AnotherForm 
4) Before closing in AnotherForm you save it the public property mentioned above 
5) In the MainForm get the public property from AnotherForm 

下面是代码:

的MainForm

public partial class MainForm : Form 
{ 
    AnotherForm anotherForm; // Declare a global object for AnotherForm 

    public MainForm() 
    { 
     InitializeComponent(); 
    } 

    private void showToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     anotherForm = new AnotherForm(); // when Menu Item is clicked instantiate the Form 
     anotherForm.FormClosing += new FormClosingEventHandler(anotherForm_FormClosing); // Add a FormClosing event Handler 
     anotherForm.ShowDialog(); 
    } 

    void anotherForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     inputTextBox.Text = anotherForm.listViewValue; // get the Value from public property in AnotherForm 
    } 
} 

AnotherForm

void listView1_ItemActivate(object sender, EventArgs e) 
{ 
    listViewValue = listView1.SelectedItems[0].Text; // Get the listViewItem value and save to public property 
    this.Close(); // Close 
} 

public String listViewValue { get; set; } // public property to store the ListView value 

这里有一点要注意,比较你的代码我没有使用ToString()ListView.SelectedItems

listView1.SelectedItems[0].ToString(); 

,而是使用了Text物业:

listView1.SelectedItems[0].Text; 
相关问题