Olukayode Oluwole
asked on
How can i avoid repeating the same script/method in every form
I have these 4 buttons across on all my c# windows application forms
(Save,Update,Delete and Exit) see buttons below
On 1 on the forms I have done the following which works as desired.
1. on form_load I call a library that accesses the database to determine whether a button should be visible or not
see the form_load call to the library below:
1. In my Form-Load Event
FormPermissionsProcessor processor = new FormPermissionsProcessor() ;
var result = processor.GetFormExecStatu s();
SetButtons();
2. In my Library the status each button is determined after reading this from a database
3. Method in my form that processes the returned values
Currently this process works for 1 form but i don't want to be repeating
the method in 3 above on every form
How can I achieve this particularly since the 4 buttons (Save,Update,Delete and Exit) are local to each form
Thanks
Olukay
(Save,Update,Delete and Exit) see buttons below
On 1 on the forms I have done the following which works as desired.
1. on form_load I call a library that accesses the database to determine whether a button should be visible or not
see the form_load call to the library below:
1. In my Form-Load Event
FormPermissionsProcessor processor = new FormPermissionsProcessor()
var result = processor.GetFormExecStatu
SetButtons();
2. In my Library the status each button is determined after reading this from a database
public class FormPermissionsProcessor
{
public Tuple<string, string, string,string> GetFormExecStatus()
{
PermissionsModel model = new PermissionsModel();
GlobalConfig.Connection.GetFormPermissions(); // This line goes into the database to fetch the variables below
string libregstatus = LoginDetails.staticformexecute;
string libreglevel = LoginDetails.staticaddbutton;
string liblicensetype = LoginDetails.staticupdatebutton;
string libdel = LoginDetails.staticdeletebutton;
return new Tuple<string, string, string,string>(libregstatus, libreglevel, liblicensetype,libdel);
}
}
3. Method in my form that processes the returned values
private void SetButtons()
{
saveButton.Visible = false;
updateButton.Visible = false;
deleteButton.Visible = false;
if (LoginDetails.staticaddbutton == "Y")
{
saveButton.Visible = true;
}
if (LoginDetails.staticupdatebutton == "Y")
{
updateButton.Visible = true;
}
if (LoginDetails.staticdeletebutton == "Y")
{
deleteButton.Visible = true;
}
}
Currently this process works for 1 form but i don't want to be repeating
the method in 3 above on every form
How can I achieve this particularly since the 4 buttons (Save,Update,Delete and Exit) are local to each form
Thanks
Olukay
ASKER
Please confirm the following:
What you have suggested means that the User Control Script will be defined Once for the whole application
and the Form script will be on every form in the application.
Is that correct ?
If so The script that I am trying NOT to replicate will be much shorted than this suggestion
Is it not better in that case to stay with my old script as i was looking NOT to keep repeating the code in my original post
in every form.
What do you think
Olukay
What you have suggested means that the User Control Script will be defined Once for the whole application
and the Form script will be on every form in the application.
Is that correct ?
If so The script that I am trying NOT to replicate will be much shorted than this suggestion
Is it not better in that case to stay with my old script as i was looking NOT to keep repeating the code in my original post
in every form.
What do you think
Olukay
An alternative may be that you create a base form that contains the common controls, methods, and events. Then each form was created by inheriting the base form. In case any form requires an additional process for any particular event or method, then you can overwrite it.
ASKER
kaufmed,
Just to restate that we started from my NOT wanting to repeat the method below on every form
private void SetButtons()
{
saveButton.Visible = false;
updateButton.Visible = false;
deleteButton.Visible = false;
if (LoginDetails.staticaddbut ton == "Y")
{
saveButton.Visible = true;
}
if (LoginDetails.staticupdate button == "Y")
{
updateButton.Visible = true;
}
if (LoginDetails.staticdelete button == "Y")
{
deleteButton.Visible = true;
}
}
Will your advise actually achieve this ??
I can see you seam to have implemented a small project on this .
Can you send it to me so i can carefully review before implementing
Thanks
Olukay
Just to restate that we started from my NOT wanting to repeat the method below on every form
private void SetButtons()
{
saveButton.Visible = false;
updateButton.Visible = false;
deleteButton.Visible = false;
if (LoginDetails.staticaddbut
{
saveButton.Visible = true;
}
if (LoginDetails.staticupdate
{
updateButton.Visible = true;
}
if (LoginDetails.staticdelete
{
deleteButton.Visible = true;
}
}
Will your advise actually achieve this ??
I can see you seam to have implemented a small project on this .
Can you send it to me so i can carefully review before implementing
Thanks
Olukay
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
For example:
User Control
Open in new window
Form
Open in new window
In the above, the form adds handlers to the "SaveButtonClick", "UpdateButtonClick", "DeleteButtonClick", and "ExitButtonClick" events. In this way, you can allow each form to respond differently when a particular button is clicked.
The user control itself deals with making the call to determine button visibility. That is handled by making the SetButtons method public--so the form itself can call it. It won't be visible to anyone except the form to which the user control is added.
Take note that the question marks next to each Invoke call are important. These (implicitly) check for null events. If you don't check for null, and you don't add a handler to a particular event, then Invoke will throw an exception when it is called.
I won't harp on your use of static methods/properties too much other than to say that you should work toward not relying on static members as much. Try to use instance members as much as possible.