当前位置:编程学习 > C#/ASP.NET >>

子窗体获取父窗体控件值后的问题

    在我的form1窗体里有一个listview用来显示我的查询结果,现在我需要选中一行结果,然后点击窗体上的按钮新建一个form2窗体,这个窗体用来修改刚才选中的那项结果,这个窗体有几个textbox,我现在完成了对这几个textbox的编写,数据库中的值也已经更新了,但是我想让我刚才选中的form1的listview的那一行也更新,请各位高手指教?小弟万分感谢! --------------------编程问答-------------------- form2中添加一个属性,用于保存修改后的结果。
form1中取这个属性的值,更改SelectedItems中的项。 --------------------编程问答-------------------- 帮顶。。不会。。 --------------------编程问答-------------------- 能不能说的具体点,我的代码是这样的。

Form1中:
private void button1_Click(object sender, EventArgs e)
        {
                    string ex1 = listView1.CheckedItems[0].Text;
                    string ex2 = listView1.CheckedItems[0].SubItems[1].Text;
                    string ex3 = listView1.CheckedItems[0].SubItems[2].Text;
                    Form2 frmEdit = new Form2(ex1,ex2,ex3);
                    frmEdit.ShowDialog(this);
         }
Form2中:
private void button1_Click(object sender, EventArgs e)
        {
                string strconnection1 = "Integrated Security = SSPI;Database = users;Server = (local)";
                SqlConnection connection1 = new SqlConnection(strconnection1);
                connection1.Open();
                SqlCommand cmd1 = connection1.CreateCommand();
                cmd1.CommandText = "INSERT INTO [Usermanagement] VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "');
                cmd1.Connection = connection1;
                cmd1.CommandType = CommandType.Text;
                int n = cmd1.ExecuteNonQuery();
                connection1.Close();
                this.Close();//关闭Form11 
         }
返回frmEdit.ShowDialog(this)的时候,数据库已经更新,但是listview里选中的checkbox那一项并没有更新……

--------------------编程问答-------------------- //在form1中
 private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.form1 = this;
            frm.Show();
        }
//form2中
public Form1 form1;
private void button1_Click(object sender, EventArgs e)
{
    form1.listView1.Items.Add(....);
}
http://topic.csdn.net/u/20100106/20/e9697297-75f9-4450-aaa9-da0da416cf41.html

--------------------编程问答-------------------- 谢谢。不过我想请教下有没有不用委托的方法,我的代码在3楼贴出来了,请高手看看能否在此基础上进行改进,小弟不胜感激!!! --------------------编程问答--------------------

public class Form2:Form
{
    ....

   public string Text1
   {//定义一个只读属性,返回textBox1的Text属性值,其他的TextBox可类似定义
     get
        {
           return textBox1.Text;
         }
    }
 }

public class Form1:Form
{
  ..
  private void button1_Click(object sender, EventArgs e)
  {
  string ex1 = listView1.CheckedItems[0].Text;
  string ex2 = listView1.CheckedItems[0].SubItems[1].Text;
  string ex3 = listView1.CheckedItems[0].SubItems[2].Text;
  Form2 frmEdit = new Form2(ex1,ex2,ex3);
  frmEdit.ShowDialog(this);
  listView1.CheckItems[0].Text=frmEdit.Text1;//取Form2中定义的属性
   ...
  }

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,