Do NOT hide soft keyboard while tapping controls in WP - c#

I have this piece of code for button in my Windows Phone 8.1 Store App project (not the Silverlight):
private void CursorRightButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(QueryTextBox.Text)) return;
QueryTextBox.Focus(FocusState.Keyboard); //also i tried FocusState.Pointer
QueryTextBox.Select((TextBox.SelectionStart + 1) % (TextBox.Text.Length + 1), 0);
}
As you can see, I am tring to move cursor to right in text programmatically and the problem is that it hides soft keyboard and then shows it again after tapping button. I need to have keyboard on while tapping this button.
I tried to tinker with Focus() methods for sender and TextBox objects but I couldn't find any possible solution.
So the question is, how do you force keyboard not to loose focus/not to hide while tapping on controls?

I found out with Sajeetharans help that I need to set IsTabStop value on controls to false. Then keyboard will stay there. I did it in constructor of my page like this
public MainPage()
{
InitializeComponent();
CursorLeftButton.IsTabStop = false;
CursorRightButton.IsTabStop = false;
}
and my button method
private void CursorRightButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(TextBox.Text)) return;
TextBox.Select((TextBox.SelectionStart + 1) % (TextBox.Text.Length + 1), 0);
}

Add a loaded event to your container say grid,
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
set focus on the control , say a textblock
Txtblock1.Focus();
}
How To Programmatically Dismiss the SIP (keyboard)

Related

Show On Screen Key Board on mouse left button down on PasswordBox and should Hide when click outside the box

Platform: C# WPF
Environment: Visual Studio 2013
Question # 1: I want to show third party on screen keyboard on mouse left button down on PasswordBox control of C# WPF. I used the following code:
private void PasswordBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe");
}
But it does not start on screen key board. Instead it triggers on MouseDoubleClick and GotFocus events.
Question # 2:
I want to Hide on screen keyboard when mouse click outside the PasswordBox and Show again on mouse left button down inside box.
Question # 3:
I want to show keyboard on single click instead of mouse double click
You could handle the PreviewMouseLeftButtonDown event for the parent window. Something like this:
bool isVisible = false;
PreviewMouseLeftButtonDown += (ss, ee) =>
{
if (!passwordBox.IsMouseOver && isVisible)
{
System.Diagnostics.Process.GetProcessesByName("CCOnScreenKeyboard")?.FirstOrDefault()?.Kill();
}
else if (!isVisible)
{
System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe");
isVisible = true;
}
};
I belive the best way to do this will be through using the Focus events, as you only want the keyboard when you're interacting with the PasswordBox, and for it to go once you have stopped interacting.
private void PasswordBox_GotFocus(object sender, RoutedEventArgs e) =>
Process.Start("D:\\CCOnScreenKeyboard.exe");
private void PasswordBox_LostFocus(object sender, RoutedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("CCOnScreenKeyboard"))
process.Kill();
}

ComboBox show dropdown menu on text selection

I want to show the list of items in a combo box when the user selects the text. I have a touch screen application and it's very difficult to hit the dropdown arrow so I figure I'd show the menu when the text is selected which is often what gets touched. I'm using VS 2008. and suggestions for a touch friendly numeric up down solution in VS2008?
You could use the ComboBox.Click event handler and the ComboBox.DroppedDown property and do something like this:
private void ComboBox1_Click(System.Object sender, System.EventArgs e)
{
ComboBox1.DroppedDown = true;
}
You could also use the same event handler for a numericUpDown and use the mouseposition as well as the position and height of the NumericUpDown to get whether or not the click was above or below the halfway-line of the control by doing something like this (not sure if my math here is perfect, but it worked when I tested it):
if ((MousePosition.Y - this.PointToScreen(NumericUpDown1.Location).Y < NumericUpDown1.Height / 2))
{
NumericUpDown1.Value += 1;
}
else
{
NumericUpDown1.Value -= 1;
}
HTH
I was working on a similar situation. We wanted to make the text area behave the same as the button on the right. (IE the user clicks and gets the drop down box)
davidsbro is similar to what I ended up doing, but we wanted it to close if they clicked again, so the value became dropDown.DroppedDown = !dropDown.DroppedDown;.
The issue with this is that if the user clicks the right button of the drop down box, the dialog box opens, then calls the onClick event.
I solved this situation by tracking the original state via the onmouseover event. If the value has changed, we have to assume that the button on the select box handled the click already.
private bool cbDropDownState = false;
private void dropDown_MouseEnter(object sender, EventArgs e)
{
cbDropDownState = dropDown.DroppedDown;
}
private void dropDown_Click(object sender, EventArgs e)
{
if (dropDown.DroppedDown == cbDropDownState )
dropDown.DroppedDown = !dropDown.DroppedDown;
}

Mouse up event doesn't get triggered

