How to reuse event - c#

I am creating one c# project. In this project I have one mdi form and many child forms. All the child forms contains one panel named as panel1.
Now when child form opens i use the following code in all child form
all child forms' load event contains the following line.
this.WindowState = FormWindowState.Maximized;
and all child forms' resize event contains the following line.
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
so my question is if possible that the above code i write only once so i donot write this code in all the child forms load and resize event.

Yes it is possible. You have to pull up common functionality into base class event handlers, and then invoke them from child ones:
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
this.Load += new System.EventHandler(this.FormLoad);
this.Resize += new System.EventHandler(this.FormResize);
}
protected virtual void FormLoad(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
protected virtual void FormResize(object sender, EventArgs e)
{
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
}
...
}
public class DerivedForm : BaseForm
{
protected override void FormLoad(object sender, EventArgs e)
{
base.FormLoad(sender, e);
// specific code goes here
}
protected override void FormResize(object sender, EventArgs e)
{
base.FormResize(sender, e);
// specific code goes here
}
...
}

Create a base class and derive every child class from it.
Like this:
public class FormBase : Form
{
public FormBase()
{
this.WindowState = FormWindowState.Maximized;
// put more generic code here
}
... // or here
}
public class YourMdiChild : FormBase
{
... // all code specific for that child
}

I think the best way is to do it before opening the child form!
public class ChildForm : Form
{
public void doTheSizing()
{
// make it maximize...
// your sizing of pannel...
// etc...
}
}
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm childForm01 = new ChildForm();
childForm01.doTheSizing();
// now show the child window using Show() or ShowDialog().
childForm01.ShowDialog();
}
}

Related

calling button click function from another class

i currently have one form and a few user control, i have a panel set from the Form1 - mainpanel, so that whenever i click a button, one of the user control will be showing on the mainPanel.
my problem is that one of the control(InfoSetting) has some other new buttons, and when i click it I would like the mainPanel to show the new user control. However, right now all other buttons from Form1 is working fine, but when I click on the InfoSetting button, nothing seems to show up.
here is my code from Form1
public partial class Form1 : MetroFramework.Forms.MetroForm
{
InfoSettings infoSetting;
CashSales cashSales;
PriceSetting priceSetting;
public Form1()
{
InitializeComponent();
infoSetting = new InfoSettings();
cashSales = new CashSales();
priceSetting = new PriceSetting();
}
protected void ButtonClicked(object sender, EventArgs e)
{
buttonHandler(sender);
}
public void buttonHandler(object sender)
{
Button button = sender as Button;
mainPanel.Controls.Clear();
if (button != null)
{
switch (button.Name)
{
case "HomeBtn":
infoSetting.Dock = DockStyle.Fill;
mainPanel.Controls.Add(infoSetting);
break;
case "cashSalesBtn":
cashSales.Dock = DockStyle.Fill;
mainPanel.Controls.Add(cashSales);
break;
case "settingBtn":
infoSetting.Dock = DockStyle.Fill;
mainPanel.Controls.Add(infoSetting);
break;
case "priceSettingBtn":
cashSales.Dock = DockStyle.Fill;
mainPanel.Controls.Add(cashSales);
break;
default:
mainPanel.Controls.Clear();
break;
}
}
}
and here is the code from the InfoSetting user control
public partial class InfoSettings : UserControl
{
public InfoSettings()
{
InitializeComponent();
}
private void priceSettingBtn_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.buttonHandler(sender);
}
}
as stuartd said you can add a call to your parent Form:
public partial class InfoSettings : UserControl
{
public InfoSettings()
{
InitializeComponent();
}
private void settingBtn_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1) this.Parent;
form1.buttonHandler(sender);
}
}
It would probably be better to have a common class that you can access in both form and user control and have the code you want to run in a method there. Something like:
public class FormHelper
{
public void CodeIWantToRunIn2Places()
{
// Implementation Here
}
}
You can implement the above as a singleton so you have access in both places. Then in your form classes you could do:
FormHelper.Instance.CodeIWantToRunIn2Places();
In your event handler as well as in your user control.
You can make the Form 1 as "static" and also the method buttonHandler(object sender) as "static" as well.
Then inside the priceSettingBtn_Click, call the below function directly
Form1.buttonHandler(sender);
I would have a global object with a pointer to all the forms in the application.
public Global
{
public Form1 MainForm;
public Dialog1 D1;
...
}
public static Global = new Global();
In as each form is created
Global.MainForm = form1;
then you can go
private void priceSettingBtn_Click(object sender, EventArgs e)
{
Global.MainForm.buttonHandler(sender);
}
As other have said , its probably better to not call the click handler, its better to have this code and the real click handler call a business logic level function

When displaying MDI child, the child form is cut off at the top

