Set focus (boundary) for radiobutton - c#

I have a button which is rdbAuto, when form is load, rdbAuto will be checked, I want to set the focus (boundary) for this radiobutton, how can I do that?

You can override the RadioButton control with something like this
public class SuperRadioButton : RadioButton
{
private bool showFocusCues = false;
protected override void InitLayout()
{
this.GotFocus += (sender, args) =>
{
showFocusCues = true;
};
this.LostFocus += (sender, args) =>
{
showFocusCues = false;
};
}
protected override bool ShowFocusCues
{
get
{
return showFocusCues;
}
}
}
This will force the boundary to be shown whenever the radio button has focus.
Use this control instead of the standard radio button and then call the Focus method in the Form_Shown event
private void Form1_Shown(object sender, EventArgs e)
{
superRadioButton1.Focus();
}

Related

How to override my custom button's event without specify page variables

I created a CustomButton, inherited from Button class. Then I created some event, like GotFocus, LostFocus, etc.
public sealed class CustomButton : Button
{
public CustomButton()
{
this.DefaultStyleKey = typeof(CustomButton);
}
protected override void OnApplyTemplate()
{
this.GotFocus += CustomButton_GotFocus;
this.LostFocus += CustomButton_LostFocus;
base.OnApplyTemplate();
}
private void CustomButton_LostFocus(object sender, RoutedEventArgs e)
{
//some common behavior code
}
private void CustomButton_GotFocus(object sender, RoutedEventArgs e)
{
//some common behavior code
}
}
Then I used this CustomButton in some pages. But in some specified pages, I don't want the CustomButton excute the GotFocus, LostFocus event.
So how to override these event in the specified pages?
I tried add GotFocus, LostFocus event in the specified pages, but it will finally run the common code behavior in CustomButton.
For the requirement, you could make a DependencyProperty to control the GotFocus or LostFocus event could be excuted or not.
For example:
public class CustomButton : Button
{
public CustomButton()
{
this.DefaultStyleKey = typeof(Button);
Current = this;
}
private static CustomButton Current;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
public bool EnableDetected
{
get { return (bool)GetValue(EnableDetectedProperty); }
set { SetValue(EnableDetectedProperty, value); }
}
// Using a DependencyProperty as the backing store for EnableDetected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EnableDetectedProperty =
DependencyProperty.Register("EnableDetected", typeof(bool), typeof(CustomButton), new PropertyMetadata(0, new PropertyChangedCallback(OnValueChanged)));
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true)
{
Current.GettingFocus += Current_GettingFocus;
}
else
{
Current.GettingFocus -= Current_GettingFocus;
}
}
private static void Current_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
}
}
Usage
<local:CustomButton Content="Btn" EnableDetected="true" />
you can "take off" the events out of the CustomButton by iterating the page controls:
foreach(var cb in this.Controls.OfType<CustomButton>())
{
cb.GotFocus -= CustomButton.GotFocus;
cb.LostFocus -= CustomButton.LostFocus;
}
I believe this should work.

UserControl click event is slow

I have my UserControl, and I have attached it's click event so I can set it's border style.
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
this.Click += Item_Click;
IsSelected = false;
}
public bool IsSelected { get; set; }
void Item_Click(object sender, EventArgs e)
{
if (!IsSelected)
{
IsSelected = true;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
}
else
{
IsSelected = false;
this.BorderStyle = System.Windows.Forms.BorderStyle.None;
}
}
}
When I click over the UserControl I get it's border style assigned or removed... this works fine. But if I try to click faster It doesn't respond as I click on the UserControl.
If I click once and then wait and click again it works perfect but I want to increase the click response time, like if it was a button.
Any clue on how do I have this behavior?
If you are clicking very fast, you are getting a Double-Click event. Try using the MouseDown event instead.
But since this is the UserControl's base event, you can just override the method instead of attaching an event handler:
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left) {
// your code here...
}
}

NullReferenceException when subscribing to an event in the user control on the main form

On my User Control I have created an event to notify my main form that a tab control must switch its tab. This event must be triggered on a button's click on the user control. (I have a very similar event in another User Control that is working fine)
In my user control:
public delegate void EventHandler(object sender, EventArgs args);
public event EventHandler TabChangeRequested = delegate { };
protected void OnTabChangeRequested()
{
if (TabChangeRequested != null)
{
TabChangeRequested(this, new EventArgs());
}
}
private void btnBackToSelectType_Click(object sender, EventArgs e)
{
this.OnTabChangeRequested();
}
In my main form:
public FormMain()
{
InitializeComponent();
myUserControl.TabChangeRequested += (sender, args) => { ChangeRunTabToType(); };
}
private void ChangeRunTabToType()
{
if (this != null)
this.ChangeTabIndex(metroTabControlRun, 1);
}
When I run my program I get a NullReferenceException on the line
myUserControl.TabChangeRequested += (sender, args) => { ChangeRunTabToType(); };
Does anyone know where it comes from?
If myUserControl isn't instantiated yet (and that's what is null), then I'd recommend moving:
myUserControl.TabChangeRequested += (sender, args) => { ChangeRunTabToType(); };
to after where you are creating/assigning myUserControl!

Is there any way not to call Control Event?

