我在子窗体中实现添加了父窗体的treeview中树的新值,现在我想就是在子窗体关闭的时候自动刷新父窗体的treeview,让他的显示中增加了我刚才添加的内容
请问该如何编写?(用c#语言)
请问该如何编写?(用c#语言)
解决方案 »
- 如何给自定义控件添加DataSource等属性
- 不太写代码了,今天发现MaximumSize、MinimumSize属性有意思的问题。
- [求教]如何将一张表的内容加上变量插入另一表!
- 用serialPort控件使用com1口自发自接数据(50分)
- 问大一个问关于正则表达式的。
- 实时曲线显示问题??
- C# snmp编程 解析自定义的mib文件
- 表格中的数据倒出成EXCEL出错
- 请问windows程序设计中如何转换一个文件的相对路径为绝对路径!在线等...
- C#怎么控制输出数的宽度
- 如果實現點擊GirdView數據行上一任意一列,實現選中該行?
- 窗口透明 设置的问题! 新人求教!!~大侠们快来啊!
获取出窗体对象方法还是蛮多的。简单的就是创建一个静态的对象,当父窗体创见的时候把自身赋值给那个静态对象。通过静态的对象就可以了。
构造函数中加一个f=this;
在子窗体中就可以通过Form1.f来引用父窗体了。
构造函数中加一个f=this;
还得通过父类的构造函数来传递到子类吧?
public xm_tree()
{
InitializeComponent();
treeview1 = this;
}
子窗口的private void button1_Click(object sender, EventArgs e)
{
//一些添加父窗口中treeview中值的操作
xm_tree.treeview1.Refresh(); //刷新
}
xm_tree.treeview1.Refresh(); //这个窗体类必须得有Refresh()方法。
xm_tree.treeview1.Refresh(); 能够使用啊
不过我那treeview还是没有刷新
在父窗体定义一个刷新函数
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();
}
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)
{
}
}
}
{
treeView1.Refresh();
}把你程序中的:xm_tree.tr.Refresh();改成:xm_tree.tr.TV_Refresh();
tv = ctype(child.mdiParent,MainForm).Treeview1
好像是重画界面的什么... I 开头的..其他的忘记了...
reflash()方法好像是 JAVA的..刷新... ...嘿嘿..
这样才会刷新的。
FrmXiangQing XQ = new FrmXiangQing();
XQ.Owner = this;子窗体中
this.Owner="";
frm.ShowDialog(this);
子窗体((Form1)this.Owner).TextBox1.Text = "这将显视在主窗体";
可以用属性,在子窗体中定义一个类型为treeview的属性,在父窗体实例子窗体时将父窗体中的treeview1传过去。
或者在子窗体中定义一个类型为父窗体类的属性,在父窗体实例子窗体时将父窗体自己传过去,也就时xx=this;。
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;
}
}
}
}