| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1095 人关注过本帖
标题:两个窗体,如何做到对其中一个的操作影到到另一个??
只看楼主 加入收藏
athenalux
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河北石家庄
等 级:小飞侠
威 望:8
帖 子:975
专家分:2514
注 册:2008-11-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
两个窗体,如何做到对其中一个的操作影到到另一个??
如下:class form1{}和class form2{},其中form1中有一个button控件与一个textbox控件,我想要做到点击这个BUTTON时,form2中的某个变量能自动赋值为textbox的值
搜索更多相关主题的帖子: 窗体 
2009-09-22 08:24
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:5 
Form1:
程序代码:
using System; 
using System.Collections.Generic; 
using  
using System.Windows.Forms; 
 
namespace FormTest 
{ 
    public partial class Form1 : Form 
    { 
        private bool hasOpenForm = false; 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            if (!this.hasOpenForm) 
            { 
                this.hasOpenForm = true; 
                Form2 form = new Form2(); 
                form.StartPosition = FormStartPosition.CenterScreen; 
                form.OnTransferText += new EventHandler<Form2.TransferTestEventArgs>(form_OnTransferText); 
                form.FormClosed += new FormClosedEventHandler(form_FormClosed); 
                form.Show(); 
            } 
        } 
 
        void form_FormClosed(object sender, FormClosedEventArgs e) 
        { 
            this.hasOpenForm = false; 
        } 
 
        void form_OnTransferText(object sender, Form2.TransferTestEventArgs e) 
        { 
            this.textBox1.Text = e._Text; 
        } 
    } 
} 

Form2:
程序代码:
using System; 
using System.Collections.Generic; 
using  
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
 
namespace FormTest 
{ 
    public partial class Form2 : Form 
    { 
        public event EventHandler<TransferTestEventArgs> OnTransferText; 
 
        public Form2() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form2_Load(object sender, EventArgs e) 
        { 
            if (sender != null) 
            { 
                OnTransferText(this, new TransferTestEventArgs(this.textBox1.Text)); 
            } 
        } 
 
        public class TransferTestEventArgs : EventArgs 
        { 
            public readonly string _Text; 
 
            public TransferTestEventArgs(string Text) 
            { 
                _Text = Text; 
            } 
        } 
    } 
} 

2009-09-22 08:59
jiaguofu2005
Rank: 2
等 级:论坛游民
帖 子:47
专家分:59
注 册:2009-9-2
收藏
得分:5 
form2:
 using System;  
using System.Collections.Generic;  
using   
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
 
namespace FormTest  
{  
    public partial class Form2 : Form  
    {  
        public event EventHandler<TransferTestEventArgs> OnTransferText;  
 
        public Form2()  
        {  
            InitializeComponent();  
        }  
 
        private void Form2_Load(object sender, EventArgs e)  
        {  
            if (sender != null)  
            {  
                OnTransferText(this, new TransferTestEventArgs(this.textBox1.Text));  
            }  
        }  
 
        public class TransferTestEventArgs : EventArgs  
        {  
            public static string _Text;
 ;  
 
            public TransferTestEventArgs(string Text)  
            {  
                _Text = Text;  
            }  
        }  
    }  
}  
form1中e._text改为form2._text;
2009-09-22 09:53
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:0 
精辟!
2009-09-22 10:13
馹妮玛丽
Rank: 2
等 级:论坛游民
帖 子:9
专家分:12
注 册:2009-9-18
收藏
得分:0 
好!
2009-09-22 11:08
ZYF1988
Rank: 1
等 级:新手上路
帖 子:35
专家分:6
注 册:2009-5-13
收藏
得分:0 
好!
2009-09-24 00:26
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
在Form2对象创建时候把Form1的textbox控件当做参数传给Form2(需要重写一个构造函数),这样的方法最简单

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-24 00:52
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:5 
我来解释一下2楼的方法
1,在Form2中自定义事件句柄OnTransferText,和传递text的消息TransferTestEventArgs
2,在Form1中创建Form2实例时候注册事件处理方法,就是对Form2.TransferTestEventArgs进行处理的方法form_OnTransferText
3,于是在当Form2中OnTransferText事件发生时,会调用Form1的form_OnTransferText进行处理
4,if (sender != null)  {  OnTransferText(this, new TransferTestEventArgs(this.textBox1.Text));} 是事件触发的地方
但是2楼写的方法是将Form2的textbox内容回传给Form1的textbox,跟楼主想要的不符啊
 
收到的鲜花
  • zhoufeng19882009-09-24 11:52 送鲜花  2朵   附言:好人! 对了,楼主的意思好像就是把Form2中 ...

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-24 01:11
Mo诫
Rank: 4
等 级:业余侠客
帖 子:80
专家分:223
注 册:2009-7-29
收藏
得分:5 
**********************************************************方法1
中介方(定义委托)——如窗体Form1
C# code
    //定义委托
    public delegate void MyDel(String str);
 
 
服务方(定义事件,引发事件)--Form1  
C# code
    public partial class Form1 : Form
    {
        //定义事件
        public event MyDel MyEvent;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            //引发事件  
            MyEvent(this.textBox1.Text);
            f2.Show();
        }
    }
 
 
客户方(订阅事件,处理事件) ——如窗体Form2
C# code
    public partial class Form2 : Form
    {
        public Form2(Form1 f1)
        {
            InitializeComponent();
            //订阅事件
            f1.MyEvent += new MyDel(f1_MyEvent);
        }
 
        //处理事件
        void f1_MyEvent(string str)
        {
            this.label1.Text = str;
        }
    }
**********************************************************方法2
在Form2窗体里做一个属性:
        private string TextCont;
        public string TextCont1
        {
            get { return TextCont; }
            set { TextCont = value; }
        }
然后按钮的click事件:
        private void button1_Click(object sender, EventArgs e)
        {
            this.TextCont1 = textBox_B.Text;
            DialogResult = DialogResult.OK;
        }
最后回到Form1窗体,按钮的click事件:
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog() == DialogResult.OK)
                TextBox_A.Text = f2.TextCont1;
        }
 
2009-09-24 10:34
快速回复:两个窗体,如何做到对其中一个的操作影到到另一个??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026931 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved