Set User Control's default event - c#

I have a User Control containing a bunch of controls. I want to set the default Event of this User Control to the Click event of one of my buttons.
I know for setting default event to one of the UserControl's events I should add the attribute:
[DefaultEvent("Click")]
public partial class ucPersonSearch : UserControl
...
I'm wondering if it's possible to do something like:
[DefaultEvent("btn1_Click")]
public partial class ucPersonSearch : UserControl
...
I want to fire some methods in the form hosting this User Control at the time btn1 is clikced.
This is really a knit in my project, and you're answer will be valueable.

You can't expose events of your class members to the outside of the class. How can others subscribe to the Click event of a Button inside your UserControl? Did you try it? It's not possible unless you make the button accessible from the outside, which is not good (everybody can change all the properties).
You have to define a new event, and fire your new event when your desired event (clicking on the button) happens:
[DefaultEvent("MyClick")]
public partial class UCPersonSearch : UserControl
{
Button btnSearch;
public event EventHandler MyClick;
public UCPersonSearch()
{
btnSearch = new Button();
//...
btnSearch.Click += new EventHandler(btnSearch_Click);
}
void btnSearch_Click(object sender, EventArgs e)
{
OnMyClick();
}
protected virtual void OnMyClick()
{
var h = MyClick;
if (h != null)
h(this, EventArgs.Empty);
}
}

Related

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.

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

How to access selected tab tabcontrol from another component class?

I am trying to access(get) the selected tab from tabcontrol from another component class. I want see what tab is pressed every time someone clicks on a different one.
I have a component class which is embedded into another class which has the tabcontrol in it.
Thanks in Advance
If I understand you correctly, add an event handler for the tab control's TabIndexChanged event in your other component class.
Ex: you can use like this or you can achieve using property also
public class A
{
B obj1 = new B();
private void tabControl1_TabIndexChanged(object sender, EventArgs e)
{
obj1.PageChanged(tabControl1.SelectedIndex.ToString());
}
}
public partial class B
{
public void PageChanged(string Val)
{
//put your code
// after page chaged what should happen
}
}

Defining an event for a windows forms custom control

I'm working on an form custom control.the control is a MonthCalendar like Visual studio(C#) MonthCalendar control and I want to define an event for my control.
How can define a new event for this form custom control?
If your event should not provide any additional info (Foo is name of your event):
public event EventHandler Foo;
And raise it this way:
protected virtual void OnFoo()
{
if (Foo != null)
Foo(this, EventArgs.Empty);
}
If you need to pass some additional info to event handlers, then create custom arguments class by inheriting from EvenArgs class
public class FooEventArgs : EventArgs
{
public string Message { get; private set; }
public FooEventArgs(string message)
{
Message = message;
}
}
Declare event this way:
public event EventHandler<FooEventArgs> Foo;
And raise it this way:
protected virtual void OnFoo(string message)
{
if (Foo != null)
Foo(this, new FooEventArgs(message));
}
Its good practice to create protected methods for raising events by descendants of class where event is declared. Also good practice to use event naming convention:
add suffix -ing to event name for events which raised before
something happened (often you could cancel such events) (e.g. Validating)
add suffix -ed to event name for events which raised after something happened (e.g. Clicked)
As Thorsten stated, good practice to create virtual methods for raising events. Its not only allows to raise events from descendants, but also disable event raising, or adding some behavior prior/after event raising.
public event EventHandler<EventArgs> YourEvent;

How can I update a TextBox from a class using events?

I would like to update a form's textbox in winforms with data that is being processed from a class file.
Example:
Class.RunProcess();
Inside of RunProcess I would like to fire an event that will update the textbox with the text that I pass in on the win form.
I know you can do this with events so I don't have to make the textbox public or pass it into the many methods I have, I am just not quite sure how to start.
Edit:
Maybe I am just not clear.
I have a windows form with a text box on it.
I have a button that runs a process.
The process is in a class file outside of the form class.
Inside this process method I would like to be able to update the text box on the main form without having to pass the TextBox as a parameter to all the methods that I will have (I currently have 6 and will be more).
I thought I could subscribe to an event that would allow me to display a message in the text box while the processes ran in my class.
It's probably time to consider:
Databinding your textbox to your class
Model/View/Presenter (MVP)
For #1 (Databinding), you can use the INotifyPropertyChanged interface on your class and raise the event for changed properties.
public class BusinessModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _quantity;
public int Quantity
{
get { return _quantity; }
set
{
_quantity = value;
this.OnPropertyChanged("Quantity");
}
}
void OnPropertyChanged(string PropertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
And in your form, youcan bind the .Text property of the textBox to the object in several ways. Through the UI or in code.
Links:
Bind Better with INotifyPropertyChanged
How to: Implement the INotifyPropertyChanged Interface
Now, if you need to add to text that already exists such as in your example, you can either track the full text in the class or raise events from your class and respond in the form code. Tracking it in the class would be better - you really don't want any logic in the form at all, which brings us back to binding and/or some form of MVP/MVC.
If you're wanting Class.RunProcess to run when an event from an external class is fired then you can do it like this
public delegate void MyDelegate();
public class MyClass
{
public event MyDelegate Myevent;
public void DoSomething()
{
this.Myevent();
}
}
public static class Class
{
public static void RunProcess()
{
txtData.Text += "Message";
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MyClass myClass = new MyClass();
myClass.Myevent += Class.RunProcess;
}
}
You could add a parameter to your constructor that is a TextBox and save the reference in the class as a private variable and call that in each method.

Categories

Resources