首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 窗体如何与用户控件交互
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xl_0715
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 揭贴率:
    发表于:2008-08-19 22:35:15 楼主
    WinForm中
    拿了个新窗体,在项目中加了个用户控件,功能很简单(一个ComBox,一个DataGridView用来进行下省市之间的连动)
    在新窗体一个TextBox ,点一下这个文本框,显示用户控件,在ComBox里是省,选择之后,DataGridView里所属市就自动列出了,
    这时,我想通过双击DataGridView里的某个市区,直接将该值写到窗体的TextBox里面去...同时这个用户控件隐藏

    请高人赐教
    60  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • billlyh
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-20 08:14:441楼 得分:0
    ding!!!!!!!!!!!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • greystar
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-20 08:16:462楼 得分:0
    你可自定义事件,或者将你的ComBox,DataGridView在用户控件里中暴露接口也可.这样就你可以操作了.
    方式有很多种.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • LQknife
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-20 08:35:193楼 得分:0
    用一个公共变量存值然后 赋给textbox :-)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • slimfeng
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-20 09:08:484楼 得分:0
    添加新控件的事件处理即可
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • pp_shy
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-20 10:16:125楼 得分:0
    在窗体的Form_Load事件中定义用户控件中的DataGridView双击事件
    C# code
    private void Form1_Load(object sender, EventArgs e) { foreach (Control control in this.userControl11.Controls) { if (control is DataGridView) { DataGridView dgv = (DataGridView)control; dgv.CellContentDoubleClick += new DataGridViewCellEventHandler(dgv_CellContentDoubleClick); } } } void dgv_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { //这样双击事件对外提供了,处理内容略 }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xl_0715
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-21 11:00:276楼 得分:0
    谢谢楼上各位
    写 写 去
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zhzuo
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 20:57:547楼 得分:0
    这个可以参考DataGrid的处理。
    C# code
    //在另一个窗口中修改当前选定的DataGrid的行 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; //主窗体代码 namespace Zhzuo { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private DataSet ds; private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); ds = new DataSet("MyDataSet"); InitData(ds); this.dataGrid1.DataSource = this.ds; this.dataGrid1.DataMember = this.ds.Tables[0].TableName; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(16, 20); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(340, 160); this.dataGrid1.TabIndex = 0; this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick); // // button1 // this.button1.Location = new System.Drawing.Point(264, 196); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "修改"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(372, 229); this.Controls.Add(this.button1); this.Controls.Add(this.dataGrid1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion private void InitData(DataSet ds) { DataTable dt = new DataTable("TabeHost"); DataColumn dc = new DataColumn("IpAddress",typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("UserName",typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("Password",typeof(string)); dt.Columns.Add(dc); ds.Tables.Add(dt); DataRow dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.229"; dr["UserName"] = "zhzuo"; dr["Password"] = "zhengzuo"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.230"; dr["UserName"] = "11"; dr["Password"] = "12"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.231"; dr["UserName"] = "123"; dr["Password"] = "12"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.232"; dr["UserName"] = "22"; dr["Password"] = "788"; dt.Rows.Add(dr); } private void button1_Click(object sender, System.EventArgs e) { DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current; Form2 form2 = new Form2(drv); form2.ShowDialog(); } private void dataGrid1_DoubleClick(object sender, System.EventArgs e) { button1_Click(button1,EventArgs.Empty); } } } //子窗体代码 ======================= using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Zhzuo { /// <summary> /// Form2 的摘要说明。 /// </summary> public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private DataRowView drv; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form2(DataRowView dr) { InitializeComponent(); this.drv = dr; this.textBox1.Text = (string)drv["IpAddress"]; this.textBox2.Text = (string)drv["UserName"]; this.textBox3.Text = (string)drv["Password"]; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(28, 16); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 3; this.textBox1.Text = "textBox1"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(28, 56); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 4; this.textBox2.Text = "textBox2"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(28, 92); this.textBox3.Name = "textBox3"; this.textBox3.TabIndex = 5; this.textBox3.Text = "textBox3"; // // button1 // this.button1.Location = new System.Drawing.Point(152, 152); this.button1.Name = "button1"; this.button1.TabIndex = 6; this.button1.Text = "保存"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(236, 152); this.button2.Name = "button2"; this.button2.TabIndex = 7; this.button2.Text = "退出"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(324, 201); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } #endregion private void button1_Click(object sender, System.EventArgs e) { drv["IpAddress"] = this.textBox1.Text; drv["UserName"] = this.textBox2.Text; drv["Password"] = this.textBox3.Text; } private void button2_Click(object sender, System.EventArgs e) { this.Close(); } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zhzuo
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 20:58:448楼 得分:0
    另外,关注数据的交换,可以参考
    Windows窗体间的数据交互
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    谈基于.net平台windows开发中的模式窗体
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xl_0715
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-26 22:11:599楼 得分:0
    谢谢楼上的代码

    因为我用的是用户控件
    我是在用户控件上添加了几个属性,并且在用户窗体上来进行主窗体的值的改变
    做法是 在主窗体的textbox的双击事件中将控件名传到用户控件,然后在用户控件中遍历父窗体 ...
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • liuhz_jsmstc
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-27 15:18:5610楼 得分:0
    C# code
    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 WindowsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { SqlConnection sqlcon = Connection(); sqlcon.Open(); this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; string strCmd = "SELECT DISTINCT PROVINCE FROM PROVINCECITYTAB "; SqlDataAdapter sqladp = new SqlDataAdapter(strCmd, sqlcon); DataSet ds = new DataSet(); sqladp.Fill(ds, "provinceTab"); for (int i = 0; i < ds.Tables["provinceTab"].Rows.Count; i++) { string province = ds.Tables["provinceTab"].Rows[i]["province"].ToString(); this.comboBox1.Items.Add(province); } sqlcon.Close(); } private SqlConnection Connection() { string strCon = "data source=MANNA;initial catalog=manna;integrated security=true"; SqlConnection sqlcon = new SqlConnection(strCon ); return sqlcon; } private void BindDataGridView() { SqlConnection sqlcon = Connection(); sqlcon.Open(); String strProvince=this.comboBox1 .Text ; string str = "SELECT CITY FROM PROVINCECITYTAB WHERE PROVINCE='" + strProvince + "'"; SqlDataAdapter sqladp = new SqlDataAdapter(str, sqlcon); DataSet ds = new DataSet(); sqladp.Fill(ds, "cityTab"); this.dataGridView1.DataSource = ds.Tables["cityTab"]; sqlcon.Close(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { BindDataGridView(); } private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { SqlConnection sqlcon = Connection(); sqlcon.Open(); String strProvince = this.comboBox1.Text; string str = "SELECT CITY FROM PROVINCECITYTAB WHERE PROVINCE='" + strProvince + "'"; SqlDataAdapter sqladp = new SqlDataAdapter(str, sqlcon); DataSet ds = new DataSet(); sqladp.Fill(ds, "cityTab"); this.dataGridView1.DataSource = ds.Tables["cityTab"]; sqlcon.Close(); int i = e.RowIndex; int j = e.ColumnIndex; string city = ds.Tables["cityTab"].Rows[i][j].ToString(); this.textBox1.Text = city; } } }

    贴出来仅供参考!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zhzuo
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-27 21:25:0911楼 得分:0
    采用用户控件,和交互数据可以进一步参考这里,
    Windows窗体间的数据交互
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    谈基于.net平台windows开发中的模式窗体
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
    在.net应用程序中使用用户控件
    http://blog.csdn.net/zhzuo/archive/2004/11/30/199599.aspx
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved