I have a usercontrol that contains a FlowLayoutPanel (topdown flow) with a bunch of radiobuttons. The control exposes a CheckedChanged event that fires whenever one of the radiobuttons's check changed.
My form contains the usercontrol and a textbox. I subscribe the usercontrol's CheckedChanged event and depending on which radiobutton gets checked, I either disable the textbox or put a focus inside the textbox.
All this works fine with mouseclick when changing the radiobutton's check state. However, this will hang indefinitely when using the arrow keys. I don't understand why the difference.
The following are steps to reproduce the behavior I'm seeing:
Create a usercontrol and drop a FlowLayoutPanel control and set its FlowDirection = TopDown. Then add two radiobuttons to the FlowLayoutPanel.
Provide an event handler in the usercontrol
public event EventHandler CheckedChanged
{
add { radioButton2.CheckedChanged += value; }
remove { radioButton2.CheckedChanged -= value; }
}
Create a windows form and drop the above user control. Add a textbox and set Enabled to False. Subscribe to the usercontrol's CheckedChanged event as follows
private void userControl11_CheckedChanged(object sender, EventArgs e)
{
textBox1.Select();
}
Run. Notice that if you use the mouse to click between the radiobuttons, thing works fine; but it will crash if you use the up/down arrow keys.
public event EventHandler CheckedChanged
{
add {
radioButton2.CheckedChanged += value;
}
remove {
radioButton2.CheckedChanged -= value;
}
}
Hmm, value is uninitialized? Or am I missing something?
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 have a ContextMenuStrip containing two TextBoxes.
I want the validate the text inserted in a TextBox by using the TextBox.Leave event, but whenever I click outside the TextBox or the ContextMenuStrip, the event does not fire. The same for other events, like Validate, Enter etc. However, some events like MouseEnter or Click work.
Do you have any ideas?
Thank you!
I found a workaround using the LostFocus event on this post:
https://social.msdn.microsoft.com/Forums/windows/en-US/79fe930b-880a-4e18-8e06-def459cec29d/how-to-detect-when-toolstriptextbox-loses-focus?forum=winforms
Just create LostFocus event handler manually. NOTE- LostFocus event is not showing in VS designer.
public Form1()
{
InitializeComponent();
toolStripTextBox1.LostFocus += ToolStripTextBox1_LostFocus;
}
private void ToolStripTextBox1_LostFocus(object sender, EventArgs e)
{
//Your code!!!
}
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 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 am using a windows form in which i have a build a usercontrol with one textbox and one CheckedList control. What i wanted to do is i want to capture the ListIndexChanged event of CheckListbox control in my form where i use this usercontrol.
Please help me how to do this
You can add this
public event EventHandler SelectedIndexChanged
{
add { checkListBox.SelectedIndexChanged += value; }
remove { checkListBox.SelectedIndexChanged -= value; }
}
to your control and subscribe to this event from the form
take a look here