如何根据在父表单上单击的按钮打开包含规格的新表单? [英] How do I open a new form with specifications based on the button clicked on parent form?
问题描述
对不起,如果问题有点偏,还是编程中的新手。我似乎无法在一个问题中描述它。
我正在尝试通过使用1个表单来实现3个功能来简化我的程序(添加,编辑,查看)。现在有了这三个功能,唯一的区别是,编辑和视图中禁用了文本框。
我无法内化我应该如何处理这个。我正在使用组合框作为菜单。我已经指定了按钮,根据它所使用的菜单打开特定的表单。
我的计划是在父表单中的变量上放置一个值如果要启用字段,则单击按钮并在子窗体上调用它作为基础。但是,我似乎无法从父表单中调用它。
我尝试了什么:
尝试从父表单调用一个变量,但似乎无法调用它。
Sorry if the question is a bit off, still a newbie in programming. I can't seem to describe it in 1 question.
I'm trying to simplify my program by just using 1 form for 3 functions (Add, Edit, View). Now with those 3 functions, the only difference is that, Edit and View have textboxes disabled in them.
I'm having trouble internalizing on how I should approach this one. I'm using a combobox as a menu. I've specified the buttons to open specific forms based on what menu it is on.
My plan was to put a value on a variable in the parent form when the button is clicked and call it on the child form as a basis if the fields should be enabled or not. But, I can't seem to call it from the parent form.
What I have tried:
Tried calling a variable from parent form but can't seem to call it.
推荐答案
这不难,真的!
在Form2中创建一个枚举:
It's not difficult, really!
Create an enum in Form2:
public enum FormFunction
{
Add,
Edit,
View
}
然后将Form2构造函数更改为接受枚举的实例:
Then change the Form2 constructor to accept an instance of the enum:
public Form2(FormFunction function)
{
InitializeComponent();
switch (function)
{
case FormFunction.Add:
...
break;
case FormFunction.Edit:
...
break;
case FormFunction.View:
...
break;
default: throw new ArgumentOutOfRangeException("Unknown FormFunction: " + function);
}
}
并使用新构造函数构造它:
And construct it using the new constructor:
Form2 f = new Form2(Form2.FormFunction.Edit);
然后,您可以根据需要修改控件状态和可见性。
You can then modify your controls state and visibility as required.
这篇关于如何根据在父表单上单击的按钮打开包含规格的新表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!