如何从另一个表单添加面板中的表单

编程入门 行业动态 更新时间:2024-10-10 23:18:58
本文介绍了如何从另一个表单添加面板中的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好... 我在c#中做一个简单的项目,我想在一个面板中显示表单,表单从另一个面板添加在.NET中形成此条件然后代码非常简单

Hi All... I am doing a simple project in c#, i want to display the form's in one panel, the forms added that panel from another form also.IN this Condition in VB then the code is very simple

Public Class Form2 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Form3.TopLevel = False Form3.TopMost = True Form1.Panel1.Controls.Add(Form3) Form3.Show() End Sub End Class

但是在c#中这不是一个简单的,我知道如何添加控件在相同表格的面板中,但我是这个案例的新手,所以请帮助我,如何在C#Windows应用程序中从另一个表单添加面板中的表单。

But in c# this is not a simple,I know how to add the controls in panel from the same form but i am new for this case, So please Help me, How to add the Form in Panel from another Form in C# Windows Application.

推荐答案

这是一种非常糟糕的做事方式:它在C#中的工作方式与在VB中的工作方式相同,但是......这并不意味着它是一个很好的解决方案。 如果你翻译你的代码进入C#,它将起作用: This is a very poor way to do things: it will work just the same in C# as it does in VB, but...that doesn't mean it's a good solution. If you translate your code into C#, it will work: private void button1_Click(object sender, EventArgs e) { frmOther fOther = new frmOther(); fOther.TopLevel = false; fOther.TopMost = true; panel1.Controls.Add(fOther); fOther.Show(); }

但是......你的代码要求Form2知道Form1和Form3,这违反了OOP的规则并将表格联系在一起。 多了很多更好的解决方案是创建一个添加到面板的UserControl(如果需要独立显示,则设置为From3),因为这样可以保持整洁。 Form2根本不应该访问Form1控件!它本应该请求Form1自行完成,或者你最终想要对Form1做出任何改变,可能会破坏整个代码!

But... your code requires that Form2 knows about Form1 and Form3 which breaks the rules of OOPs and ties the forms together. A much, much better solution would be to create a UserControl which you add to the panel (and to From3 if it needs to be shown independently) as this keeps things tidier. And Form2 should not access Form1 controls at all! It should instead request Form1 to do it itself, or you end up with any change you want to make to Form1 potentially breaking code all over the place!

嗨...... 我的问题解决了以下代码我很怀疑,感谢Vulpes。 Hi... My problem is solved the following code is clear my doubt,Thanks for Vulpes. public class Form2 : Form { private void button1_Click(object, EventArgs e) { Form3 form3 = new Form3(); form3.TopLevel = false; form3.TopMost = true; Form1 form1 = (Form1)Application.OpenForms["Form1"]; Panel panel1 = (Panel)form1.Controls["panel1"]; panel1.Controls.Add(form3); form3.Show(); } }

此外,我还要感谢所有评论。

And Also i give the thanks for all Comments.

简单描述你在VB中做的事情:当在Form2的一个实例上单击一个按钮时: 1.取一个预先存在的实例Form3 2.将Form3的实例插入到Form1实例的Panel Control中 It将表单放在另一个表单中,或者在另一个表单中的控件内部是不是一个好主意(除非您使用旧的MDI架构,我建议您避免使用)。当他建议你使用UserControl时,OriginalGriff是正确的! 为了帮助你更好地理解WinForms中的编程,我们需要知道哪个Form是main表单在这里:Form1创建Form2和Form3吗? 如果是这样,那么我们可以说你的目标是: 1.给Form2的一个实例提供一种与Form1通信的方法,它需要在其中的一个Panel中插入一个UI实例。 因此,我们给Form1一种方式来订阅Form2上的Button Click事件,如下所示: 在Form2中:通过创建公开访问其按钮: To simply describe what you are doing in VB: When a Button is Clicked on an instance of Form2: 1. take a pre-existing instance of Form3 2. insert that instance of Form3 into a Panel Control in an instance of Form1 It is not a good idea to put a Form inside another Form, or inside a Control in another Form (unless you are using the old MDI architecture, which I suggest you avoid). OriginalGriff is right-on when he advises you to use a UserControl ! To help you get a better understanding of programming in WinForms, we need to know which Form is the "main" Form here: does Form1 create Form2, and Form3 ? If that's the case, then we can say your goal is: 1. to give an instance of Form2 a way to communicate to Form1 that it needs to insert an instance of a UI into a Panel inside it. So, we give Form1 a way to subscribe to the Button Click Event on Form2 like this: In Form2: by creating a publicly acessible reference to its Button: public partial class Form2 : Form { // create a public property of type 'Button public Button f2Button { set; get; } public Form2() { InitializeComponent(); // set the Property so 'Form1 can see it f2Button = this.button1; } }

在Form1中:订阅Button的点击事件

In Form1: by subscribing to the Button's Click Event

public partial class Form2 : Form { public Form1() { InitializeComponent(); } Form2 f2 = new Form2(); private void Form1_Load(object sender, EventArgs e) { // subscribe to Form2's 'button1 Click Event f2.f2Button.Click += f2Button_Click; } private void f2Button_Click(object sender, EventArgs e) { // the code to add some UI to the Panel on Form1 goes here MyUserControl uc = new MyUserControl; panel1.Controls.Add(uc); // set position, or Dock, or Anchor of the added UserControl as needed } }

通过将Form2暴露给其他表单/类的内容限制为只有Button,我们试图保持耦合(依赖)在表格/类之间至少。

By constraining what Form2 exposes to other Forms/Classes to only its Button, we are trying to keep the "coupling" (dependency) between Forms/Classes to a minimum.

更多推荐

如何从另一个表单添加面板中的表单

本文发布于:2023-11-02 09:59:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   面板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!