I have a usercontrol inheriting TextBox. But somehow the click event does not work. Other events work. How to make that work click?
this.Click += new EventHandler(AfyTransparentTextBox_Click);
private void AfyTransparentTextBox_Click(object sender, EventArgs e)
{
MessageBox.Show("KY-KY");
}
I don't know if this will be appropriate in your scenario but it works:
public class AfyTransparentTextBox : TextBox
{
protected override void OnClick(EventArgs e)
{
System.Windows.MessageBox.Show("KY-KY");
base.OnClick(e);
}
}
Related
I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}
I have a Form with Button ButtonGo.
and I have a class which takes a button through its constructor, then handles its events:
public class HandlingClass
{//.......
Button go ;
public HandlingClass(Button btn)
{
this.go = btn;
this.go.Click += new EventHandler(this.go_Click);
}
//.....
public void go_Click(object sender, EventArgs e)
{
//logic here
}
What am I doing wrong, and why isn't the event being raised when I press the Button in the caller form?
This code works for me
public class HandlingClass
{
Button go;
public HandlingClass(Button btn)
{
go = btn;
go.Click += go_Click;
}
void go_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
}
and in your loaded event of the class having the button you just add the below code
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
HandlingClass hc=new HandlingClass(**MyButton**);
}
MyButton should be the reference to your button.
I create a custom user control and use that in my form.
but it does not capture any mouse event!
what is the problem?
thanks
Define the event in your custom control
private delegate void MyClickEvent(object sender, EventArgs e);
public event MyClickEvent MyClick;
public void OnMyClickEvent(object sender, EventArgs e)
{
if (MyClick != null)
MyClick(sender, e);//execute event
}
Now in MainForm
public partial class Form1
{
public Form1()
{
myCustomButton.MyClick += FireThisOnClick;
}
private void FireThisOnClick(object sender, EventArgs e)
{
//this will be executed on click
}
}
I've created a new Web User Control. This control has a button. On the click event of this button I raise an event in this way:
public partial class SearchDate : UserControl, IPostBackEventHandler
{
private static readonly object ObjEvent = new object();
// delegates
public delegate void VoidDelegate();
// events
[Category("Action")]
[Description("Azionato quando si preme il cancella nel filtro data")]
public event VoidDelegate CancelSearchDate
{
add
{
Events.AddHandler(ObjEvent, value);
}
remove
{
Events.RemoveHandler(ObjEvent, value);
}
}
protected virtual void OnCancelSearchDate()
{
var cancelEvent = (EventHandler) Events[ObjEvent];
if (cancelEvent != null)
{
cancelEvent(this, new EventArgs());
}
}
protected void BtnSearchDateCancelClick(object sender, EventArgs e)
{
OnCancelSearchDate();
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void RaisePostBackEvent(string eventArgument)
{
OnCancelSearchDate();
}
}
I've created a delegate, an event and raise it on click of the button.
In another web form, where this control is hosted, i've added an handler to this event in this way:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
InitDateFilter(SearchDate);
}
protected void InitDateFilter(SearchDate searchDateControl)
{
searchDateControl.CancelSearchDate += SearchDateControlCancelSearchDate;
}
void SearchDateControlCancelSearchDate()
{
// other stuff
currUtente.FiltroData = null;
SaveSession();
}
When I click to the button, it try to evaluate the event but it's always null.
I'm new in asp.net webforms and it seems to me strange that it not works well.
I've tried to raise the event in the simpler way (without the 'add' and 'remove' on the event definition), just calling the event if it isn't null but the behavior is the same.
Thanks
try to just override the OnInit event in the Page and add the handler to your event there.
like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitDateFilter(SearchDate);
}
Let me know if it helps
Im making a userControl named [File_Manager] and i was wondering if i can add a button to this custom control that i can set its job later after adding this custom control to another form .. something like
File_Manager fManager = new File_Manager();
fManager.SetFreeButtonJob( MessageBox.Show("Hello") ); // something like this.
then whenever user press that button .. the messageBox shows up.
So.. Is it possible to do that?
thanks in advance.
Sure you can. Just attach the buttons click handler to the action you pass in.
fManager.SetFreeButtonJob(() => MessageBox.Show("Hello"));
private void SetFreeButtonJob(Action action)
{
button1.Click += (s,e) => action();
}
Just note that passing in the Action breaks the encapsulation of user control though. You should probably do something like SetFreeButtonJob(Jobs.SayHello); and put the knowledge of what to do inside the control.
Create a custom event for your UserControl and fire it when your Button is clicked. You can then attach an event handler to the custom event in your Form. Or you can just raise the UserControl's Click Event when your Button is clicked.
public delegate void CustomClickEventHandler(object sender, EventArgs e);
public partial class buttonTest : UserControl
{
public event CustomClickEventHandler CustomClick;
public buttonTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CustomClick(sender, e);
}
}
and in your Form
public Form1()
{
InitializeComponent();
buttonTest1.CustomClick +=new CustomClickEventHandler(userControl1_ButtonClick);
}
private void userControl1_ButtonClick(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
Or as my second option try.
private void button2_Click(object sender, EventArgs e)
{
OnClick(e);
}
and in your Form subscribe to the UserControl's Click event.
buttonTest1.Click +=new EventHandler(buttonTest1_Click);
private void buttonTest1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Again");
}