So I created a custom UserControl just out of a PictureBox and a RadioButton, something like the following:
public partial class Foo : UserControl
{
//some declared properties for the designer...
}
Now I added this object to a Form and subscribed to it's Click() event.
private void customAddedContr_Click(object sender, EventArgs e) { }
But just clicking on this red marked part fires the Click() event.
So just by clicking on the UserControl itself, so when a click is performed on the PictureBox or RadioButton this Click() event does not get fired. I thought that the Click event for this new UserControl gets fired for each click aiming to this Window Handle.
So do I need to bind the PictureBox's and RadioButton's Click() event to the UserControl's Click() event earlier on or what do I oversee?
Edit:
For understanding purpose, here is a colored picture of the CustomControl.
The top dark grey part is the PictureBox. The blue one is the UserControl itself on which I placed the other controls. The light grey bottom is the RadioButton. So just clicking on the blue part (UserControl) fires the Click() event.
I would recommend exposing the picturebox and radio buttons click events separately.
You raise your own events for the radio and picturebox at the usercontrol level and inside each click even you would raise the controls events. then you handle those click events outside the control and respond accordingly.
public partial class Foo : UserControl
{
public event EventHandler RadioButtonClicked;
public event EventHandler PictureBoxClicked:
}
then in the picturebox or button click event inside the control you raise the individual event at the control level
private void PictureBox1_Click(object sender, EventArgs e)
{
if (PictureBoxClicked != null)
PictureBoxClicked(sender, e);
}
Related
I have a UserControl (let's call it "PresentationCell") which contains a label, and an PictureBox.
In another control, which is using this PresentationCell, I have added an event
presentationCell.GotFocus += OnFocus;
private void OnFocus(object sender, EventArgs e)
{
if (sender is PresentationCell current)
current.BackColor = Color.Azure;
}
This will not be fired, if I click / focus on the Label or PictureBox that is within the PresentationCell.
How can I make it fire, when just something within the PresentationCell is in focus?
The problem here is, that the Label and PictureBox controls aren't selectable controls, so they aren't able to receive focus from mouse clicks.
What you could to instead, is to handle the mouse click event and check if you have hit the PresentationCell. If the PresentationCell is hit you can programatically set the focus like so:
hitPresentationCell.Focus();
This will then fire the GotFocus event.
In your OnFocus method you will have to switch the focus to another control or the event will fire endlessly.
I'm trying to create a custom control which fires an even on click.
My control is just a panel with a couple of labels and a picturebox inside.
The click works perfectly, the only issue is that I have to click the background of the control and if I press on the picturebox, is not working.
I've added the on click event to the control, but I would like to press in every place of it to trigger the event, not just the background of the panel.
I thought about adding a transparent object that covers entirely the control. I actually don't like this idea, however, I've tried with a picturebox, but i cannot see through it. It's not transparent. I can just see the panel background but It covers the labels and the image.
Thanks for the support.
If you just have a couple of objects in your panel, you can hook the Click event of all objects it contains to the same event handler, there is nothing wrong doing this.
public class MyUserControl : UserControl
{
public event Action<MyUserControl> MyControlClick
public string ID {get; set;}
public MyUserControl()
{
InitializeComponents();
// The same event handler code will be used for the three controls
myPictureBox.Click += global_Click;
myLabel1.Click += global_Click;
myLabel2.Click += global_Click;
this.Click += global_Click;
}
void global_Click(object sender, EventArgs e)
{
if (MyControlClick != null)
MyControlClick(this);
}
}
If you have a more important amount of objects, you can rely on this answer to create a truly transparent panel that handles clicks. The drawback is that you will have to detect which object has been clicked by using HitTest based on the mouse location.
On the form side :
aControl.MyControlClick += aControl_MyControlClick;
// ...
// This code is triggered when a MyUserControl is clicked
void aControl_MyControlClick(MyUserControl ctl)
{
MessageBox.Show(ctl.ID);
}
Actually! You cannot raise any event to the element in the Usercontrol unless you have to apply own method to your usercontrol or you can disable the element in the usercontrol but it will change the color of that element but It will raise the click event when you click your control.
I have a custom control created, comprised of a bunch of text boxes, 1 button and radio buttons.
I then have a "parent" control which only has this 1 custom control placed on it, and in the code behind I have a reference to this control's Presenter.
The presenter handles the actual code for searching (when the one button is pressed). How do I set up the button click event on the child control to call the Search method from the presenter?
So I have the following:
CtlSearchDetails
ViewSearchScreen
PresenterSearchScreen
The button click event is on CtlSearchDetails and it needs to call the method on PresenterSearchScreen. I cannot figure out how to reveal this method to the instace of the control on ViewSearchScreen.
In your custom child control, you want to expose an event for the button click:
public event Action OnButtonClicked;
Then hook the button clicked event from the designer
private void btn_myButton_Clicked(object sender, EventArgs e)
{
if (OnButtonClicked != null)
OnButtonClicked();
}
Then in your parent container, you want to handle this event from the child control
this.myChildControl.OnButtonClicked += new Action(onChildButtonClicked);
private void onChildButtonClicked()
{
// Do your search here
}
I have a UserControl defined such that:
UserControl
TextBox
Button (Clear)
I have a GotFocus handler on the UserControl so that whenever it gets focus, it calls TextBox.Focus(). The problem I am running into is that If I click the clear button, it clears the text and then refocuses to the textbox, triggering two GotFocus events on my control. I want this to act as either:
One GotFocus event
One GotFocus event (button), One LostFocus event(button), One GotFocus event (textbox)
I have played with FocusManager.IsFocusScope to no avail. Is there even a way to trigger a manual LostFocus right before I call Textbox.Focus?
In your GotFocus event you can check whether the mouse is over the clear button and whether the left mouse button is pressed, in such a case you can ignore the call to TextBox.Focus():
private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
if ((this.clearButton.IsMouseOver && Mouse.LeftButton == MouseButtonState.Pressed) == false)
{
this.textBox.Focus();
}
}
I have a class StripButton which inherits from Label. I have overriden the onClick method. The user of this class also assigns events to the onClick eventhandler. I have noticed that the overriden method gets fired last. I want the overriden method to fire first. How can I do that?
The Click handler of a StripButton on your Form will fire when the base.OnClick method in the OnClick handler is called in your UserControl. So if you want your UserControl's OnClick code to run before the Click handler code runs on your Form, call base.OnClick last in your UserControl's OnClick handler. Conversely, if you want your Form's Click code to run after your UserControl's OnClick code, call base.OnClick first in your UserControl's OnClick handler.
It's probably easiest to illustrate with the simplest example:
// Your UserControl.
class StripButton : Label
{
protected override void OnClick(EventArgs e) {
Console.WriteLine("This runs before the Click handler on the parent form.");
base.OnClick(e);
Console.WriteLine("This runs after the Click handler on the parent form.");
}
}
// On your form.
private void stripButton1_Click(object sender, EventArgs e) {
Console.WriteLine("stripButton1_Click on form");
}
If you run this you'll see the following in the Output Window:
This runs before the Click handler on the parent form.
stripButton1_Click on form
This runs after the Click handler on the parent form.
Hopefully that answers your question.