I have 50 UserControls that I add to a flowlayoutPanel dynamically.
I need to set focus to a user control but it doesn't work.
I have been searching a lot but can't find any example that I understand.
The only example that I find is this
Setting Focus to a .NET UserControl...?
I tried to use userCtrl.Focus(); but it didn't work.
As I have been reading the usercontrol doesn't like to have focus.
Addition: Now that I understand more of the Control class, I
understand that if you derive from Control you should not subscribe
to its events, but use the On.. functions, like OnEnter. I've
changed my answer accordingly
To Activate any Control, including a UserControl use Control.Select().
If you do this for a TextBox, you'll see that Select ensures that it gets the input focus.
I guess you want to do something with the selected UserControl (the Control that has the focus), for instance, you want to change its appearance, or select any of the controls on it. To do this, your UserControl class has to subscribe to the events Control.Enter and Control.Leave
I have created a UserControl with a CheckBox that is automatically checked whenever the UserControl is selected (has the input focus):
Addition: If you derive from a Control, don't subscribe to events Enter and Leave. Instead override the functions that raise these events: OnEnter / OnLeave.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
protected override void OnEnter(EventArgs e)
{
this.checkBox1.Checked = true;
base.OnEnter(e); // this will raise the Enter event
}
protected override void OnLeave(EventArgs e)
{
this.checkBox1.Checked = false;
base.OnLeave(e); // this will raise the Leave event
}
}
I have a form with a button, and an event handler that is called when the button is clicked:
private void OnButton1Clicked(object sender, EventArgs e)
{
this.userControl1.Select();
}
Now whenever the button is clicked I see that the user control gets the focus because the check box is checked and whenever I click elsewhere the checkbox is unchecked.
You can set focus to a control by using the ActiveControl Property
this.ActiveControl = myUserControl;
Though you did not detail what did you mean it did not work, focusing has many aspects conventionally.
1. Explicit focusing
Calling Focus() method of a control is the same as setting ActiveControl of the container form. If CanFocus returns true (your control and all its parents are visible and enabled), it works; however, you will have no visual feedback, except some indirect hint, eg. the originally focused control (button or textbox) loses the focus.
To visualize the focused state you might want to use some custom paint:
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.Clear(Focused ? SystemColors.Highlight : SystemColors.Control);
}
If you derive directly from Control instead of UserControl, override the following two methods to force a repaint on changing the focused state:
protected override void OnGotFocus(EventArgs e)
{
Invalidate();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
Invalidate();
}
2. Focusing by the mouse
To receive focus by clicking the control add this line to the constructor:
SetStyle(ControlStyles.Selectable, true);
If you derive directly from Control instead of UserControl, override the OnMouseDown, too:
protected override void OnMouseDown(MouseEventArgs e)
{
if (!Focused)
Focus();
base.OnMouseDown(e);
}
3. Focusing by the keyboard
To receive focus by the TAB key just set the TabStop property to true and adjust the TabOrder property.
Example to focus on textBox1:
textBox1.Select();
you can try tab index of the user control. If you set its tab index to 1 it will be focused once the program start.
Related
I have Windows Form TestForm, and in my Form I have several labels that are only used to display some text.
I need to display a MessageBox.Show anytime the Form is clicked. So I have an event handler for the click, which looks like this:
private void TestForm_Click(object sender, EventArgs e)
{
MessageBox.Show("The form has been clicked");
}
Unfortunately, the click event doesn't fire when I click over a label in the Form. Is there a way to fix this, besides consuming the click event for the labels?
Thanks.
To use the same click event for all labels:
In the properties for each label, go to the Events (lightning bolt tab).
You will see (probably near the top) a label for Click, click the dropdown for this event, and you will be shown a list of handlers that you could use for that label.
Here's the Properties > Events > Click handler (bottom right):
Because all of your labels are of the same type, and produce the same EventArgs, you are able to use the same handler for all of them.
Then, when you are adding more Labels, just choose the event handler from the Click event dropdown:
Hope this helps!
To flesh out LarsTech's comment, I have used something like this in the past when I was having problems with labels overlapping each other and lack of true transparency in WinForms. What I did was make the labels invisible on the Form, then iterate through them in the Form's paint event, pull the information out of them and then use Graphics.DrawString to draw the text. That way you you will still be able see them in design mode.
This is a quick example of what I mean.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (var temp in this.Controls)
{
if (temp is Label) //Verify that control is a label
{
Label lbl =(Label)temp;
e.Graphics.DrawString(lbl.Text, lbl.Font, new SolidBrush(lbl.ForeColor), new Rectangle(lbl.Location, lbl.Size));
}
}
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("The Form has been clicked");
}
}
I have created a custom XAML UserControl class that I pass to the ShowDialog method. I would like to be able to trigger clicking the OK button on the dialog through other events generated in my UserControl - for example double clicking a ListItem. I have the code to handle the double click just fine (tied into the MouseDown even and checked the click count) but I don't know how to trigger a new event to the parent dialog to close it.
// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
// trigger dialog to close with OK button
}
}
Make your user control implement IDialogContent. You can then call directly to the CloseDialog event you implemented, and that will trigger the dialog closure.
You will need to decide how you want to handle that in your follow up code by setting some sort of state on your user control/view model or some other data as fits your particular extension.
// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
CloseDialog(this, EventArgs.Empty);
}
}
I need TextBox that supports double click, but while trying to code it, i stuck with a problem that Silverlight textbox doesn't raise left-button mouse events if i click inside of text area. So what are possible solutions here? I'm using Silverlight 4.
You should create a custom textbox control that inherits from the default TextBox and override the OnMouseLeftButtonUp method, like this:
public class MyTextBox : TextBox
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
// Do your stuff here
}
}
If you need to handle the double click, it would be easier to switch to Silverlight 5 that introduced the concept of a click count. Here is a tutorial for handling the double click: http://www.silverlighthostingnews.com/index.php/archives/440
Basically you will just have to do this:
if(e.ClickCount == 2) {...}
EDIT: here is how to do in Silverlight 4:
Create the custom textbox that inherits the default TextBox class and override the mouse events like so:
-
public class DoubleClickTextBox : TextBox
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
// Do nothing
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
// Do nothing
}
}
Add the DoubleClickExtender class in your project class.
In your code-behind, attach to the double click event:
-
DoubleClickExtender dce = new DoubleClickExtender(textBox, 300)
dce.DoubleClick += new MyEventHandler(dce_DoubleClick);
You may be able to cheat with this one, although I haven't tried this myself
Change the template of the textbox to have a transparent overlay that
is IsHitTestVisible = true
Set the double click event on the overlay.
If you detect a single
click on the overlay, do nothing and let it bubble through
If you detect a double click, set the event handled of the
mousebuttoneventargs to true and perform your double click action
Please note that double click on Silverlight4 isn't natively supported, so you'll have to come up some way to simulate that.
I have a problem with MouseEvents on my WinForm C# application. I want to get ALL mouse clicks on my application.
How to determine which control has been clicked ?(I'm beginner C#)
Try this:
private void Control_Clicks(object sender, EventArgs e)
{
Control control = (Control)sender; // Sender gives you which control is clicked.
MessageBox.Show(control.Name.ToString());
}
This, this or this may help.
Hope it helps.
private void Form1_Load(object sender, EventArgs e)
{
SetupClickEvents(this);
}
/// <summary>
/// This will loop through each control within the container and add a click handler to it
/// </summary>
/// <param name="container">The container whose children to handle clicks for</param>
private void SetupClickEvents(Control container)
{
foreach(Control control in container.Controls)
{
control.Click += HandleClicks;
}
}
private void HandleClicks(object sender, EventArgs e)
{
Control control = (Control)sender;
MessageBox.Show(string.Format("{0} was clicked!", control.Name));
}
If you're doing Windows Forms, you have several options :
Hook mouse event, and after figure out if the clicked component actually makes part of your application
You can declare a base class MyComponent : Control. That component overrides MousClick event and raise a special event notifying about a fact. Every control in your app derive from that control, so every control will notify about click happened on it. It's enough to subcribe
to thier events and process them as requested.
Just a couple of ideas...
You'd have to wire them all up to the same event handler. This can be done in the properties window for the controls in question. You could also write your own function to traverse the control tree and tie the function to each of their event handlers.
You can recursively traverse the Form.Controls collection with a foreach loop.
void attachGlobalHandler(Control aToParse, EventHandler aGlobalHandler)
{
foreach(Control lControl in aToParse.Controls)
{
attachGlobalHandler(lControl, aGlobalHandler);
lControl.Click += aGlobalHandler;
}
}
And then you call that on your form, with the name of the function you want to call:
attachGlobalHandler( Form1, myClickHandler );
And that should tie it to EVERY clickable control on the form. The sender argument of the handler should then always refer to the control that fired the event. That being said, I'd probably just attach individual event handlers, unless you need to treat multiple controls as a group.
WARNING: The code above is untested.
For the second question asked "How to determine which control has been clicked?" each control has events which may be handled in code.
The easiest way to know when a control has been clicked is to attached to the clicked event of a control which is done from the properties for the control. You may have to click the lightning bolt icon to see the events. Double-clicking beside the even will create an empty handler.
For example if you had a simple form with a single button attaching click events to the form and to the button will tell you when there is a click anywhere. In most cases the button click would be the most useful to handle.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
It's really simple!
On your click event in your Win-Form, You add
// Here is a modified version of your code:
private void Form1_Click(object sender, EventArgs e)
{
var control = Form1.ActiveControl;
}
If I do not create an "Edit->Copy" menu item and assign it the shortcut keys "CTRL+C", then I can select a control (RichTextBox, DataGridView, etc..) and hit "CTRL+C" and the control itself will handle the copy. I can copy text out, and paste it into notepad, etc..
Now throughout my whole form, I have a lot of controls. But I have a custom control that I want to make clear that I handle Copy functionality for. So I added the ShortcutKey CTRL+C to Edit->Copy, and by default it is set to Enabled.
Now, I have to implement an event handler for the 'click' event on that menu item. If I explicitly put in code to handle the copy, then it works:
public void menuEditCopy_Click(object sender, EventArgs e)
{
myCustomControl.Copy();
}
However, now Copy does not work on any other type of control. My first inclination was to find out the type of control that has focus, and implement a limited set of copy code for each of them:
public void menuEditCopy_Click(object sender, EventArgs e)
{
if (this.ActiveControl is MyCustomControl)
{
((MyCustomControl)this.ActiveControl).Copy();
}
else if (this.ActiveControl is RichTextBox)
{
((RichTextBox)this.ActiveControl).Copy();
}
}
etc...
However, my controls are added to a SplitContainer, and debugging shows that this.ActiveControl is set to the splitcontainer instance, not the control, even though I know that control is selected.
So my last thought is to literally check if every control has focus:
public void menuEditCopy_Click(object sender, EventArgs e)
{
if (myCustomControl.Focused)
{
myCustomControl.Copy();
}
else if (richTextBox1.Focused)
{
richTextBox1.Copy();
}
}
I would like to avoid this if possible, it is a lot of controls, and if I add a new control, I would need to update it. Is there a better way of doing this?
Thanks
A SplitContainer implements ContainerControl, so you could check for either one and look for it's ActiveControl instead. ContainerControl is the base class, so I would go for that - you might catch another type of container as well:
private void DoCopy(Control control)
{
if(control is ContainerControl)
DoCopy(control.SelectedControl);
else if(control is MyCustomControl)
((MyCustomControl)control).Copy();
else if(control is RichTextBox)
((RichTextBox)control).Copy();
else
throw new NotSupportedException("The selected control can't copy!");
}
void menuEditCopy_Click(object sender, EventArgs e)
{
DoCopy(this.ActiveControl);
}
You could try settting the KeyPreview property of your form to true. Then you could set up a handler for the form's KeyDown event which would look like the following:
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if(e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
{
if (ActiveControl.GetType() == typeof(MyCustomControl))
{
((MyCustomControl)ActiveControl).Copy();
e.Handled = true;
}
}
}
Here you are specifying that you have handled the Ctrl-C event by setting the event args Handled property to true. Else, if you leave it as false, the Ctrl-C key press will be handled as per usual by each individual control.
Because we have set the KeyPreview to true the form's handler gets to see each key press before any other control that it contains and can decide to deal with the key press itself or else allow it to be handled in the same way as if the form had never previewed it.
I think as well it would be necessary to remove the short-cut key from your menu item (although you could still manually put the text "Ctrl+C" next to your menu item name) for this to work, otherwise your menu item will hijack the key stroke.