Windows Store App Textbox keeps losing focus - c#

I have a simple application that has a TextBox and a Canvas. When the user touches the Canvas, I want to move the focus to the TextBox. So, on the PointerReleased event, I got:
private void canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
I also subscribed to the TextBox GotFocus and LostFocus and noticed that, immediately after the TextBox got focused, it looses the focus. Why is it loosing focus and how can I solve it?
Thanks in advance.

This will work for me.
private void Canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
private void textBoxMain_GotFocus(object sender, RoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}

Related

C# Form - MouseClick will click the actual control after losing form focus

If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.

c# how to catch mouse event outside textbox

i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}

C# mousedown event continously press key

okay so what I want to do, it when the mouse is HELD DOWN I want it to continously press a key. it should continously press this key until I let off.
Imagine if you will, a left and right button on a windows form,
then by clicking and holding the right button, the letter "R" displays on a textbox continously until you release the button. What I am doing has very little to do with that scenario, but you get what I'm trying to do here.
What exactly do I put in the mouse down to keep the sendkeys going forever without locking up the application?
I hope my question makes sense. lol.
Thanks
RT
private void pictureBoxKeyboard_MouseDown(object sender, MouseEventArgs e)
{
//something goes here
}
This is worth a read...
http://msdn.microsoft.com/en-us/library/ms171548.aspx
SendKeys.Send("r")
This might just fire once, you may want to attach a timer that starts on the MouseDown event and stops on the MouseUp event. Then you could put the SendKeys in the Timer.Tick event.
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 500;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
this.Text = "moose-Up";
}
private void button1_MouseDown(object sender, EventArgs e)
{
timer1.Start();
this.Text = "moose-Down";
this.richTextBox1.Select();
}
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send("r");
Debug.Print("tickling");
}
Select the control that you wish to receive the SendKeys value...

Click event in blank area of TreeView

To determine the content of my context menu I need to detect when the user clicks into the blank area of a treeview.
But, neither the Click nor the MouseClick events fire.
Is it possible to achieve the desired functionality by deriving from TreeView?
If not, what can I do?
You can use the MouseDown event, that fires even in the empty area.
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
// MessageBox.Show("treeView1_MouseClick");
}
private void treeView1_Click(object sender, EventArgs e)
{
// MessageBox.Show("treeView1_Click");
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("treeView1_MouseDown");
}

How can I make an event for getting focus for double click on textbox? (Windows Phone)

I have a textBox and I call an event get focus, when click on it. The behaviour is different when I make a double click on it, how can I make an event for getting focus for double click on this textbox?
You can use the OnTap() and OnDoubleTap() methods of the TextBox. And in each method you can define the different logic and set the focus on the TextBox.
Update:
Here's a simple code structure on how to make it work:
XAML:
<TextBox x:Name="InputTextBox" Margin="0,0,0,520" />
C#
public MainPage()
{
InitializeComponent();
InputTextBox.Tap += InputTextBoxTap;
InputTextBox.DoubleTap += InputTextBoxDoubleTap;
}
private void InputTextBoxDoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Double tapped!";
}
private void InputTextBoxTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Tapped!";
}
I tested this on both the emulator and on a device and it works in both cases!
The reason is simple.
If you have notice the arguments supplied are different
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void textBox1_Click(object sender, EventArgs e)
{
}
Yes if you want them to resemble the same you can select "MouseClick" event from the properties.
Cheers!

Categories