C# Form UserControl OnClick events - c#

I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".

Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.

Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}

Related

How to programmatically invoke the Click event handler of a TextBox

I know how to programmatically invoke the event handler of a Button:
button1.PerformClick();
I would like to do the same for the Click event handler of a TextBox. The problem is that TextBox does not have a
textBox1.PerformClick();
I suggest method extraction (why should we mix UI - windows messages and Business Logic):
//TODO: put a better name here
private void onMyTextBoxClick() {
//TODO: relevant code here
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick();
}
Then you can just call onMyTextBoxClick:
...
// Same business logic as if MyTextBox is clicked
onMyTextBoxClick();
...
Edit: If you really want EventArgs aruments, just provide them:
//TODO: put a better name here
private void onMyTextBoxClick(TextBox box, EventArgs e) {
//TODO: relevant code here
}
// Default EventArgs
private void onMyTextBoxClick(TextBox box) {
onMyTextBoxClick(box, EventArgs.Empty);
}
// Both TextBox and EventArgs are default ones
private void onMyTextBoxClick() {
onMyTextBoxClick(MyTextBox, EventArgs.Empty);
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick(sender as TextBox, e);
}
Usage:
// Default EventArgs
onMyTextBoxClick(myTextBox);
// Custom EventArgs
EventArgs args = ...
onMyTextBoxClick(myTextBox, args);

How to perform Click-Event on a hidden button

In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }

C# Calling a method

I have a form with multiple instances of a user control on it.
I've assigned the following:
Switch.armySwitchCloseButton.Click += armySwitchClose;
So when one of those instances is pressed, I call the following method:
void armySwitchClose(object sender, EventArgs e)
The above method has a bunch of additional code in it which isn't required for here.
Now what I need to do is from another button, call this above function from every instance.
How can I do this?
Many Thanks
In constructor of each user control you can pass the same instance of some object who knows how to run this method
void armySwitchClose(object sender, EventArgs e)
Then, you call this method inside each event method, for ex:
Public Class UserControl(){
private MakeEvent makeEvent;
Public MyClass(MakeEvent makeEvent)
{
this.makeEvent = makeEvent;
Switch.armySwitchCloseButton.Click += armySwitchClose;
}
void armySwitchClose(object sender, EventArgs e)
{
makeEvent.armySwitchClose(sender,e);
}
}
Public Class MakeEvent() {
void armySwitchClose(object sender, EventArgs e)
{
//the real implementation
}
}
Hope this solve your problem.
Oh, I think I get your problem wrong... When you click in one button all the other events in others UC must be triggered, right? This problem can be solved with the Observer Pattern
Public Interface IObserver
{
void armySwitchClose(object sender, EventArgs e);
}
Public UserControl1: Observer
{
void armySwitchClose(object sender, EventArgs e)
{
//implementation UC1
}
}
Public UserControl2: Observer
{
void armySwitchClose(object sender, EventArgs e)
{
//implementation UC2
}
}
In button UC:
Public UserControlButton
{
private List<IObserver> observers;
public void addObserver(IObserver observer)
{
observers.Add(observer);
}
public void button_clickedEvent(object sender, EventArgs e)
{
foreach(IObserver observer in observers)
{
observer.armySwitchClose(object sender, EventArgs e);
}
}
}
In form with all buttons you call addObserver adding each user control.

How to propagate the name change of Form's UI controls's throughtout all the .cs in project

In C# , Winform, I have created a form and bunch of UI controls on it. I have changed the name of the controls through Properties windows but the following automated generated code did not update automatically. However, the InitializeComponent code is automatically updated though. My problem is now that I don't remember which box or whihc label I renamed to certain name.. Two questions : How could I have done this more efficiently to begin with? Question 2) Is there anything I could do now to make it automatically change the corresponding names? I have heard of refactoring but I don't know if I could have used it here and how? I appreciate any help.
public partial class frmMyInterface : Form
{
public frmMyInterface()
{
InitializeComponent();
}
private void frmMyInterface_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Rename each these event handlers and then on the property window, reassign the events selecting from the dropdrown. Or delete these event handlers and double click on each event in the property window and this time it will update it for you

Having issues getting around the scope of my event handler(ASPX .NET 3.5)

I've been trying to template control panels in my site so I can take a panel and populate it fully. I'm good up until the point where my event handling needs to access functions on my page. My current test will take me to a login redirect page. So how can I get this event handler to perform a redirect?
public class DebugButton : Button
{
public string msg;
public DebugButton()
{
this.Click += new System.EventHandler(this.Button1_Click);
this.ID = "txtdbgButton";
this.Text = "Click me!";
msg = "not set";
}
protected void Button1_Click(object sender, EventArgs e)
{
msg = "Event handler clicked";
}
}
*on the Page*
protected void Page_Load(object sender, EventArgs e)
DebugButton btnDebug = new DebugButton();
PnlMain.Controls.Add(btnDebug);
Really appreciate the help. Thanks!
To do a redirect you can use:
Note:
Assuming that your login page is named login.aspx and it is located in root folder of your website.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/login.aspx");
}
or
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("login.aspx");
}
If you want the event to have access to the page, then the page needs to subscribe to the click event.
Aka:
on the Page
protected void Page_Load(object sender, EventArgs e)
{
DebugButton btnDebug = new DebugButton();
btnDebug.Click += new System.EventHandler(Button1_Click);
PnlMain.Controls.Add(btnDebug);
}
protected void Button1_Click(object sender, EventArgs e)
{
// access whatever you want on the page here
}
I just found out that that System.Web.HttpContext.Current will get me the current context of the page. So long as the custom class is part of the app(this one is in the apps folder of course) I'm good to go. Heres a sample of my quick TestTemplate that I used to make a custom button.
public class TestTemplate : Button
{
public TestTemplate()
{
this.Text = "Click Me";
this.ID = "btnClickMe";
this.Click += new System.EventHandler(this.EventHandler);
//
// TODO: Add constructor logic here
//
}
public void EventHandler(object sender, EventArgs e)
{
//System.Web.HttpContext.Current.Server.Transfer("Default.aspx");
System.Web.HttpContext.Current.Response.Write("This is a test!");
}
}

Categories

Resources