Let's say there is event ComboBox_SelectedIndexChange something like this
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//do something//
}
And i have a function which changes value of ComboBox.
Private void MyFunction()
{
MyComboBox.Text = "New Value";
}
Can i make MyFunction prevent from calling the event MyComboBox_SelectedIndexChanged while changing the value of MyComboBox?
Can i make MyFunction prevent from calling the event MyComboBox_SelectedIndexChanged while changing the value of MyComboBox?
No, you cannot. You have two fundamental options, both of which accomplish the same thing:
You can unhook the event handler method from the control, set the value, and then reattach the event handler method to the control. For example:
private void MyFunction()
{
MyComboBox.SelectedIndexChanged -= MyComboBox_SelectedIndexChanged;
MyComboBox.Text = "New Value";
MyComboBox.SelectedIndexChanged += MyComboBox_SelectedIndexChanged;
}
You can declare a class-level field that will keep track of whether the value was updated programmatically or by the user. Set the field when you want to update the combo box programmatically, and verify its value in the SelectedIndexChanged event handler method.
For example:
private bool allowComboBoxChange = true;
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (allowComboBoxChange)
{
//do something
}
}
private void MyFunction()
{
allowComboBoxChange = false;
MyComboBox.Text = "New Value";
allowComboBoxChange = true;
}
You may attach or detach an event handler.
//attach the handler
MyComboBox.SelectedIndexChanged+=(sender,eventArgs)=>
{
//code
};
//detach the handler
MyComboBox.SelectedIndexChanged-=(sender,eventArgs)=>
{
//code
};
Or
Private void MyFunction()
{
comboBox1.SelectedIndexChanged -= new EventHandler(TestIt);
MyComboBox.Text = "New Value";
comboBox1.SelectedIndexChanged += new EventHandler(TestIt);
}
private void TestIt(object sender, EventArgs e)
{
//do something//
}

.net cf TextBox that displays keyboard on focus

I have a few text boxes on my UI that I want to display the mobile keyboard for when the control has focus, then go away.
Note: for this particular program, it is a tall screen and has no physical keyboard on the device.
Add an InputPanel to your Form, hook up the GotFocus and LostFocus events of the TextBox and show/hide the input panel in the event handlers:
private void TextBox_GotFocus(object sender, EventArgs e)
{
SetKeyboardVisible(true);
}
private void TextBox_LostFocus(object sender, EventArgs e)
{
SetKeyboardVisible(false);
}
protected void SetKeyboardVisible(bool isVisible)
{
inputPanel.Enabled = isVisible;
}
Update
In response to ctacke's request for completeness; here is sample code for hooking up the event handlers. Normally I would use the designer for this (select the textbox, show the property grid, switch to the event list and have the environment set up handlers for GotFocus and LostFocus), but if the UI contains more than a few text boxes you may wish to have it more automated.
The following class exposes two static methods, AttachGotLostFocusEvents and DetachGotLostFocusEvents; they accept a ControlCollection and two event handlers.
internal static class ControlHelper
{
private static bool IsGotLostFocusControl(Control ctl)
{
return ctl.GetType().IsSubclassOf(typeof(TextBoxBase)) ||
(ctl.GetType() == typeof(ComboBox) && (ctl as ComboBox).DropDownStyle == ComboBoxStyle.DropDown);
}
public static void AttachGotLostFocusEvents(
System.Windows.Forms.Control.ControlCollection controls,
EventHandler gotFocusEventHandler,
EventHandler lostFocusEventHandler)
{
foreach (Control ctl in controls)
{
if (IsGotLostFocusControl(ctl))
{
ctl.GotFocus += gotFocusEventHandler;
ctl.LostFocus += lostFocusEventHandler ;
}
else if (ctl.Controls.Count > 0)
{
AttachGotLostFocusEvents(ctl.Controls, gotFocusEventHandler, lostFocusEventHandler);
}
}
}
public static void DetachGotLostFocusEvents(
System.Windows.Forms.Control.ControlCollection controls,
EventHandler gotFocusEventHandler,
EventHandler lostFocusEventHandler)
{
foreach (Control ctl in controls)
{
if (IsGotLostFocusControl(ctl))
{
ctl.GotFocus -= gotFocusEventHandler;
ctl.LostFocus -= lostFocusEventHandler;
}
else if (ctl.Controls.Count > 0)
{
DetachGotLostFocusEvents(ctl.Controls, gotFocusEventHandler, lostFocusEventHandler);
}
}
}
}
Usage example in a form:
private void Form_Load(object sender, EventArgs e)
{
ControlHelper.AttachGotLostFocusEvents(
this.Controls,
new EventHandler(EditControl_GotFocus),
new EventHandler(EditControl_LostFocus));
}
private void Form_Closed(object sender, EventArgs e)
{
ControlHelper.DetachGotLostFocusEvents(
this.Controls,
new EventHandler(EditControl_GotFocus),
new EventHandler(EditControl_LostFocus));
}
private void EditControl_GotFocus(object sender, EventArgs e)
{
ShowKeyboard();
}
private void EditControl_LostFocus(object sender, EventArgs e)
{
HideKeyboard();
}
Use the InputPanel class. Enable it when you get focus, disable it when you lose focus.

Categories