How to specify allowed characters in keyboard? - xamarin

I want to create a page for pin entry for both, android and iOS platforms. Numeric specification in Keyboard property is close to my needs. I can do something like this to restrict allowed characters and overall length. However I need to get rid of the dot character on the keyboard. How can I achieve that?

You can remove the dot from the soft keyboard.
Using the solution you linked and Keyboard="Numeric", you can use the same TextChanged event that restricts entry text size to restrict the '.'.
Example:
public void OnTextChanged(object sender, TextChangedEventArgs args)
{
var e = sender as Entry;
string val = e.Text;
if (string.IsNullOrEmpty(val))
{
return;
}
if (MaxLength > 0 && val.Length > MaxLength)
{
val = val.Remove(val.Length - 1);
}
if (val.Contains("."))
{
val.Replace(".", string.Empty);
}
e.Text = val;
}
Other option would be creating a Grid for the PIN. And show the PIN in a Label instead of and Entry to avoid pasting.

Related

how to select index in a combobox using DOWN ARROW key?

I want this functionalitymy combobox has multiple values. for example: RJ11, RJ12, RJ13 and so on.
AutoCompleteMode set to append and AutoCompleteSource set to listItems. all i want is when i type rj in combobox it will suggest me rj11 in combobox's text field and when i press DOWN ARROW key it should select rj11 but what it is doing is it keep searching for next possible match.
i tried :
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Down)
{
comboBox1.SelectedIndex = comboBox1.FindStringExact(comboBox1.Text);
}
}
but it is not working.
i am very new to winform and programing.
please help.
and yeah sorry for my bad english.
You can try to refer to the following code to select the index in a combobox by using down key.
We can override the ProcessCmdkey method in your form.
Code:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((this.ActiveControl == comboBox1) && (keyData == Keys.Down))
{
comboBox1.SelectedIndex = comboBox1.FindStringExact(comboBox1.Text);
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}

KeyUp and KeyDown events

I'm working on a typing program to learn touch typing and increase speed.
The program generates random words in the textbox (txtsrc) and the user must type the word in another textbox(txtinput) everything is going right until I stuck in this problem: I want to compare every char in txtinput when the user hit the key down if the key is correct to keep going if not change the color of char in txtsrc to red.
how can I allow the user to use the backspace to remove his errs
and how to compare the chars while the user input the text
I can't find the right algorithm please help
private void txtinput_KeyUp(object sender, KeyEventArgs e)
{
if (keycount < 0) { keycount++; }
if (e.KeyCode == Keys.Back)
{
--keycount;
txtsrc.Select(keycount, 1);
txtsrc.SelectionColor = Color.Black;
}
if (keycount >= 0 && txtinput.Text.Length > 0)
if (txtsrc.Text[keycount] != txtinput.Text[txtinput.Text.Length - 1])
{
txtsrc.Select(keycount, 1);
txtsrc.SelectionColor = Color.Red;
}
if (e.KeyCode != Keys.Back)
{
keycount++;
label1.Text = "Keycount: " + keycount.ToString();
}
}

How could I choose one particular file to load with loadStrings

The title is explicit enough, I want to let the user choose the text file he want to open.
I do not know if there is an explorer or an input field already implemented on processing.
Any help would be great.
Use selectInput. From the Processing reference:
Opens a platform-specific file chooser dialog to select a file for input. After the selection is made, the selected File will be passed to the 'callback' function. If the dialog is closed or canceled, null will be sent to the function, so that the program is not waiting for additional input. The callback is necessary because of how threading works.
I've modified the example sketch they provide in the reference to include loading the file with the loadStrings method.
String[] txtFile;
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
// load file here
txtFile = loadStrings(filepath);
}
}
There is no Implemented method, but you could could make a buffer and monitor key presses like so:
String[] File;
String keybuffer = "";
Char TriggerKey = Something;
void setup(){
//do whatever here
}
void draw(){
//Optional, to show the current buffer
background(255);
text(keybuffer,100,100);
}
void keyPressed(){
if(keyCode >= 'a' && keyCode <= 'z'){
keybuffer = keybuffer + key;
}
if(key == TriggerKey){
File = loadStrings(keybuffer + ".txt");
}
}
when triggerkey is pressed, it loads the file

How to stop pivot looping

