我在子窗体中实现添加了父窗体的treeview中树的新值,现在我想就是在子窗体关闭的时候自动刷新父窗体的treeview,让他的显示中增加了我刚才添加的内容
请问该如何编写?(用c#语言)

解决方案 »

  1.   

    首先要获取父窗体的对象,设置你这个treeview为public ,然后就可以通过,父窗体.treeview1来访问到你这个treeview控件,随便你想做什么操作都可以了。
    获取出窗体对象方法还是蛮多的。简单的就是创建一个静态的对象,当父窗体创见的时候把自身赋值给那个静态对象。通过静态的对象就可以了。
      

  2.   

    在父窗体的定义中加一个public static Form1 f=null;
    构造函数中加一个f=this;
    在子窗体中就可以通过Form1.f来引用父窗体了。
      

  3.   


    构造函数中加一个f=this; 
    还得通过父类的构造函数来传递到子类吧?
      

  4.   

    BTW:通过构造函数传递,指的是子窗体的构造函数。父窗体在创建子窗体的时候,将this传递进去。当然,这种方法需要改写子窗体类的构造函数,使之能够接受参数。
      

  5.   

    配置XML文件~ 在FORMCOLSE事件中添加更新
      

  6.   

    我这样写对不?在父窗口 public static xm_tree treeview1 = null;  //xm_tree是父类名
            public xm_tree()
            {
                
                InitializeComponent();
                treeview1 = this;
            }
    子窗口的private void button1_Click(object sender, EventArgs e)
    {
            //一些添加父窗口中treeview中值的操作
           xm_tree.treeview1.Refresh();  //刷新
          
    }
      

  7.   

    public static xm_tree treeview1 = null;  //把窗体变量命名为treeview1?
    xm_tree.treeview1.Refresh();  //这个窗体类必须得有Refresh()方法。
      

  8.   

    哦  我把 名字treeview1  改掉了    
    xm_tree.treeview1.Refresh(); 能够使用啊   
      不过我那treeview还是没有刷新
      

  9.   

    在子窗体调用父窗体功能,最好是用委托
    在父窗体定义一个刷新函数
    private void _Refresh()
    {
       xm_tree.treeview1.Refresh();
    }调用子窗体的时候绑定委托 
    SunForm sunform = new SunForm();
    sunform.FatherRefresh = this._Refresh;在子窗体定义委托
    public delegate void DoAction();//如果有参数的话,定义参数public DoAction FatherRefresh;然后在子窗体关闭的时候调用
    privte void Form_Closing()
    {
       //do something .......
       FatherRefresh();
    }
      

  10.   

    这是 xm_tree  父窗体using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace cwgl.yjxm
    {
        public partial class xm_tree : Form
        {
            public static xm_tree tr = null;
            public xm_tree()
            {
                
                InitializeComponent();
                tr = this;
            }

            public void xm_tree_Load(object sender, EventArgs e)
            {
                GetScoule();
            }
            public void GetScoule()
            {
                SqlConnection con = new SqlConnection(global::cwgl.Properties.Settings.Default.cwglCon);
                con.Open();
                SqlCommand com = new SqlCommand("select * from v_xmsz order by 项目类型,分类,项目名称", con);
                SqlDataReader dr = com.ExecuteReader();            TreeNode NodeClassInfo = treeView1.Nodes.Add("A", "财政项目", 0, 1);
                   //省略   treeview的显示
                dr.Close();
                con.Close();
            }
               
            public void GetText(string strID)
            {
                
                SqlConnection conn = new SqlConnection(global::cwgl.Properties.Settings.Default.cwglCon);
                conn.Open();
                SqlCommand com = new SqlCommand("select * from v_xmsz where 项目名称 = '"+strID+"'",conn);
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    this.textBox1.Text = Convert.ToString(dr[2]);
                    this.textBox2.Text = dr[1].ToString();
                    this.textBox3.Text = dr[25].ToString();
                    this.textBox4.Text = dr[3].ToString();                this.textBox5.Text = dr[4].ToString();                this.textBox6.Text = dr[14].ToString();                this.textBox7.Text = dr[21].ToString();                this.textBox8.Text = dr[12].ToString();
                }
                 dr.Close();
                conn.Close();
                
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                this.toolStripStatusLabel1.Text = "系统时间:" + DateTime.Now.ToLongTimeString();
            }        private void button4_Click(object sender, EventArgs e)
            {
                this.Close();
            }        
            private void button1_Click(object sender, EventArgs e)
            {
                if (treeView1.SelectedNode.Text == "财政项目" )
                {
                    string strpp = "";
                    string strp = "";
                    string str = treeView1.SelectedNode.Text;
                    tjxm tj = new tjxm(str, strp, strpp);
                    tj.ShowDialog();
                }
                else if (treeView1.SelectedNode.Parent.Text == "财政项目")
                {
                    string strpp = "";
                    string strp = treeView1.SelectedNode.Parent.Text;
                    string str = treeView1.SelectedNode.Text;
                    tjxm tj = new tjxm(str, strp, strpp);
                    tj.ShowDialog();
                }            
                else if (treeView1.SelectedNode.Parent.Parent.Text != null)
                {
                    string strpp = treeView1.SelectedNode.Parent.Parent.Text;
                    string strp = treeView1.SelectedNode.Parent.Text;
                    string str = treeView1.SelectedNode.Text;
                    tjxm tj = new tjxm(str, strp, strpp);
                    tj.ShowDialog();
                }        }        private void button2_Click(object sender, EventArgs e)
            {
                upform up = new upform(this.textBox1.Text,this.label2.Text);
                up.ShowDialog();
              
            }        public void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                GetText(treeView1.SelectedNode.Text);
                
            }
    }
    }
    这是tjxm  子窗体using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using cwgl.yjxm;
    namespace cwgl.yjxm
    {
        public partial class tjxm : Form
        {
            public string str;
            public string strp;
            public string strpp;
            public tjxm(string str,string strp,string strpp)
            {
                InitializeComponent();
                
                this.str = str;
                this.strp = strp;
                this.strpp = strpp;
                
            }        private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
               
            }
            SqlConnection con = new SqlConnection(global::cwgl.Properties.Settings.Default.cwglCon);
            private void button1_Click(object sender, EventArgs e)
            {
                con.Open();
                string st1 = this.textBox1.Text;
                string st2 = this.textBox2.Text;
                if (this.textBox2.Text == "")
                {
                    MessageBox.Show("请填写项目名称","错误");
                    return;
                }
                SqlCommand com00 = new SqlCommand("select * from v_xmsz", con);
                SqlDataReader dr = com00.ExecuteReader();
                while (dr.Read())
                {
                    if (dr[2].ToString() == st2)
                    {
                        MessageBox.Show("请勿添加相同的项目名字(如果为填入,请正确填写)", "错误 ");
                        this.textBox1.Text = "";
                        this.textBox2.Text = "";
                        this.Close();                }
                }
                dr.Close();
                if (this.textBox2.Text != "")
                {
                    switch (str)
                    {
                       //省略  实现treeview中的内容添加               
                   this.Close();
                }
                xm_tree.tr.Refresh();
              con.Close();
             
            }
           private void tjxm_Load(object sender, EventArgs e)
            {
               
            }
        }
    }
      

  11.   

    让你贴两处,贴这么一大片,看的眼晕...你在父窗体的定义中加一个方法:public void TV_Refresh()
    {
       treeView1.Refresh();
    }把你程序中的:xm_tree.tr.Refresh();改成:xm_tree.tr.TV_Refresh();
      

  12.   

    将近绝望了  还是没有刷新treeview  还得我关了父窗体再打开~
      

  13.   

    你在ShowDialog之后,直接treeView1.Refresh()不行么?
      

  14.   

    我用了断电测试   确实用到了 treeview1.Refresh()  不过就是没反应
      

  15.   

    子窗体中如何操纵父窗体中的TreeView?
      

  16.   

    我一直在想 refresh是不是没起到作用?
      

  17.   

    C#不会用大意是
    tv = ctype(child.mdiParent,MainForm).Treeview1
      

  18.   

      C#  是这样 reflash窗体的嘛???  不是还有另一个关键字吗???
      好像是重画界面的什么... I  开头的..其他的忘记了...
      

  19.   


      reflash()方法好像是 JAVA的..刷新...  ...嘿嘿..
      

  20.   

    你的添加信息要刷新refresh是没用的,如果信息进数据库,必须在子窗体关闭的时候,把数据冲数据库中取出。
    这样才会刷新的。
      

  21.   

    父窗体
    FrmXiangQing XQ = new FrmXiangQing();
    XQ.Owner = this;子窗体中
    this.Owner="";
      

  22.   

    这里的两个窗体 当然是指同一个类 不是子窗体.是子窗体中的父窗体类.就是父窗体在构造子窗体时.把this传给他.
      

  23.   

    找到一块以前写的代码..希望对你有所帮助主窗体Form2 frm = new Form2();
    frm.ShowDialog(this);
    子窗体((Form1)this.Owner).TextBox1.Text = "这将显视在主窗体";
      

  24.   

    尽量别用静态变量
    可以用属性,在子窗体中定义一个类型为treeview的属性,在父窗体实例子窗体时将父窗体中的treeview1传过去。
    或者在子窗体中定义一个类型为父窗体类的属性,在父窗体实例子窗体时将父窗体自己传过去,也就时xx=this;。
      

  25.   

    使用委托,
        
    public static class UserInfo
        {
            public static List<Users> UserList = new List<Users>();
        }using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;namespace TestDelegationApplication
    {
        public partial class UserManagerForm : Form
        {
            public UserManagerForm()
            {
                InitializeComponent();
            }        private void btnExitApplication_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        private void btnAddUser_Click(object sender, EventArgs e)
            {
                AddOrEditForm AddOrEdit = new AddOrEditForm();
                AddOrEdit.OperationType = 0;
                AddOrEdit.RefreshUserList = this.RefreshList;
                AddOrEdit.ShowDialog();
            }        public void RefreshList()
            {
                listUserManager.Items.Clear();
                for (int i = 0; i < UserInfo.UserList.Count; i++)
                {
                    ListViewItem listItem = new ListViewItem(new string[] { UserInfo.UserList[i].Guid, UserInfo.UserList[i].UserName, UserInfo.UserList[i].Sex, UserInfo.UserList[i].Age, UserInfo.UserList[i].CreateDate });
                    listUserManager.Items.Add(listItem);
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace TestDelegationApplication
    {
        public partial class AddOrEditForm : Form
        {
            public AddOrEditForm()
            {
                InitializeComponent();
            }        public int OperationType; //操作类别、        /// <summary>
            /// 添加用户委托
            /// </summary>
            public delegate void DoAction();
            public DoAction RefreshUserList;        private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
            }        public void BindAge()
            {
                for (int i = 1; i <= 100; i++)
                {
                    cboAge.Items.Add(i);
                }
            }        private void AddOrEditForm_Load(object sender, EventArgs e)
            {
                BindAge();
                BindText();
            }        void BindText()
            {
                if (OperationType == 0)
                {
                    this.Text = "添加用户";
                    this.grpAddOrEdit.Text = "添加用户";
                    this.btnAddOrEdit.Text = "添加用户";
                }
            }        private void btnAddOrEdit_Click(object sender, EventArgs e)
            {
                string btnText=btnAddOrEdit.Text;
                switch (btnText)
                { 
                    case "添加用户":
                        Users u = new Users();
                        u.Guid = System.Guid.NewGuid().ToString();
                        u.UserName = txtUserName.Text;
                        u.Sex = cboSex.Text;
                        u.Age = cboAge.Text;
                        u.CreateDate = DateTime.Now.ToString();
                        UserInfo.UserList.Add(u);
                        RefreshUserList();
                        break;
                    default:
                        break;
                }
            }
        }
    }