Custom user control doesn't catch any mouse events in c# - c#

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
}
}

Related

Wait for Input in User Control

I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}

Is it possible to group click events?

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)
{
// ....
}
}

How do I can call event from usercontrol to main form

I have a userControl and I've a button there, I'd like to call event when I'm clicking on the button in my main form from userControl. I do this:
UserControl
public UserControlerConstructor()
{
_button.Click += new EventHandler(OnButtonClicked);
}
public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
public event ButtonClickedEventHandler OnUserControlButtonClicked;
private void OnButtonClicked(object sender, EventArgs e)
{
// Delegate the event to the caller
if (OnUserControlButtonClicked != null)
OnUserControlButtonClicked(this, e);
}
Form
public Form1()
{
userControlInstance.OnUserControlButtonClicked += new EventHandler(OnUCButtonClicked);
}
private void OnUCButtonClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
It doesn't work because when I click in the form do nothing in the form code, but it does in userControl code. But I'd like to do in form code. I don't know how to call event from userControl to the form.
Well now I don't know if you're explicity want to use the delegate, no? If not, why don't you just do:
public Form1()
{
userControlInstance._button.Click += OnUCButtonClicked;
}
private void OnUCButtonClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
up to now your code does not compile. You are using the wrong event handler type. It should show the following compiler error:
EventHandler cannot be converted to ButtonClickedEventHandler
Do the following steps:
1) put the declaration of the delegate outside of the class UserControlerConstructor:
public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
public partial class UserControlerConstructor: UserControl
{
1) then change the type of the handler when registering the event in Form:
public Form1()
{
userControlInstance.OnUserControlButtonClicked += new ButtonClickedEventHandler(OnUCButtonClicked);
}
This way it should work

Handling Button Clicks from a different Class

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.

C# event Click TextBox

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);
}
}

Categories