当前位置:网站首页>Data interaction between C # WinForm development forms
Data interaction between C # WinForm development forms
2022-07-22 21:44:00 【A chicken with multiple senses】
Originally, I learned it for the first time Winform Development did not expect to use multiple forms , But I found that some functions are really difficult to realize without multiple forms , So you still have to use multiple forms , One of the problems faced by multiple forms is data interaction , For example, my main interface is a question bank , Sub interface is a topic , Then I won't tell the sub topic which topic and which path , Obviously, I don't know which sub topic to show .
How to realize the interaction between forms , There are about three options :
(1) Modify the constructor of the subform
I think this is the simplest and crudest , It's also one I use , The principle is very simple, that is, you can directly modify the constructor , Here's an example :
stay Form1.cs in , The definition code is as follows :
album_SendTo_ANS = A;
node_SendTo_ANS = thisNode;
Form Form2 = new Form(this);
Form2.ShowDialog();
So in Form2.cs in , You can get the constructor as follows :
public Form2(Form1 form1)
{
InitializeComponent();
album_SendTo_ANS = form1.get_album_SendTo_ANS();
node_SendTo_ANS = form1.get_node_SendTo_ANS();
}
Of course, here I set the two passed parameters to form1 Private members of , Therefore, encapsulation is still used here get To get . This is actually Form Form2 = new Form(this); You can set parameters according to your own needs , Because the number of parameters here may change , Therefore, choose the simple method of directly transferring the form , Generally, parameters are transmitted .
(2) Add dependent properties to the subform
Here we want to make the child form belong to the parent form , Therefore, you can set the Owner attribute ,Owner The value of the property is a reference to the parent form . When a child form belongs to the parent form, it has the following features :
① The child form is minimized and closed with the parent form . for example , If subform Form2 Parent form Form1 all , Then close or minimize Form1 when , It also turns off or minimizes Form2.
② The child form does not appear after the parent form . Subsidiary forms can be used to find and replace windows such as windows , When the owner form is selected , These windows should not disappear . To determine the form owned by a parent form , have access to OwnedForms attribute .
There are two ways :
Form2 form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
Form2 form2 = new Form2();
form2.ShowDialog(this);
When the affiliation is determined , You can modify parameters in the following ways :
Form1 form1 = (Form1)this.Owner;
this.Data1 = form1.Data2;
(3) By way of entrustment
Compared with the above two methods , This method may be more complicated , But the basic idea is the same , The changes are as follows :
① stay Form2.cs Add delegate function definitions in :public delegate void SendFun(string str);
② stay Form2.cs Add delegation examples in :public event SendFun SendToParent;
③ stay Form2.cs Add a button to , And add the following events :
private void button1_Click(object sender, EventArgs e)
{
if (Send != null)
{
Send(this.textBox1.Text);
}
}
④ stay Form1.cs Add method in RecvInfo() The method is as follows :
private void RecvInfo (string str)
{
textBox1.Text = str;
}
⑤ stay Form1.Designer.cs Medium InitializeComponent() Method to add a delegate instance definition :
this.myForm.Send += new Example3_Form2. SendFun (RecvInfo);
That is to say Form Communication between , I haven't realized this scheme yet , I also saw it on other blogs , So record .
边栏推荐
- Prompt "warning: remote host identification has changed" when SSH
- C# Winform开发 打开txt文件 并显示在 RichTextBox中 加上编辑后保存功能
- Bash variable -- position parameter variable
- 记录:问题1,电脑闪屏;问题2,电脑桌面图标双击没有反应
- 7.远程管理命令
- 与 Cartesi 联合创始人一起喝杯咖啡
- ActiveMQ persistence steps
- NVIDIA CLARA资料
- Wechat applet comprehensive case practice 1
- Application & rich text editor & file upload
猜你喜欢
随机推荐
水库防汛泄洪抢险应急广播系统建设4G无线广播模式分析
Prompt "warning: remote host identification has changed" when SSH
C语言学习笔记:函数递归与迭代+例子
蓝桥杯-省赛-翻硬币
C# Winform开发 WinAppDriver 自动测试的使用
Coinbase's top 10 forecasts for Web3 and encryption economy in 2022
Coinbase对2022年Web3和加密经济的10大预测
C WinForm development file / folder creation
Solve problems encountered when compiling mosquitto
Compared with technology and products, listening and opinions are the top priority of Web3 at present
6.拷贝移动及文件内容命令
Bash basic functions - wildcards and other special symbols
C# Winform开发 文件/文件夹的新建
Cartesi March 2022 review
2022年Q2行业展望及Portfolio项目进展
Wechat applet introduction tutorial learning notes
Makefile中如何自动生成头文件依赖
Web编程入门 2.3 解决fakepath问题 获取本地文件真实地址
小区IP网络广播背景系统解决方案-基于局域网、专网或广域网传输
与 Cartesi 联合创始人一起喝杯咖啡