I want to create a wizard control from the pivot control. To achieve this I need to stop the pivot looping. I want to stop the pivot control moving forward from the last item to the first and backwards from the first to the last.
I'm pretty sure I should be able to intercept the manipulations e.g. cancel if I detect a right to left manipulation on the last pivot item. I can capture this in ManipulationDelta but don't know how to cancel the manipulation.
I have tried setting e.Handled = True but it didn't work.
I tried to set IsHitTestVisisble to false but this kills all manipulations. I tried setting it back to true in ManipulationCompleted but this then allows all manipulations.
Any ideas?
Cheers
Steve
The pivot is not designed to be used as a wizard and does not support stopping it's looping behaviour as this would create an inconsistent UX for users.
If you really must create a wizard do it with multiple pages.
Don't use a Pivot for a Wizard. Create your own transitions instead.
I couldn't reply to your comment on Matts answer but I just wanted to point you to this:
http://forty3degrees.wordpress.com/2011/07/19/creating-a-swipable-contentcontrol/
It's the last entry in my very neglected blog and should provide a good base for creating a wizard using a pivot style swipe.
Calum.
EDIT: I tried to do what you wanted with the pivot but couldn't find a way to stop it looping. The only way that I can think of to achieve this would be to derive a custom control from Pivot. Unfortunately SelectedIndex/SelectedItem are not virtual so you would need to hide them (with the new modifier) and reproduce the logic from the base class.
It's just an alternative solution I've posted here - you can try to make use of XNA framework TouchPanel and Touch.FrameReported Event:
using Microsoft.Xna.Framework.Input;
public MainPage()
{
InitializeComponent();
myPivot.IsHitTestVisible = false; // disable your Pivot
Touch.FrameReported += Touch_FrameReported;
TouchPanel.EnabledGestures = GestureType.HorizontalDrag;
}
TouchPoint first;
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint mainTouch = e.GetPrimaryTouchPoint(this);
if (mainTouch.Action == TouchAction.Down)
first = mainTouch;
else if (mainTouch.Action == TouchAction.Up && TouchPanel.IsGestureAvailable)
{
if (mainTouch.Position.X < first.Position.X)
{
if (myPivot.SelectedIndex < myPivot.Items.Count - 1)
myPivot.SelectedIndex++;
}
else if (mainTouch.Position.X > first.Position.X)
{
if (myPivot.SelectedIndex > 0)
myPivot.SelectedIndex--;
}
}
}
Thought it would probably work from WP7.1 as TouchPanel is available from that version of the OS.
If you absolutely want to keep the Pivot from looping, here is a quick and dirty hack:
int previousSelectedIndex = 0;
public PageWithPivot()
{
InitializeComponent();
pivot.SelectionChanged += new SelectionChangedEventHandler(pivot_SelectionChanged);
}
private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (pivot.SelectedIndex == 0 && previousSelectedIndex == <number Of screens - 1>)
pivot.SelectedIndex = <number Of screens - 1>;
previousSelectedIndex = pivot.SelectedIndex;
}
This causes your PivotControl to jump back to the last pivotItem. Not very pretty but works.
This is so weird because it only works in the Emulator. I guess you shan't mess with the UI
You can use MVVM:
<phone:Pivot Foreground="Black"
Name="pivot1"
Title="AIDE"
SelectedIndex="{Binding SelectedItem, Mode=TwoWay}">
Cs:
private class HelpViewModel : ViewModelBase
{
public HelpViewModel()
{
}
private int _SelectedItem = 0;
public int SelectedItem
{
get
{
return _SelectedItem;
}
set
{
if (_SelectedItem != value)
{
if (value == 3)
_SelectedItem = 0;
else
_SelectedItem = value;
RaisePropertyChanged(() => SelectedItem);
}
}
}
}
public AppHelpPivot()
{
InitializeComponent();
LayoutRoot.DataContext = new HelpViewModel();
}

Hiding the keyboard when a control loses focus?

now I have this:
public void focusChanged(Field field, int eventType) {
if ( field == txtAmount && eventType == 1)
{
getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE);
}
}
Now my problem is that the keyboard isn't hiding. I think the error is in the eventType parameter. What number identifies a LostFocus event? I hard coded in '1' for tests but it doesnt seem to work.
FocusChangeListener focusListener;
//In the constructor:
txtAmount = new EditField(Field.FIELD_RIGHT);
txtAmount.setFocusListener(focusListener);
public void focusChanged(Field field, int eventType) {
if ( field == txtAmount && eventType == 1)
{
Dialog.alert("iasdi");
getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE);
}
}
why isn't this working? is there an enum or something that i can use to choose what eventType I should react to?
Also, if I remove the event type (so that the code executed regardless of the action just when focus changes right? nothing happens the dialog I put in for show, doesn't display meaniing the event is never entered. Any suggestions?
thanks
eventType can be one of next constants, declared in FocusChangeListener class:
public static final int FOCUS_GAINED = 1;
public static final int FOCUS_CHANGED = 2;
public static final int FOCUS_LOST = 3;
!!! Use FOCUS_LOST = 3 instead of FOCUS_GAINED = 1 to handle focus lost event.
Also, check for null getVirtualKeyboard() method returning value, because it returns null on touch devices without virtual keyboard (like Bold 9700).
public static void hideVirtualKeyboard() {
if (net.rim.device.api.ui.VirtualKeyboard.isSupported()) {
Screen screen = UiApplication.getUiApplication().getActiveScreen();
if (null != screen) {
net.rim.device.api.ui.VirtualKeyboard vk = screen
.getVirtualKeyboard();
if (vk != null) {
vk.setVisibility(net.rim.device.api.ui.VirtualKeyboard.HIDE);
}
}
}
}
For matching the eventType, try using the constants defined in FocusChangeListener instead of hard coding "1". In this case, you probably want to use FocusChangeListener.FOCUS_LOST.
As for the case of the code not running, are you actually setting the value of the "focusListener" variable? From the code you posted, you aren't and it will just be passing as "null" into setFocusListener().

Resources