Click here to Skip to main content
15,889,659 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 20-May-18 18:59pm
Comments
BillWoodruff 21-May-18 0:14am    
I'm confused: you say you are: "using 1 form for 3 functions" ... then, you talk about creating other Forms.

Please clarify your application design.

1 solution

It's not difficult, really!
Create an enum in Form2:
C#
public enum FormFunction
    {
    Add,
    Edit,
    View
    }
Then change the Form2 constructor to accept an instance of the enum:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900