I have a problem with a Winforms application written in VS2012(C#).
I am posting it here after lots of research and attempts with no luck.
I have 2 MDI child forms and one parent form which I use to switch between them.
After loading child1 the 1st time, the MDI form looks like it was pushed up and the top part of it is hidden and cannot be seen; this problem is resolved after the 2nd click.
This is the source of child1 which is the same code for child2
public partial class Child1 : Form
{
public Child1()
{
InitializeComponent();
panel1.Dock = DockStyle.Top;
StationFormUtils.SetPanelHeaderDefinitions(panel1);
}
private void Child_Load(object sender, EventArgs e)
{
//removes the child bar
this.MaximizeBox = false;
}
}
public static void SetPanelHeaderDefinitions(Panel panel)
{
panel.Size = new System.Drawing.Size(BaseClass.StationTableWidth, BaseClass.StationHeaderHeight);
panel.BackgroundImage = global::TestMdi.Properties.Resources.StationHeaderStrip;
panel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Tile;
}
And this is the source code for the parent:
public partial class Form1 : Form
{
private Child1 ChildMainFrm1 = new Child1();
private Child2 ChildMainFrm2 = new Child2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildMainFrm1.MdiParent = this;
ChildMainFrm1.WindowState = FormWindowState.Maximized;
ChildMainFrm1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
ChildMainFrm2.MdiParent = this;
ChildMainFrm2.WindowState = FormWindowState.Maximized;
ChildMainFrm2.Show();
}
}
Does anyone know what am I doing wrong or what am I missing?

accessing form controls from the class file

