列表框项目选择并打开form2文本框进行编辑 [英] listbox item select and open form2 textbox for editing

查看:111
本文介绍了列表框项目选择并打开form2文本框进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





form1有listbox1,form2有textbox1用于将项目添加到form1列表框



例如form2 textbox1.text = sometext添加到form1 listbox1。



如果我点击编辑按钮列表框选择项目,它应该是打开form2 with textbox.text = sometext content编辑



帮我代码。



rgds

Cad





form1



form1 has listbox1 and form2 has textbox1 for adding item to form1 listbox

for eg form2 textbox1.text = sometext added to form1 listbox1.

if i click the edit button listbox selected item its should be open form2 with textbox.text = sometext content for editing

help me the code.

rgds
Cad


form1

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}

public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}



form2




form2

public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}





i使用上面的代码但是当我选择并单击编辑按钮时,form2用文本打开但是在更改文本后单击确定然后它没有更新,意味着添加了新项目。

这里是我遇到的问题。解决这个问题。



i use above code but when i select and click edit button ,form2 open with text but after change text and click ok then its not updated ,means added new item.
where is my stuck problem.solve this.

推荐答案

你已经有了将文本从form1传递给form2的功能,但你还需要一些东西将form2中的修改信息传递回form1,例如in form1
You've got the function to pass the text from form1 to form2 but you also need something to pass the amended information from form2 back to form1 e.g. in form1
public void SetNewListBoxValue(string newValue)
{
    listBox1.Items.Add(newValue);
}



和表格2


and in form2

private void okButton_Click(object sender, EventArgs e)
{
    f1.SetNewListBoxValue(textBox1.Text);
}


这篇关于列表框项目选择并打开form2文本框进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