How to programmatically hide the keyboard - c#

I have a textbox with a button inside (Telerik's RadTextBox with an Action configured).
When the user presses the Action, a progress bas is displayed, the screen goes dark, and some magic happens.
My problem is that since the action doesn't result in the textbox losing focus, the on-screen keyboard is not hidden, and keeps covering half the screen.
I would like to programmatically hide the on-screen keyboard, but don't know how.

Just set focus to the main page:
this.Focus();
this will focus a control that doesn't use the keyboard and thus hide the keyboard. Unfortunately there is no API to the keyboard to hide it.

Instead try disabling and then enabling the textbox in question in an appropriate place (like once a query has been submitted or an action triggered):
TextBox.IsEnabled = false;
TextBox.IsEnabled = true;
(Via https://stackoverflow.com/a/23905874/1963978)
Not clean, but it does the job (in Windows 10 mobile).

here lot solution is available for a Textblock only but in my case AutoCompleteBox
<toolkit:AutoCompleteBox Name="autoComplateTxt"
Grid.Row="4"
Margin="15,5,2,10"
Padding="0"
Height="65"
Text=""
BorderThickness="1"
BorderBrush="Black"
VerticalAlignment="Center"
DropDownClosed="autoComplateTxt_DropDownClosed"
/>
private void autoComplateTxt_DropDownClosed(object sender, RoutedPropertyChangedEventArgs<bool> e)
{
this.Focus();
}

Related

Hide Virtual Keyboard from Windows App C#

I am new in UWP, I want to hide On screen keyboard which pops up on focus on textbox.I already have numeric pad to accept the input from user. How to avoid keyboard's automatic functionality.
Tried with PreventKeyboardDisplayOnProgrammaticFocus="True" and
InputPane.GetForCurrentView().Showing += (s, e) => (s as InputPane).TryHide();
but no use.
You can set PreventKeyboardDisplayOnProgrammaticFocus on TextBox to True, this can solve your problem.
Update
When the user clicks on the TextBox, the FocusState of the space is Pointer, not Programmatic, so the PreventKeyboardDisplayOnProgrammaticFocus property does not work.
This is a Hack method that achieves your purpose through visual spoofing:
<Grid>
<TextBox x:Name="HideTextBox" Width="1" Height="1" PreventKeyboardDisplayOnProgrammaticFocus="True"/>
<TextBox x:Name="ShowTextBox" GotFocus="ShowTextBox_GotFocus" IsReadOnly="True" Text="{Binding ElementName=HideTextBox,Path=Text}"/>
</Grid>
code-behind:
private void ShowTextBox_GotFocus(object sender, RoutedEventArgs e)
{
HideTextBox.Focus(FocusState.Programmatic);
}
As you can see, when ShowTextBox is set to ReadOnly, it does not trigger the virtual keyboard. When it gets the focus, we programmatically shift the focus to the "hidden" HideTextBox. At this time, the virtual keyboard will be intercepted. User-entered content can be obtained by binding.
It's not perfect, I also look forward to a better way to solve this problem.
Best regards.

What alternatives do I have to flyout/ContentDialog? (c#, xaml)

I currently have a program where you can load a text in it.
Now I created a button that Pops up a flyout/ContentDialog but Im not happy with it because Limits me of what Im trying to achieve.
When I click the button it opens a flyout, the flyout gets the full Focus. That means I cannot scroll to the text WHILE the flyout-box is open. And if I click outside the flyout-box the flyout-box disappears.
I have a similar Problem to the ContentDialog.
When I click the button and the ContentDialog Pops up, everything behind the ContentDialog goes a bit into White/Grey Color. Also the ContentDialog does not allow any Focus outside the ContentDialog itself.
So what do I want to have?
I want that when I click on the button that a Window appears. I should be able to customize the window (writing text in it and it should have a button).
While this Window is open I want to be able to do Actions outside that window without the window Closing. For example Scrolling through the text I loaded.
Is there something I can achieve this with?
Take a look at the Popup class. This will let you display content on top of other content within your app's window. It's similar to the Flyout but without all of the built-in Flyout behavior that you don't want. The Popup class documentation has more details and commentary on when and how to use it.
Here's a really bland example with no styling.
<Grid>
<Popup x:Name="popup">
<StackPanel>
<TextBlock Text="Poppity pop pop" />
<Button Click="ClosePopup_Click">Close</Button>
</StackPanel>
</Popup>
<Button Click="OpenPopup_Click">Open Popup</Button>
</Grid>
private void OpenPopup_Click(object sender, RoutedEventArgs e)
{
popup.IsOpen = true;
}
private void ClosePopup_Click(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}
There is a slightly more complicated example in the Popup documentation
I just hide and show grids with whatever I want inside.

Stop screen capture on UWP app in TaskView of Windows 10

I stop screen capturing on my UWP app by
ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false;
However, the app still shows its preview on Taskview of Windows 10
Can someone let me know how to disable the app's preview on Task view
This is the current TaskView
and this is how I need it
Capturing an app's thumbnail looks like a system behavior, and I don't know how to disable it.
But since the capture only happens when the user presses Alt+Tab or clicks the task switch button on the task bar, there is a chance to cover the app with an overlay before system takes a screen capture of the app.
First add an opaque overlay, and set its initial visibility as Collapsed.
<Grid>
<TextBlock FontSize="50" Text="Your controls here!" />
<Grid Background="Black" x:Name="overlay" Visibility="Collapsed" />
</Grid>
Then register a handler for app window's Activated event,
Window.Current.Activated += Current_Activated;
Display/Hide the overlay when window is deactivated/activated,
private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
{
overlay.Visibility = Visibility.Visible;
}
else
{
overlay.Visibility = Visibility.Collapsed;
}
}

Moving cursor to next textbox automatically in UWP

I have 8 small textboxes with a maxlength of 1, and I want to make it so when somebody types a character in one of the boxes it automatically moves the cursor to the next box without the user having to press tab or manually click the next box in win 10 UWp
Is there an easy way to do this?
it automatically moves the cursor to the next box without the user having to press tab or manually click the next box in win 10 UWp Is there an easy way to do this?
The Focus method would be that you want. Pass FocusState.Keyboard as the parameter if you’re setting focus as a result of a keyboard interaction, like a tab sequence or key press.
I have made a simple code sample for your reference:
<StackPanel Orientation="Horizontal" Height="50">
<TextBox x:Name="txb1" MaxLength="1" Width="20" TextChanged="txb_TextChanged"></TextBox>
<TextBox x:Name="txb2" MaxLength="1" Width="20" TextChanged="txb_TextChanged" Margin="10 0 0 0"></TextBox>
</StackPanel>
private void txb_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (!string.IsNullOrEmpty(textbox.Text.Trim()))
{
txb2.Focus(FocusState.Keyboard);
}
}

WPF Webbrowser control disable drop

Is there a way to disable dropping items like notepad, word etc on to a System.Windows.Controls.WebBrowser control in WPF.
I have tried various options like
AllowDrop="False" - Does not work.
Tried to catch events like Drop, PreviewDrop, DragLeave, PreviewDragLeave, PreviewDragOver, PreviewDragEnter - None of these events fire when I drop items on the WebBrowser control.
Using the constructor or Xaml to create the following method will stop the drag from changing the webbrowsers current state:
private void webBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
// The WebBrowser control is checking the Uri
if (e.Uri.ToString() != "Place your url string here") //ex: "http://stackoverflow.com"
{
// Uri is not the same so it cancels the process
e.Cancel = true;
}
}
As you mentioned, none of the Drop event are fired, and it seems like the native browser control is very limited which leaves you with two possibilities:
Either user another browser control like wpfchromium,
Or define a transparent popup in top of the browser and handle
the drop even within it
:
<WebBrowser Height="300" Width="300" x:Name="wbBrowser"/>
<Popup Opacity="1" AllowsTransparency="True" IsOpen="True" Placement="Center"
PlacementTarget="{Binding ElementName=wbBrowser}" >
<Border Background="Black" Opacity="0.01"
Width="300" Height="300">
</Border>
</Popup>
This solution ain't pretty at all and needs also more logic to handle when the popup's parent control moves..
...you may also find this solution helpful, if your main content is html:
How to disable drop on wpf webbrowser control

Categories