I have one mdi form and many child forms all the child form contains panel control named as panel1 now i want to access panel1 control from my class file for that i write the following code but it is not working.
Class1.cs file :
public partial class FormBase : Form
{
public FormBase()
{
this.Load += new System.EventHandler(this.FormLoad);
//this.Resize += new System.EventHandler(this.FormResize);//error
this.panel1.Resize += new System.EventHandler(this.FormResize);//error as a panel1 is not access
}
protected virtual void FormLoad(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
protected virtual void FormResize(object sender, EventArgs e)
{
//error : panel1 control has a red underline
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;//error as a panel1 is not access
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;//error as a panel1 is not access
}
}
now my one of the child form
frmAddNewEmployee.cs
public partial class frmAddNewEmployee : FormBase
{
public frmAddNewEmployee()
{ InitializeComponent();
}
protected override void FormLoad(object sender, EventArgs e)
{
base.FormLoad(sender, e);
}
protected override void FormResize(object sender, EventArgs e)
{
base.FormResize(((Panel)sender).Name, e);//error
base.FormLoad(sender, e);//error
}
}
so my question is how to access this panel1 control of the child forms from my Class1.cs file

Accessing a Parent form button from a Child form button

I have a program that has a parent form which then creates a child form. Upon clicking the updateButton within the child form, I want the searchButton within the parent form to fire.
However I get an error for protection reasons. I have tried setting everything Public just to see, still wont work for me.
Error 1 'SalesSystem.SystemForm.searchButton' is inaccessible due to
its protection level SalesSystem\UpdateForm.cs 111 20 SalesSystem
This is what I have so far.
Parent Code
namespace SalesSystem
{
public partial class SystemForm : Form
{
public SystemForm()
{
InitializeComponent();
}
protected void searchButton_Click(object sender, EventArgs e)
{
//search code
}
private void updateButton_Click(object sender, EventArgs e)
{
try
{
UpdateForm upForm = new UpdateForm(resultBox.SelectedItems[0].Text, dbdirec, dbfname);
upForm.ShowDialog(this);
}
catch (Exception)
{
//
}
}
}
Child Code
namespace SalesSystem
{
public partial class UpdateForm : Form
{
public UpdateForm(string selectedPerson, string dbdirec, string dbfname)
{
InitializeComponent();
}
private void updateButton_Click(object sender, EventArgs e)
{
//do stuff
SystemForm parent = (SystemForm)this.Owner;
parent.searchButton.PerformClick();
this.Close();
}
}
}
Your searchButton button control is set to private by default in WinForm. You've said you set everything to public but I assume you mean you've set everything in the code you've posted to public. There are a few ways to fix this. The direct fix would be to simply go to Visual Studio designer, select the button, and set its Modifier property to internal or public.
However, it seems you're closing your form straight after so I'd just have my parent form subscribe to the FormClosing event of the form.
UpdateForm upForm = new UpdateForm(resultBox.SelectedItems[0].Text, dbdirec, dbfname);
upForm.FormClosing += (s, o) =>
{
//your code for what the parent class should do
};
upForm.ShowDialog(this);
If you're not closing the form then you can create your own event handler that your parent form subscribes to.
You have 2 options:
create a public void search() method in your parent form. Then, instead of accessing the the button on the parent form and invoking its click event, you run the search code directly. The new method is not tied to a GUI element and accessing it from a different form is no problem.
The better solution is to create a delegate. A delegate is an execution target that will be assigned at run time. The parent form still has a public void search() method. And when it creates the child form, it will pass the name of that function as parameter. The child form has no knowledge about the parent form (as opposed to the first option where the child MUST know that there is a method called search()). When it is time to inform whoever created the child form, the delegate is called. This is a small example:
public partial class SystemForm : Form
{
public delegate void dSearch();
public SystemForm()
{
InitializeComponent();
}
protected void searchButton_Click(object sender, EventArgs e)
{
search();
}
private void search()
{
//search code
}
private void updateButton_Click(object sender, EventArgs e)
{
try
{
UpdateForm upForm = new UpdateForm(resultBox.SelectedItems[0].Text, dbdirec, dbfname, search);
upForm.ShowDialog(this);
}
catch (Exception)
{
//
}
}
}
And the child form:
public partial class UpdateForm : Form
{
private SystemForm.dSearch _target;
public UpdateForm(string selectedPerson, string dbdirec, string dbfname, SystemForm.dSearch target)
{
_target = target;
InitializeComponent();
}
private void updateButton_Click(object sender, EventArgs e)
{
//do stuff
_target();
this.Close();
}
}
You should use the "Model View Controller" or "Model View Presenter" pattern to approach this kind of thing.
Each form should only be concerned with displaying its contents to the user. When it comes to responding to UI events such as button clicks, each form (i.e. each "View") should simply raise an event which informs the controller/presenter that something has happened.
The controller/presenter should then respond appropriately. Then the logic that wires together different forms (such as the parent and child forms in your example) is encapsulated in the Controller class. Such logic does not really belong in either of the forms.
I wrote an example that demonstrates a simple design to do this sort of thing in another answer some time ago. Rather than copy/paste it all here, I'll just give you a link to it:
How to make Form1 label.text change when checkbox on form2 is checked?
You'll have to scroll down to see my answer. It's broadly similar to what you're doing; hopefully it will make sense to you! Follow the instructions to make a test application and run it to see what happens.
I'm tired and might be missing something but that is correct behaviour.
Your child form does not directly inherit from your parent form.
Your parent form has a protected level, so only it and classes that extend it can access the method.
2 solutions:
Change your child form to:
public partial class UpdateForm : SystemForm
Change method to public
public void searchButton_Click(object sender, EventArgs e)
You could expose a Search Event from your UpdateForm and subscribe to this event in the SystemForm
namespace SalesSystem
{
public partial class SystemForm : Form
{
public SystemForm()
{
InitializeComponent();
}
protected void searchButton_Click(object sender, EventArgs e)
{
//search code
}
private void updateButton_Click(object sender, EventArgs e)
{
try
{
UpdateForm upForm = new UpdateForm(resultBox.SelectedItems[0].Text, dbdirec, dbfname);
upForm.OnSearch += Search;
upForm.ShowDialog(this);
}
catch (Exception)
{
//
}
}
private void Search(string searchParameter)
{
....
}
}
namespace SalesSystem
{
public delegate void SearchEventHandler(string searchParameter);
public partial class UpdateForm : Form
{
public event SearchEventHandler OnSearch;
public UpdateForm(string selectedPerson, string dbdirec, string dbfname)
{
InitializeComponent();
}
private void updateButton_Click(object sender, EventArgs e)
{
//do stuff
OnSearch("SearchThis");
this.Close();
}
}
}

Show/hide, BringToFront/SendToBack a panel on Parent form when a Child Form on the MDI Parent Form closes or appears

I need to hide a panel on the Parent Form when a Child Form on an MDI Parent Form closes & show back the panel on the Parent form when the Child Form is closed.
Currently am using SendtoBack() to show the Child Form infront of the Panel which is on the Parent Form , but when i close the Child Form, then the Panel doesn't appears back, even if i use :
BringtoFront()
OR
Panel1.Visible=true
public partial class CHILD : Form
{
private void CHILD_Load(object sender, EventArgs e)
{
this.FormClosed += new FormClosedEventHandler(CHILD_FormClosed);
}
void CHILD_FormClosed(object sender, FormClosedEventArgs e)
{
PARENTForm P = new PARENTForm();
P.panel1.BringToFront();
P.panel1.Visible = true;
}
}
public partial class Form1 : Form
{
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
CHILD P = new CHILD();
P.Showg();
P.MdiParent = this;
P.BringToFront();
panel1.SendToBack();
panel1.Visible = false;
}
}
THIS ISN'T WORKING....PLEASE HELP..!
You creating new parent form in child form. You need to pass parent form object to child form and then use it to show/hide panel and set panel Modifiers property to public.
For example...
Parent form:
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
ChildForm childForm = new ChildForm();
childForm.MdiParent = this;
childForm.Show();
}
}
Child form:
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private void Child_FormClosed(object sender, FormClosedEventArgs e)
{
ParentForm parentForm = (ParentForm)this.MdiParent;
parentForm.panel1.Visible = true;
}
}
Use this Code
in Parent Form
private void MainMenu_ChildForm_Click(object sender, EventArgs e)
{
ChildForm frm = new ChildForm();
frm.MdiParent = this;
ShowHideMainPanel(frm);
frm.Show();
}
void ShowHideMainPanel(Form frm)
{
frm.FormClosed += new FormClosedEventHandler(Form_Closed);
panel1.Visible = false;
}
void Form_Closed(object sender, FormClosedEventArgs e)
{
panel1.Visible = true;
}
by assigning close event for child to show panel after its closing

Categories

Resources