I’m developing a user management system, in which I would like to re-use the GUI for the Add User form for the Edit User form, which just changing a few functions.
For example, in my Add Users, the User ID is set to the next available ID, and the Username, password, confirm password, and group are left blank. There is an Add User button that performs validation on the above mentioned fields, and the title of the form is “Add User”
All that needs to change for Edit user is to fill in the data from the user being edited (Getting the DataRow from my DataGridView is not a problem) and changing the Add button to an “Edit User” button while still performing the same validation, and changing the title of the form to “Edit User”.
Is such a thing possible? What would this type of use be called so I could google it more efficiently?
This is absolutely possible, and it’s done all the time. When you instantiate your form for add, you’ll typically do something like this:
And if you want to edit a user:
Notice that you’ll have two constructors for the form. Within the code for the AddUserForm (which you should really call ModifyUserForm, or UserForm), you’ll do an Add() if the user is null, and an Update() if it’s not null.
Example constructors:
Then save:
I did this all without Visual Studio, so I don’t promise it compiles. 🙂