简体   繁体   English

在按钮上单击以其他窗口形式显示面板c#

[英]Displaying a panel in another windows form on button click c#

I'm working on ac# program and I want a panel to appear on a form when a button is clicked in another. 我正在使用ac#程序,并且我希望在另一个按钮上单击某个按钮后,面板会出现在表单上。 So when the add button is clicked on form2 the panel requesting the details for this to be possible will be displayed on form 1. 因此,当在form2上单击添加按钮时,要求提供详细信息的面板将显示在表单1上。

I currently have a static method set up in form1 which can be accessed from form2 - however due to panel.Show() being non static it won't allow me to use this in the function. 我目前在form1中设置了一个静态方法,可以从form2中访问它-但是由于panel.Show()是非静态的,因此不允许我在函数中使用它。

In Form1 I have: 在Form1中,我有:

public static void showPanel()
{
   panel.Show()
}

In my second form I have the following: 在第二种形式中,我具有以下内容:

private void btn_add_Click(object sender, EventArgs e)
{
   form1.showPanel();
   this.Hide();
}

I have tested with just having the static function show a message box which works. 我已经测试过,只有静态功能显示一个有效的消息框。 Is it possible to do it the way I want or do I need to take a few steps back and try a different technique? 是否可以按照我想要的方式进行操作,还是需要退后一步并尝试其他技术?

Are we talking about a new instance of the second form if so you can try to instantiate the new form using: 我们是否在谈论第二种形式的新实例,如果您可以尝试使用以下方法实例化新形式:

Form newForm = new YourFormName(potential parameters); newForm.showPanel(); newForm.Show();

If you want to execute the form on an open form you can give the first form a reference(field with instance) of the second form. 如果要在打开的表单上执行表单,则可以为第一个表单提供第二个表单的引用(带有实例的字段)。 Or you can try using: Application.OpenForms . 或者,您可以尝试使用: Application.OpenForms if you give it [1] it'll give you the second open form probably. 如果您给它[1],它可能会给您第二个开放形式。 You can also use .OfType to get the correct form in case your form order isn't always the same. 您还可以使用.OfType获取正确的表单,以防表单顺序不总是相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM