How do always display the Numeric keyboard in windows phone 8? - c#

I'm working on windows phone 8 app, I had a page which inputs number for that I gave code like this,
<TextBox Name="txtNumber" Height="Auto" Margin="0,10,0,510" >
<TextBox.InputScope>
<InputScope>
<InputScopeName NameValue="Number" />
</InputScope>
</TextBox.InputScope>
</TextBox>
by the above code; It display the numeric keyboard when I place the cursor to type; But I need a fixed keyboard which is always visible and if we type it has to enter the value to the textbox.
Would somebody please tell me how to do that.

Try this on for size:
Xaml:
<Grid
x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0"
Loaded="ContentPanel_Loaded">
<TextBox
Name="TB1"
HorizontalAlignment="Left"
Height="72"
Margin="0,74,0,0"
VerticalAlignment="Top"
Width="456"
InputScope="Number"/>
</Grid>
Code:
private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
{
// Turn on Tab Stops. You can set this in XAML as well.
this.IsTabStop = true;
// Set focus on the TextBox.
TB1.Focus();
}
It will spark up the SIP as it enters the <TextBox> ready for input. Hope it's what your looking for.
Got it from this MSDN blog.

The easiest way would probably be with creating your own user control. However it is most likely a lot of work to get it to work as a normal keyboard.
http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx
Or maybe perhaps this will help http://www.silverlightshow.net/items/Windows-Phone-7-Creating-Custom-Keyboard.aspx

Related

Formatted Textboxes for UWP

I was wondering if anybody had any recommendations on how I could format a textbox for time inputs(hh:mm:ss) as well as Coordinates in Degrees,Minutes, Seconds (dd°mm'ss"). I am well aware that you can set the format of a string by the String.Format() method.... this does give me the desired layout in the textbox, but I want a control that has the appropriate symbols locked in and will autotab over those symbols as the user gives their inputs.
I have been searching for a way to do this in C#/UWP for a while with no luck, if anybody has any suggestions or could point me in the right direction it would be greatly appreciated.
The UWP Community Toolkit has some very good controls which are easy to integrate and one of them is the TextBox Mask control which is ideal for your scenario.
You can also try out the TextBoxRegex control which is not quiet what you want but is indeed something you should check out before proceeding.
Please reach out in case you are having difficulty in integrating the control for your specific requirement.
Optionally, you can download the sample app for the UWP toolkit to know about all the available controls /Tools. (UWP Community Toolkit Sample App)
EDIT 1:
As you might have noticed, the default behaviour for tab is that it sets the focus to the next control. Thus, having a single control will not be ideal since on press of tab, your textbox would lose focus and you would need to update the text and set the focus back to your textbox.
A relatively simpler way would be the following:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1">
<StackPanel.BorderBrush>
<SolidColorBrush Color="{ThemeResource SystemBaseHighColor}"/>
</StackPanel.BorderBrush>
<TextBox x:Name="hour" MaxLength="2" BorderBrush="{x:Null}" VerticalAlignment="Center" HorizontalAlignment="Center" LostFocus="hour_LostFocus"></TextBox>
<TextBlock Text=":" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="minute" MaxLength="2" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center" LostFocus="minute_LostFocus"></TextBox>
<TextBlock Text=":" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="second" MaxLength="2" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center" LostFocus="second_LostFocus"></TextBox>
</StackPanel>
This is basically your custom build control/usercontrol. So that when user tabs out it moves to the next textbox inside the stackpanel and so on.
In your hour_LostFocus event you can validate the textbox's current value and append 0 and do other validations .
private void hour_LostFocus(object sender, RoutedEventArgs e)
{
string val = (sender as TextBox).Text;
Regex regex = new Regex(#"^([0[0-9]|1[0-9]|2[0-3])$");
Match match = regex.Match(val);
if (!match.Success)
{
//append 0 and other validations
}
}
The same applies for minute and seconds .. just that the regex would be different in these cases.

Windows Phone page navigation does not work

I'm new in creating apps for Windows Phone. I've got problem with redirecting to another page. I've created blank page with HyperlinkButtonand in .cs file I wrote this:
private void but_elf_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Elfy));
}
In xaml:
<HyperlinkButton x:Name="but_elf" Content="Elfy"
HorizontalAlignment="Center" Margin="100,125,100,255" Grid.Row="1"
VerticalAlignment="Center" Width="200" Height="70" />
When I launch app and click on the button - nothing happens. There are no errors, no messages. I've tried to put NavigateUri in button's property but after pressing button (in launched app) the message has shown: "You need to install an app for this task. Would you like to search for one in the Store?" After pressing "Yes" the app says: "Sorry, no apps found".
How to figure out this problem? I'm creating app for Windows Phone 8.1 in .NET Framework 4.5. Thanks for any help.
You are missing a reference for the 'Click'-event handler. Please change your XAML to this:
<HyperlinkButton x:Name="but_elf" Content="Elfy"
HorizontalAlignment="Center" Margin="100,125,100,255" Grid.Row="1"
VerticalAlignment="Center" Width="200" Height="70" Click="but_elf_Click" />
Please see:
C# Documentation for Click on MSDN
C# Documentation for HyperlinkButton on MSDN

Windows Phone Listbox Selection to Textbox

Question:
What is the simpliest way for pre-defined items in a Listbox to be populated to a TextBox (Preferably single selection) In a Windows 7 Phone Enviroment?
Example: I select "Bob" from the list box and then "Bob" is then displayed in the text box instantly so the user doesn't need to type in a Username, and can instead use a pre-made username from the list of Usernames.
Problems:
I've tried looking for these little things and found no solutions.
The solutions I did find are incredibly vague.
Many videos on YouTube and elsewhere haven't really covered this for Windows 7
I fear Windows 8 is different.
Right now, my code fails to populate the Textbox with the Listbox Selection.
Current Code:
<TextBox HorizontalAlignment="Left" Margin="75,175,0,488" Name="textBox1"
Text="{Binding ElementName=listBox1, Path=SelectedValue}" Width="298" FontSize="20"
BorderBrush="Transparent" OpacityMask="#6E030303" Foreground="#FFEBC285" Background="Black"
FontFamily="Trebuchet MS" MaxLength="7">
In your XAML you can do something like this
<ListBox name="lb"/>
<TextBox Text="{Binding ElementName=lb, Path=SelectedValue}"/>
What this does is it databinds the TextBox.Text property to the ListBox.SelectedValue property`. The Textbox should populate when you select new items in the listbox.

WPF Newbie from Forms: Basic Events understanding

Even though late to the party, our company is finally moving slowly but surely to WPF for our desktop programming needs.
I have a few questions I was hoping this community could help me with... b/c I am finding even though I am reading all the tutorials in the world, that with WPF, there are so many different ways to approach the most basic things you used to do with Forms, that I just need a 101...
For instance...
Can anyone please enlighten a frustrated WPF newbie why this BASIC code is not working?
private void BtnImport_Click(object sender, RoutedEventArgs e)
{
//Button disabled on it's own without below routines
BtnImport.IsEnabled = false;
// So does textbox which updates on it's own without below routines
TxtTest.Text = "Started at : " + DateTime.Now.ToString() + "\n";
//Bunch of routines that each run in their own loops
}
When I try to disable the button and update the textbox along with those routines mentioned.. the routines run JUST FINE.. .but the button NEVER gets disabled nor does the textbox get updated?
I have a feeling I still am not understanding how WPF does things in Bindings, but I was hoping for a light bulb moment from here to just point me out WHY the basics don't work here?
Thanks~
Thank you so much. here is the XAML. #Joe I totally get that I am not using bindings, and I think that is a CORE of my confusion, if I am setting it directly, why is it not working when other routines are running after I directly set a property? (and my novice thought was that it was bindings). In any case, as requested here is XAML code.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="#FF8FB1B1" Name="AnalyticsWindow" Loaded="AnalyticsWindow_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="74*" />
<ColumnDefinition Width="429*" />
</Grid.ColumnDefinitions>
<TextBox Height="23" Margin="66,62,187,0" Name="TxtTime" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<Button Content="Import" Height="23" HorizontalAlignment="Left" Margin="141,179,0,0" Name="BtnImport" VerticalAlignment="Top" Width="75" Click="BtnImport_Click" Grid.Column="1" />
<DatePicker Height="25" HorizontalAlignment="Left" Margin="64,108,0,0" Name="ObjDateFrom" VerticalAlignment="Top" Width="115" Grid.Column="1" />
<DatePicker Height="25" HorizontalAlignment="Left" Margin="218,108,0,0" Name="ObjDateTo" VerticalAlignment="Top" Width="115" Grid.Column="1" />
<Label Content="Log" Height="28" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="234,28,39,0" Name="label2" VerticalAlignment="Top" Width="156" Grid.Column="1" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="218,62,0,0" Name="TxtTest" VerticalAlignment="Top" Width="182" Grid.Column="1" />
</Grid>
Based upon what you wrote, the code is actually working, but not giving the UI 'enough time to breath' so that your changes can be rendered on the user surface. Consider changing to this...
Dispatcher.BeginInvoke((Action) (() => {
BtnImport.IsEnabled = false;
TxtTest.Text = "Started at : " + DateTime.Now.ToString() + "\n";
}));
with a symmetrical BeginInvoke at the end of your intensive processing so that the button is enabled again. This will mitigate your problem and help you get the behaviour you are after.
I would also suggest that the parts stated as...
//Bunch of routines that each run in their own loops
...are candidates for the WPF BackgroundWorker or passing the code off to a Task<> that you can await upon. Doing this will keep the UI responsive and let you enable/disable buttons and update text blocks in 'real-time'.
The BackgroundWorker or Task<> is the so-called 'golden solution' if the UI thread is totally starved out. There are a zillion samples out there you can easily cut and paste into your code. I recommend the Task<> approach because it is not reliant upon WPF namespaces.
In its simplest form, you can do this...
Task.Run(() =>
{
// lots of business processing code here
});
And synch up your stuff using await...

c# WPF how to produce a flashy warning

I'm learning WPF, so bear with me.
I would like to have my WPF application flash in the user's face if a certain event is fired.
What is the best way to "notify" the user? I really want the user to react!
Cheers, Patrick
Environment: Windows7/64bit/.Net4
If you want the user to react you can force them to by simply opening a modal dialogue. The most lightweight of which being the MessageBox. You can also create normal modal windows using their ShowDialog method, you can make those windows as "fancy" as you want by getting rid of their normal appearance. This is achieved by setting the WindowStyle to None and AllowsTransparency to true, this will remove all the frame elements, so the window is now pure content.
Popups are handy for non-modal notifications and they already are content-only, but setting their AllowsTransparency to true may also be desired if you want rounded corners for example.
Best is entirely subjective and depends on many context variables but here is how I do it MVVM style.
In your main view model, define a property
pubic ObservableCollection<AlertViewModel"> Alerts { get; private set; }
in my case the AlertViewModel has only a "Message" property and a "Dismiss" RelayCommand.
In the XAML of your main view add
<Grid>
<all of my other other view controls>
<ItemsControl x:Name="AlertsControl" Opacity="50" ItemsSource="{Binding Alerts}"/>
</Grid>
Make sure it is the last item in the main container of your main view. This ensures it has the highest z order and will appear on top of all other controls.
Here is the data template for this view model
<DataTemplate DataType="{x:Type vm:AlertViewModel}">
<Border CornerRadius="10" Margin="3" Background="Red">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Margin="10" Grid.Column="0"
Command="{Binding ElementName=theWindow, Path=DataContext.DismissAlarmCommand}"
CommandParameter="{Binding}">Dismiss</Button>
<TextBlock Foreground="White" FontWeight="ExtraBold" Grid.Column="1"
Text="{Binding Message}" FontSize="20"
VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
</Grid>
</Border>
</DataTemplate>
Now,
Alerts.Add( new AlertViewModel() { Message = "Danger Will Robinson! Danger!" } );
Will pop a Bright red alert box onto the top of your main form. It does not go away until the user presses "Dismiss"
If you want it to flash or fade in and out or bounce up and down you can add animation in the data template.
You can use a Converter or data to Enable/Disable the rest of the controls in the app byt binding to AlertsControl.HasItems
Good luck.

Categories

Resources