Performing an event at the base form level - c#

This seems fairly simple, but I haven't been able to accomplish it. I have a BaseForm class that every form in my application inherits.
I simply want to execute a line of code every time a key is pressed in any form inheriting the BaseForm. In my BaseForm I've attempted the following with no luck:
public class BaseForm : Form
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
//Perform action
}
}
public class MainForm : BaseForm
{
//All of my main form code goes here.
}
Any help would be greatly appreciated! Thanks in advance

Probably you need to set KeyPreview of your base form to true for it to be able catch all the key presses from any control. Consider doing this in the form designer or in the base class constructor. I guess you've got some editors (a textbox, for example) on your derived forms, so you need the KeyPreview to be set to true for the base form to be able to catch those key presses.
You can either override the OnKeyPress method (as in your question) or add an event handler for the KeyPress event in the base form.
public class BaseForm : Form
{
public BaseForm()
{
this.KeyPreview = true; //it's necessary!!
//or just override the OnKeyPress method instead
this.KeyPress += new KeyPressEventHandler(BaseForm_KeyPress);
}
private void BaseForm_KeyPress(object sender, KeyPressEventArgs e)
{
//do your action
}
}

What you have done so far is correct. If your OnKeyPress is not being executed then you have something wrong - do you have an OnKeyDown that is interfering?
What you do next is to have the same override in your derived forms:
public class MainForm : BaseForm
{
//All of my main form code goes here.
protected override void OnKeyPress(KeyPressEventArgs e)
{
//do whatever action this form needs to, if any
base.OnKeyPress(e);
}
}
See that call to the base.OnKeyPress? That executes that line of code you have in the base. Note that you can put that call anywhere within the function, it might be more appropriate to have it at the start before the form specific code.

Related

Prevent event from firing in derived form

I have a base form and a derived form. Suppose I have only one button on my base form and derived form also uses same button.
I want to prevent the button click event from firing on derived form if the condition is not satisfied on base form click event. Could you please help how can I achieve this? Here is my code:
You can create a separate method that you mark as virtual then instead of adding an event handler to the derived form you just call the virtual method in the original form.
public Base : Form
{
public button1_Click(Object sender, EventArgs e)
{
DoSomethingOnClick();
}
protected virtual void DoSomethingOnClick()
{
// Do something here
}
}
Then in your derived form you can override DoSomethingOnClick to do what you need it to do:
public Derived : Base
{
private bool SpecificCondition = false;
protected override void DoSomethingOnClick()
{
if (SpecificCondition)
base.DoSomethingOnClick();
else
{
// Do something else here.
}
}
}
What this does is it means you only ever have one event handler for the click event so when it's clicked it calls DoSomethingOnClick() as this is a virtual method it can be overridden in the derived form and made to do something else. If you have a specific condition where you want the button click to only perform the base forms method then you can just call base.DoSomethingOnClick(), otherwise you insert your code to do something else.
This stops you having to worry about suppressing click events as there is only ever one.

How to call a class method from a different class

I am creating Windows form application and its main form contains a panel. Different user controls are being loaded on that panel on button click.
namespace LearnCSharp
{
public partial class MainForm : Form
{
private void configButton_Click(object sender, EventArgs e)
{
var uControllerDashboard = new Controllers.Dashboard();
panel.Controls.Add(uControllerDashboard);
updateNotification("active");
}
private void updateNotification(string state)
{
switch (state)
{
case "active" :
//Do something here
break;
}
}
}
}
when click config button it loads Dashboard user controller into Panel there is a another apply button in Dashboard userControl. When I click that button I need to call updatNotification method in MainForm Class.
namespace LearnCSharp.Controllers
{
public partial class Dashboard : UserControl
{
private void btnApply_Click(object sender, EventArgs e)
{
// Need to call updateNotification method here.
}
}
}
how can I achieve my requirement. I appreciate any help. Thanks.
Use events for that.
Instead of calling the MainForm inside the UserControl, create an event in the UserControl and have the MainForm subscribe that event. Inside the UserControl you just need to trigger the event.
There are many examples on web about this. Just take a look at this:
How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?
How do i raise an event in a usercontrol and catch it in mainpage?
Hope this helps.

How to move methods to external file in windows form C#

