2016-04-25 52 views
0

在形成一个I需要将listbox.SelectedIndex发送到第二形式:如何将变量从一个窗体传递给另一个窗体上的textBoxChanged事件处理程序?

private void btnEditWord_Click(object sender, EventArgs e) 
    { 
     Form editWord = new editWord(listBox.SelectedIndex); 


     editWord.ShowDialog(); 


    } 

第二形式:选定的索引变量不在当前上下文中存在。

public editWord(int value) 

    { 
     InitializeComponent(); 

     int selectedIndex = value; 

    } 

private void wordTextBox_TextChanged(object sender, EventArgs e) 
    { 

     string word = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Word"].ToString()); 

     wordTextBox.Text = word; 

    } 
+0

添加一个构造函数,其中您将在窗体之间注入共享值,或者将窗体视为对象,以便它们可以具有属性。所以写下你自己的财产,应该公开访问,你就完成了。 –

+0

只需在构造函数外部移动'int selectedIndex;',将其全局化为第二个形式,然后在构造函数'selectedIndex = value;' – Pikoh

回答

0

我评论过你的问题,而只是要清楚,你的第二个表单代码应该是这样的:

int selectedIndex=-1; 

public editWord(int value) 

{ 
    InitializeComponent(); 

    selectedIndex = value; 

} 

private void wordTextBox_TextChanged(object sender, EventArgs e) 
{ 

    string word = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Word"].ToString()); 

    wordTextBox.Text = word; 

} 

与您所提供的代码的问题是,selectedIndex范围只构造函数。

-1

只需移动int selectedIndex;在构造函数之外,使其成为全局的第二种形式,然后在构造函数中selectedIndex = value;

+0

如果您要将我的评论复制为答案,至少应移除我的尼克和时间部分:) – Pikoh

相关问题