publicpartial class Form2 : Form
{
private string text;
private ListBox lb;
private int index;
//构造函数接收三个参数:选中行文本,ListBox控件,选中行索引
public Form2(string text,ListBox lb,int index)
{
this.text = text;
this.lb = lb;
this.index = index;
InitializeComponent();
this.textBox1.Text = text;
}
private void btnChange_Click(object sender, EventArgs e)
{
string text = this.textBox1.Text;
this.lb.Items.RemoveAt(index);
this.lb.Items.Insert(index, text);
this.Close();
}
}
public partial class Form1 :Form
{
int index = 0;
string text = null;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgse)
{
if (this.listBox1.SelectedItem != null)
{
text = this.listBox1.SelectedItem.ToString();
index = this.listBox1.SelectedIndex;
//构造Form2同时传递参数
Form2 form2 = new Form2(text, listBox1, index);
form2.ShowDialog();
}
}
}
//声明Form2继承于Form1
public partial classForm2 : Form1
{
publicint index;
public ListBox lb;
public Form2(string text)
{
//将继承过来的listBox设置为不可见
this.listBox1.Visible=false;
InitializeComponent();
this.textBox1.Text = text;
}
private void btnChange_Click(object sender, EventArgs e)
{
string text = this.textBox1.Text;
this.lb.Items.RemoveAt(index);
this.lb.Items.Insert(index,text);
this.Close();
}
}
public partial class Form1 :Form
{
public int index = 0;
public string text = null;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgse)
{
if (this.listBox1.SelectedItem != null)
{
text = this.listBox1.SelectedItem.ToString();
index = this.listBox1.SelectedIndex;
Form2 form2 = new Form2(text);
//构造完Form2后,为Form2中各参数赋值
form2.lb =this.listBox1;
form2.index = index;
form2.Show();
}
}
}
this.lb=base.listBox1; this.index=base.index;
//定义一个需要string类型参数的委托
publicdelegate void MyDelegate(string text);
public partial class Form2 :Form1
{
//定义该委托的事件
public event MyDelegate MyEvent;
public Form2(string text)
{
InitializeComponent();
this.textBox1.Text = text;
}
private void btnChange_Click(object sender, EventArgs e)
{
//触发事件,并将修改后的文本回传
MyEvent(this.textBox1.Text);
this.Close();
}
}
public partial class Form1 :Form
{
public int index = 0;
public string text = null;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgse)
{
if (this.listBox1.SelectedItem != null)
{
text = this.listBox1.SelectedItem.ToString();
index = this.listBox1.SelectedIndex;
Form2 form2 = new Form2(text);
//注册form2_MyEvent方法的MyEvent事件
form2.MyEvent += new MyDelegate(form2_MyEvent);
form2.Show();
}
}
//处理
void form2_MyEvent(string text)
{
this.listBox1.Items.RemoveAt(index);
this.listBox1.Items.Insert(index, text);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有