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

3 回答 3

0

我假设您已经有一个实例MainForm,这就是创建AnotherForm.

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

您在文本框中看不到任何内容的原因是因为您正在查看 的原始实例MainForm,而您实际上并未对其进行更改。

解决此问题的一种快速方法是将原始引用传递MainFormAnotherForm

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意识到,如下所示:MainFormAnotherForm

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"
}
于 2013-11-02T01:15:53.623 回答
0

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

听起来您想要做的是模态对话框模式。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. 您应该始终将要共享的值公开为公共属性。

于 2013-11-02T01:16:36.250 回答
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

这是代码:

主窗体

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

另一种形式

 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;
于 2013-11-02T02:30:46.347 回答