I have the following problem: I have a panel which has a specific color, say red.
When the user presses his mouse, the color of this panel gets stored in a variable. Then the user moves, his mouse still pressed, over to another panel. When he releases the mouse there, this panel should get the background color of the first that had been stored in the variable. My code looks something like this:
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
I need to identify the sender first because there are many possible panels that can trigger this event. The first MouseDown method works totally fine, the color is stored nicely in the variable. The secon one however doesn't even get triggered when the user does what I described above. When the ser clicks on the second panel, it works (there is an MouseUp part in a click aswell I guess).
What's wrong here? Why is the event not triggered when the user holds the mouse key down before?
(This answer assumes you are using Windows Forms.)
It could be that you need to capture the mouse by setting this.Capture = true in the MouseDown of the source control. (See Control.Capture)
If you did that, the source window would get the MouseUp event, and it would be the source window that had to determine the destination window under the mouse coords. You can do that using Control.GetChildAtPoint() (see this answer on Stack Overflow).
Use Windows Forms Drag and Drop Support Instead! <- Click for more info
I'm going to suggest you bite the bullet and use the .Net Drag and Drop methods to do this. It requires some reading up, but it will be much better to use it.
You start a drag in response to a MouseDown event by calling Control.DoDragDrop().
Then you need to handle the Control.DragDrop event in the drop target control.
There's a few more things you might need to do to set it up; see the Control.DoDragDrop() documentation for an example.
(For WPF drag and drop support, see here.)
when your mouse enter the target control , mouse down triggerd ang get target BackColor! you need add an boolean flag to your code :
public Color currentColor;
bool flag=false;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
if(flag==false)
{
flag=true
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
}
//assume mouse up for panles
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
if(flag==true)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
flag=flase;
}
}
and also you need change your flag in mouseMove( if )
As I mentioned in my comment Mouse Events are captured by the originating control, You would probably be better off using the DragDrop functionality built into Windows Forms. Something like this should work for you. I assigned common event handlers, so they can be assigned to all of your panels and just work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
((Control)sender).DoDragDrop(((Control)sender).BackColor,DragDropEffects.All);
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Control)sender).BackColor = (Color)e.Data.GetData(BackColor.GetType());
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I know it's an old question but I had the same issue and none of the above answers worked for me. In my case I had to handle the MouseMove event in the target control and check for the mouse to be released. I did set 'BringToFront' on my target panel just in case that helped at all.
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void panelTarget_MouseMove(object sender, MouseEventArgs e)
{
//the mouse button is released
if (SortMouseLocation == Point.Empty)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
}

Changing theme calls UserControl_Loaded event

I am not getting why behavior of WPF user control and Windows forms user control is different . I added window loaded event which just shows message box as :
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Main Window Loaded","WPF");
}
Also I created one user control and added loaded event as :
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("User Control Loaded.","WPF");
}
I have put this user control in main window.
When i launch this , I get both message box, User controls as well as windows.
Now , When i change my theme from Aero to any High contrast then user control's message box is shown again.
Why this is happening ? Why this is different from Windows form ? What should I do to avoid showing it multiple times ?
Wajeed
You could have a boolean field which stores the state of whether the dialog was shown yet or not. If you change the theme the UI-elements will reload so naturally the event will fire again.
if (!_diagWasShown)
{
_diagWasShown = true;
//Show dialogue
}
you can create bool variable, which will indicate if MessageBox was shown.
bool isUserMessageBoxShown = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (!isUserMessageBoxShown)
{
MessageBox.Show("User Control Loaded.","WPF");
isUserMessageBoxShown = true;
}
}

Make shortcuts appear in multiple menus

I am using a TextBox in a custom UserControl that I am creating. It seems that the default contextmenu doesn't show the shortcuts for Cut, Copy, Paste. This is fine, as long as they are just working.
But my Form using the UserControl has a MenuStrip that contains these default shortcuts as well. But the Cut, Copy, Paste commands are not working anymore, now that the shortcuts are assigned to the MenuStrip.
How can I use shortcuts at multiple positions in my forms? What is the best way to pass a global command like Cut and post it deeper into my UserControl? And is it possible to add the shortcuts to the default contextmenu of the textbox?
A MenuStrip item or ToolStrip item doesn't change the focus when it is clicked or its shortcut keystroke is pressed. Which is the ticket to implementing this functionality, the form's ActiveControl tells you which control has the focus. You just need to check if it is a TextBox. Like this:
private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
var box = this.ActiveControl as TextBoxBase;
if (box != null) box.Copy();
}
Do the same for the Paste() and Cut() methods. You can further enhance the UI by selectively enabling these menu/toolbar items by subscribing to the Application.Idle event and checking if the ActiveControl is a text box and the text box or clipboard contains any text. Like this:
public Form1() {
InitializeComponent();
Application.Idle += Application_Idle;
}
protected override void OnFormClosed(FormClosedEventArgs e) {
Application.Idle -= Application_Idle;
base.OnFormClosed(e);
}
void Application_Idle(object sender, EventArgs e) {
var box = this.ActiveControl as TextBoxBase;
copyToolStripMenuItem.Enabled = box != null && box.Text.Length > 0;
cutToolStripMenuItem.Enabled = copyToolStripMenuItem.Enabled;
pasteToolStripMenuItem.Enabled = box != null && Clipboard.ContainsText();
}
You have to just enabled the property of textbox for the same and it will start responding.
Just verify that myTextBox.ShortcutsEnabled = TRUE;

Categories

Resources