2013-11-02 91 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; 
相關問題