创建form1的新实例并更改该表单属性 [英] Creating a new instance of form1 and change that forms property
问题描述
在我当前的项目中,我制作了一个粘滞便笺式应用程序,其中用户可以打开多个相同表单的实例。然而,我想知道在C#中是否有一种方法可以创建form1的新实例,但是新窗体的标题/文本/标题为form2。
如果这是可以实现的,我想推动如何编写我的应用程序的部分的正确方向。
谢谢大家。
- 编辑 -
我意识到我忘了添加一些重要的东西:
我需要能够计算当前打开多少个实例。从那里开始,我将为表单文本添加+1。
尝试从类中访问表单属性:
MyForm newForm = new MyForm();
newForm.Show();
newForm.Text =Form2;
或者从当前表单调用一个方法来设置文本:
//在MyForm中
public void SetTitle(string text)
{
this.Text = text;
}
//调用方法
MyForm newForm = new MyForm();
newForm.Show();
newForm.SetTitle(Form2);
希望这有助于您!
打开的表单数量可以尝试如下所示:
//在MyForm中
private int counter = 1;
public void Counter(int count)
{
counter = count;
}
//示例
MyForm newForm = new MyForm();
counter ++;
newForm.Counter(counter);
这可能会让人困惑,但可以说你有一个按钮可以打开一个新的实例同样的形式。由于您有一个窗体在开始时打开 counter = 1
。每次点击该按钮时,它都会向窗体发送 counter ++
或 2
。如果你打开另一个表单,它会发送 counter = 3
等等。可能有更好的方法来做到这一点,但我不确定。
In my current project, I am making a sticky-note type application in which the user can have multiple instances open of the same form. However, I was wondering if there was a way in C# where I can create a new instance of form1 however have the new form's title/text/heading to be form2.
If this is achievable, I would like a nudge into the correct direction of how to code that part of my app.
Thanks everyone.
--EDIT--
I realized I forgot to add something important:
I need to be able to calculate how many instances are currently open. From there I will add +1 to the form text.
Try accessing the forms properties from the class:
MyForm newForm = new MyForm();
newForm.Show();
newForm.Text = "Form2";
Or call a method from the current form to set the text:
// In MyForm
public void SetTitle(string text)
{
this.Text = text;
}
// Call the Method
MyForm newForm = new MyForm();
newForm.Show();
newForm.SetTitle("Form2");
Hope this helps!
To check the amount of forms open you could try something like this:
// In MyForm
private int counter = 1;
public void Counter(int count)
{
counter = count;
}
// Example
MyForm newForm = new MyForm();
counter++;
newForm.Counter(counter);
It may be confusing to use, but lets say you have a button that opens a new instance of the same form. Since you have one form open at the start counter = 1
. Every time you click on the button, it will send counter++
or 2
to the form. If you open another form, it will send counter = 3
and so on. There might be a better way to do this but I am not sure.
这篇关于创建form1的新实例并更改该表单属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!