以单独的形式在文本框中显示选定的列表框项目?
Displaying selected listbox item in textbox on seperate form?
我试图在列表框中选择一个项目,并以不同的形式将其显示在文本框中。我希望当我单击 MainForm 上的按钮时它会触发,以便所选项目显示在 textBox
在我的 MainForm 中我有:
public static string myListBoxString;
private void lstDictionary_SelectedIndexChanged(object sender, EventArgs e)
{
myListBoxString = lstDictionary.SelectedItem.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
EditWordForm myEditWordForm = new EditWordForm();
myEditWordForm.SelectedValue = myListBoxString;
myEditWordForm.ShowDialog();
this.dictionaryTableAdapter.Fill(this.dictionaryDataSet.Dictionary);
}
在我尝试放置所选单词的第二种形式中:
public partial class EditWordForm : Form
{
public EditWordForm()
{
InitializeComponent();
wordTextBox.Text = MainForm.myListBoxString;
}
public string SelectedValue
{
set
{
wordTextBox.Text = value;
}
}
}
编辑*
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(ConnectionString)
{
connection.Open();
SqlCommand cmd = new SqlCommand("update dictionary set word=@word", connection);
cmd.Parameters.AddWithValue("@word", wordTextBox.Text);
cmd.ExecuteNonQuery();
this.Close();
}
}
现在我可以编辑单词了,我想将它保存到数据库中。目前,当它保存时,它会将所有 10 个单词替换为新单词,而不是一个特定条目。
从我阅读和查找的内容来看,这段代码似乎应该可以工作,但到目前为止,它只是在 运行 应用程序之后的文本框中显示一个空字段。我唯一能想到的是,我正在使用一个数据集来填充初始列表框,这会把它扔掉吗?任何指导表示赞赏!
在您的 EditWordForm 中添加一个名为 SelectedValue
的 属性
public string SelectedValue
{
set
{
wordTextBox.Text = value;
}
}
在您的主表单中,在显示表单之前设置此 属性。
private void btnEdit_Click(object sender, EventArgs e)
{
EditWordForm myEditWordForm = new EditWordForm();
myEditForm.SelectedValue = lstDictionary.GetItemText(lstDictionary.SelectedItem);
myEditWordForm.ShowDialog();
this.dictionaryTableAdapter.Fill(this.dictionaryDataSet.Dictionary);
}
我试图在列表框中选择一个项目,并以不同的形式将其显示在文本框中。我希望当我单击 MainForm 上的按钮时它会触发,以便所选项目显示在 textBox
在我的 MainForm 中我有:
public static string myListBoxString;
private void lstDictionary_SelectedIndexChanged(object sender, EventArgs e)
{
myListBoxString = lstDictionary.SelectedItem.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
EditWordForm myEditWordForm = new EditWordForm();
myEditWordForm.SelectedValue = myListBoxString;
myEditWordForm.ShowDialog();
this.dictionaryTableAdapter.Fill(this.dictionaryDataSet.Dictionary);
}
在我尝试放置所选单词的第二种形式中:
public partial class EditWordForm : Form
{
public EditWordForm()
{
InitializeComponent();
wordTextBox.Text = MainForm.myListBoxString;
}
public string SelectedValue
{
set
{
wordTextBox.Text = value;
}
}
}
编辑*
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(ConnectionString)
{
connection.Open();
SqlCommand cmd = new SqlCommand("update dictionary set word=@word", connection);
cmd.Parameters.AddWithValue("@word", wordTextBox.Text);
cmd.ExecuteNonQuery();
this.Close();
}
}
现在我可以编辑单词了,我想将它保存到数据库中。目前,当它保存时,它会将所有 10 个单词替换为新单词,而不是一个特定条目。 从我阅读和查找的内容来看,这段代码似乎应该可以工作,但到目前为止,它只是在 运行 应用程序之后的文本框中显示一个空字段。我唯一能想到的是,我正在使用一个数据集来填充初始列表框,这会把它扔掉吗?任何指导表示赞赏!
在您的 EditWordForm 中添加一个名为 SelectedValue
的 属性public string SelectedValue
{
set
{
wordTextBox.Text = value;
}
}
在您的主表单中,在显示表单之前设置此 属性。
private void btnEdit_Click(object sender, EventArgs e)
{
EditWordForm myEditWordForm = new EditWordForm();
myEditForm.SelectedValue = lstDictionary.GetItemText(lstDictionary.SelectedItem);
myEditWordForm.ShowDialog();
this.dictionaryTableAdapter.Fill(this.dictionaryDataSet.Dictionary);
}