UserControl click event is slow - c#

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

Related

Prevent tabkey focus change when usercontrol focused

I have a UserControl like following:
public partial class TextControlBox : UserControl
{
public TextControlBox()
{
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs e)
{
if(e.VirtualKey == VirtualKey.Tab)
{
//insert tabkey into textbox
}
}
}
The problem is when I add the UserControl into my Mainpage.xaml which also contains other controls, the tab-key moves the focus to the next control but
I would like it to insert a tab character (\t) into my textbox instead.
How can I archive this?
I tried handling the event using e.Handled = true if the tab-key is pressed but this did not work.
It seems that you will need to use UIElement.KeyDown Event instead of the CoreWindow.KeyDown Event. CoreWindow.KeyDown Event will not raise when the Tab key is pressed because it's a system key.
Here is the code that I'm using and it works:
public MyUserControl1()
{
this.InitializeComponent();
this.KeyDown += MyUserControl1_KeyDown;
}
private void MyUserControl1_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Tab)
{
e.Handled = true;
//insert tabkey into textbox
}
Debug.WriteLine("MyUserControl1_KeyDown");
}

How to pass values between UserControls?

So i've a Form1 with three UserControl's as UC1,UC2 & UC3. Form1 contains a Button, UC1 and UC3. Both UC1 & UC3 are initially hidden when the application runs and the Form1 button click event makes the UC1 visible. UC1 contains a panel and inside the UC1 load event I'm adding UC2 into the panel through panel.Controls.Add() method at application run time. UC2 contains a Button and the click event of this button makes UC3 visible in Form1. UC3 contain a textbox. Now when I click the UC2 Button, I want it to pass a string value to UC3 textbox. How do I do that? or generally I need a simple solution to send values from one UserControl to another UserControl.
Here is all my code...
Form1.cs
namespace Problem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
uC11.u3Visible += UC11_u3Visible;// Event came from UC1
}
private void UC11_u3Visible(object sender, EventArgs e)
{
uC31.Visible = true;
uC11.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
uC11.Visible = true;
}
}
}
UC1.cs
namespace Problem
{
public partial class UC1 : UserControl
{
public event EventHandler u3Visible;//Declaring a public event
public UC1()
{
InitializeComponent();
}
private void UC1_Load(object sender, EventArgs e)
{
UC2 xy = new UC2();
xy.Name = "UsrCntrl2";
xy.Location = new Point(18, panel1.Controls.Count * 73);
panel1.Controls.Add(xy);// Adding UC2 in UC1
xy.open += Xy_open;// UC2 button click event
}
private void Xy_open(object sender, EventArgs e)
{
EventHandler handler = u3Visible;
if (handler != null)
{
handler(this, new EventArgs());// Creating another event to make UC3 visible
//upon button click from UC2.
}
}
}
}
UC2.cs
namespace Problem
{
public partial class UC2 : UserControl
{
public event EventHandler open;//Declaring a public event
public UC2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
EventHandler handler = open;
if (handler != null)
{
handler(this, new EventArgs());// Button click sending this event
}
string text = "Hello There!";// "I want this string to pass into UC3 textbox"
var ass = new UC3(text);
}
}
}
UC3.cs
namespace Problem
{
public partial class UC3 : UserControl
{
public UC3()
{
InitializeComponent();
}
public UC3(string text)
{
InitializeComponent();
TB.Text = text;// why textbox not showing this value?
MessageBox.Show(text);// while this MessageBox showing it!
}
}
}
Any help will be greatly appreciated. Thanks in advance!

calling an child form event replacing or avoiding the mdi parent form event

In mdimain_MdiChildActivate the application logic is defined for all child forms
related to GridControl mouseDoubleClick event .
It is working fine for all grid containing child forms but in some case where grid mouseDoubleClick is defined internal for the Child Form.
So, the event is fired twice one from MdiParent and for internal part.
Is there any way such that MdiParent Parent Control event does not fire's for this mouseDoubleClick case comparing/validating the ifexist case for Child Form without changing the code in MDI form.
sample example :
private void MDIMain_MdiChildActivate(object sender, EventArgs e)
{
// code should not work
}
private void MainGridControl_MouseDoubleClick(object sender, MouseEventArgs e)
{
/// Child Form : code should work
}
This approach detects WM_NCHITTEST message sent to your MainGridControl before MdiChildActivate is fired. This can only detect if your mouse is used (Click, DoubleClick) on the MainGridControl but I think it suits in your case.
public class Form1 : Form {
public Form1(){
InitializeComponent();
Load += (s,e) => {
gridProc.AssignHandle(MainGridControl.Handle);
};
}
MainGridProc gridProc = new MainGridProc();
private void MDIMain_MdiChildActivate(object sender, EventArgs e)
{
if(gridProc.HitTestOn) { gridProc.HitTestOn = false; return; }
//code is still run if HitTestOn = false
//.......
}
public class MainGridProc : NativeWindow {
protected override void WndProc(ref Message m){
if(m.Msg == 0x84)//WM_NCHITTEST
{
HitTestOn = true;
}
base.WndProc(ref m);
}
public bool HitTestOn {get;set;}
}
}

Click event is not firing when I click a control in dynamic usercontrol

I have different controls in my usercontrols. And load usercontrols dynamically in my form
UserControl2 usercontrol = new UserControl2();
usercontrol.Tag = i;
usercontrol.Click += usercontrol_Click;
flowLayoutPanel1.Controls.Add(usercontrol);
private void usercontrol_Click(object sender, EventArgs e)
{
// handle event
}
The click event is not firing when I click a control in usercontrol. It only fires when I click on empty area of usercontrol.
Recurse through all the controls and wire up the Click() event of each to the same handler. From there call InvokeOnClick(). Now clicking on anything will fire the Click() event of the main UserControl:
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
WireAllControls(this);
}
private void WireAllControls(Control cont)
{
foreach (Control ctl in cont.Controls)
{
ctl.Click += ctl_Click;
if (ctl.HasChildren)
{
WireAllControls(ctl);
}
}
}
private void ctl_Click(object sender, EventArgs e)
{
this.InvokeOnClick(this, EventArgs.Empty);
}
}
This should solve your problem.
//Event Handler for dynamic controls
usercontrol.Click += new EventHandler(usercontrol_Click);
Take this:
this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
Because events from ChildControls are not propagated to parents. So you have to handle Click event on every child control added to UserControl.
1-define a delegate at nameapace
public delegate void NavigationClick(int Code, string Title);
2-define a event from your delegate in UserControl class:
public event NavigationClick navigationClick;
3-write this code for your control event in UserControl:
private void btn_first_Click(object sender, EventArgs e)
{
navigationClick(101, "First");
}
4-in your Windows Form than use from your UserControl at Event add:
private void dataBaseNavigation1_navigationClick(int Code, string Title)
{
MessageBox.Show(Code + " " + Title);
}

Set focus (boundary) for radiobutton

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

Categories