简体   繁体   English

从单独的设计时表单访问设计时控件

[英]Accessing design time controls from a separate design-time form

I am looking for the easiest way to access the design-time controls created in the main form of a windows application form from another form. 我正在寻找一种最简单的方法来访问从另一个窗体在Windows应用程序窗体的主要窗体中创建的设计时控件。 I am going to want to manipulate all the controls and would prefer not to make getter and setter methods. 我将要操纵所有控件,并且不希望使用getter和setter方法。 There will only ever be a couple of additional forms that are only used for input in order to change the states of controls on the primary form--so I am not concerned about best practices here because this is not going to be a big project where many people are going to be working and multiple variables are going to get confused, and because I am completely new to this and want something that works. 只会有几个仅用于输入的其他形式,以便更改主要形式上的控件状态-因此,我在这里并不关心最佳实践,因为这不会是一个大项目,许多人将要工作,并且多个变量将变得混乱,因为我对此是完全陌生的,并且想要一些可行的方法。 I am not working in a professional programming environment. 我不在专业的编程环境中工作。

I would like to be able to simply address them with dot syntax somehow. 我希望能够简单地以点语法解决它们。

That said: Merely changing the type to "public" doesn't seem to allow me to do this however. 就是说:仅将类型更改为“ public”似乎不允许我这样做。

So, for a textbox, when I change it from: 因此,对于文本框,当我将其更改为:

private System.Windows.Forms.RichTextBox AssignmentSectNumber;

To

public System.Windows.Forms.RichTextBox AssignmentSectNumber;

Where the C# system automatically declares it once I put it into my form, I still can not access it from form two with 当我将C#系统放入表单后,C#系统会自动对其进行声明,但是我仍然无法使用

Form1.AssignmentSectNumber.Text

Is there any way that I can set up the controls in Form1 so that they can be addressed in Form2 either by going through Form1 through dot syntax or simply by directly addressing them? 有什么方法可以在Form1设置控件,以便可以通过点语法通过Form1或直接对其进行寻址来在Form2中对其进行寻址?

PS: I do not see a way to make controls "public" under any of the properties listed when I put them into a form at design time. PS:在设计时将控件放入表单时,在列出的任何属性下都没有使控件“公开”的方法。 Am I missing something? 我想念什么吗?

I do not see a way to make controls "public" under any of the properties listed when I put them into a form at design time. 在设计时将控件放入表单时,我看不到任何使控件在列出的属性下“公开”的方法。 Am I missing something? 我想念什么吗?

Select the Control and then in the Properties Pane, change its Modifiers Property to Public. 选择控件,然后在“属性”窗格中,将其“修饰符”属性更改为“公共”。

When I run the program form1 appears on the screen. 当我运行程序form1出现在屏幕上。 Doesn't this mean that there must be an instance of it? 这不是意味着必须有一个实例吗? If the name of that intance isn't form1 what would it be? 如果该实例的名称不是form1,它将是什么?

You need to access the currently running instance of Form1. 您需要访问当前正在运行的Form1实例。 There are many ways to do this. 有很多方法可以做到这一点。 One very simple method is to set the Owner of the secondary form when you call its Show() method: 一种非常简单的方法是在调用辅助表单的Show()方法时设置辅助表单的所有者:

// ... from within Form1 ...
Form2 f2 = new Form2();
f2.Show(this); // <-- setting Form1 as the "owner" of Form2 via "this"

Now, over in Form2, you cast the .Owner property back to Form1 and access the Control(s) you changed the Modifiers Property for to Public: 现在,在Form2中,将.Owner属性转换回Form1,并访问将Modifiers属性更改为Public的控件:

// ... over in Form2 ...
Form1 f1 = (Form1)this.Owner;
f1.label1.Text = "Hello from Form2!";

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

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