newbie question :(
I'm making a program using windows forms and i have a lot of small methods like this
private void label1_Click(object sender, EventArgs e)
{
textBox1.Select();
}
private void label13_Click(object sender, EventArgs e)
{
textBox13.Select();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
plotGraph(prostokat);
}
in the Form1.cs file and to make the code more transparent, I would like to move these small methods out somewhere to an external file (class?) but I don't really know how to do this. If they were normal methods I would just make a class and create an object of that class and just call the methods using that object but these are functions that "happen" when a user action is performed i.e. a textbox is clicked, so I'm not sure how to make this work.
It is possible to create an extra partial class (separated file) for your Form1 and place your cluttering methods there.
Or you could collapse them with #region
#region UI Handlers
#endregion
The perfect solution would be using some kind of MVVM for WinForms. In that case in your ViewModel you can implement your business logic separately from the code-behind.
Check out this:
https://www.codeproject.com/articles/364485/mvvm-model-view-viewmodel-patte
Hope it helps!
Have a look at your Form class subsection. It most cases it is still a partial class. Create a new .cs file in the same subsection in your project and add another partial form class to it.
You can find additional information here:
Partial Classes and Methods
Sure, you can add a new class to your project (right-click the project in Solution Explorer --> Add Class --> ) and put your methods there. Then you will need to hook the methods up to the controls in code:
I added a static class called "Form Methods" and put a method in there for label1 Click event:
static class FormMethods
{
public static void label1_Click(object sender, EventArgs e)
{
Label label = (Label) sender;
// Try to find the textbox through the label's parent form
TextBox textBox = (TextBox) label.Parent.Controls.Find("textBox1", true).First();
if (textBox != null)
{
textBox.Select();
}
}
}
Then in the Form Designer code, you can hook up the event:
this.label1.Click += new System.EventHandler(FormMethods.label1_Click);
Alternatively, you can make the class part of your original form class, and it will still be a separate file. If you want to do this, you can then make your event a private non-static method, and you would change the class definition to a public partial class:
public partial class Form1 // <-- This used to be: static class FormMethods
{
// This used to be: public static void label1_Click
private void label1_Click(object sender, EventArgs e)
{
. . .
And then hooking up the event looks like:
this.label1.Click += new System.EventHandler(label1_Click);
You can create any number of partial class files mimicking your original and group methods inside as your functionally needs - however, you won't be able to use the designer to directly navigate to your callbacks. That is, if you double click a graphic element or click an event of a graphic element you will have an unexpected behavior: in both cases you will have an event handler generated in your first partial and a hook created to that . . . so you can't directly navigate to those handlers anymore, and you need to go trough your partial files looking for their definitions.
Use partial to split C# code like this.
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}

User Control to pass event back to parent form

I have a main form with 3 User Controls attached to it. How can I have events inside a specific User Control reach out and modify the UI of the other two User Controls without creating spaghetti code?
Main Form (Form1.cs)
public partial class Form1 : Form
{
private UserControl1 userControl11;
private UserControl2 userControl21;
private UserControl3 userControl31;
public Form1()
{
InitializeComponent();
}
}
UserControl1.cs
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Change buttons around and fill in some data on user control 2 and 3
}
private void button2_Click(object sender, EventArgs e)
{
// Remove certain elements from control2 and 3
}
// .. So forth
}
I will have many events that do many different things on the other two user controls. I am creating a basic interface that's similar to Visual Studio where I can open projects, close them, add files, etc. These actions on one form should load up different windows and such in the other controls. What's the best method to achieve this? Creating custom events and parsing each event in the main form? Or accessing each user control directly from the first one? Or...?
The general pattern for events is:
public class MyClass{
public static EventHanlder<ofSomeType> MyEventHandler;
public void MyClass{
MyEventHandler = _MyEventHandler;
}
public void _MyEventHandler(object sender, OfSomeType args){
//do something with passed in args.
}
}
Other classes would use these eventhandlers like this...
public class myOtherClass{
public void doSomething(){
//do something here and signal the other class
var ofSomeTypeData = GetData();
MyClass.MyEventHandler(ofSomeTypeData);
}
}
Some will argue this style is too closely coupled but it works flawlessly and is a good start.
Create an event inside each UserControl. Form1 should subscribe to events on those controls.
So suppose that UserControl1 allows the user to sort the data. Then you might write this:
public Form1()
{
InitializeComponent();
// I assume UserControl1 was created by this point
userControl1.OnDataSorted = DataSorted;
}
// This will be called when the user wants to sort the data
private void DataSorted(UserControl1 sender, EventArgs e)
{
// Change buttons around and fill in some data on user control 2 and 3
}
Then you will create an event and a delegate in the UserControl.
public class UserControl1 {
public delegate void DataSortedDelegate(UserControl1 sender, EventArgs e);
public event DataSorted OnDataSorted;
private void button1_Click(object sender, EventArgs e)
{
if (OnDataSorted != null)
OnDataSorted(this, EventArgs.Empty);
// Replace EventArgs.Empty above with whatever data Form1 needs
}
This approach creates a separation of concerns between Usercontrol1 and Form1. The control does not need to actually modify the private controls inside Form1. It merely notifies Form1 that something has happened, and allows Form1 to make the changes.

OnLoad event oninherited form does not gets called

I have 3 forms
FormBase which has no onload event
FormBaseDetail : FormBase ->
on this form I used the visual designer to create an on_load event
FormBoxDetail : FormBaseDetail ->
on this form I also used the visual designer to create an on load event
When FormBoxDetail is created, the onload event on FormBaseDetail is called but not the onload event on FormBoxDetail. This is never called.
What am i doing wrong ?
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
}
}
public partial class FormBaseDetail : FormBase
{
public FormBaseDetail()
{
InitializeComponent();
}
private void FormBaseDetail_Load(object sender, EventArgs e)
{
MessageBox.Show("FormBaseDetail");
}
}
public partial class FormBoxDetail : Test_app.FormBaseDetail
{
public FormBoxDetail()
{
InitializeComponent();
}
private void FormBoxDetail_Load(object sender, EventArgs e)
{
MessageBox.Show("why am i not getting called");
}
}
There is only two reasons why Load event can be not fired:
Event handler FormBoxDetail_Load is not attached to Load event. But you are saying its not your case.
You are not loading FormBoxDetail. Make sure you are creating instance of FormBoxDetail class. Probably you are using FormBaseDetail instead. Make sure you are using correct form class.
Here both event handlers will be fired:
var form = new FormBoxDetail();
form.Show();
First one is a FormBaseDetail_Load handler, and then goes FormBoxDetail_Load handler.
It just happen to me, when, from one executable project I instantiated (new) a class e.a. MyClasss c = new MyDll.MyClass(par), and the Form did not load.
I found that I forgot to load it:
Application.Run(c);
Regards

Categories

Resources