如何将变量从一个表单传递到另一个表单上的textBoxChanged事件处理程序?

时间:2016-04-25 15:38:35

标签: c# visual-studio-2012

在Form 1上我需要将listbox.SelectedIndex发送到第二个Form:

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;

    }

2 个答案:

答案 0 :(得分: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 :(得分:-1)

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

相关问题