Using an English only keyboard in a Xamarin.Forms Entry view - xamarin

Pretty much what the title says.
I have a page with an Entry view in it in my Xamarin.Forms app.
Clicking it brings up the software keyboard which, under all text based keyboard settings(such as url, email, text, etc.), has the option to change the input language.
My problem is that my backend accepts English only, and that's the way I want it as well, but I just can't find a way to disable the language choice on the keyboard.
Does anyone know?
I'm using Xamarin.Forms, and the above description relates to usage on an Android device, I'm yet to test on iOS.
I'm hoping for a cross platform solution that I can use in the PCL, but a solution using custom renderers is acceptable too if no XForms solution exists.
Thanks!

You can do something else to enable only english characters - you can hook to the text changed event of the entry and check inside if the text is english or not.
You can do something like this:
private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)
{
var newText = ((Entry)sender).Text;
// Check for english characters.
...
// If not english, put the old value
((Entry)sender).Text = e.OldTextValue;
}

Related

Block Soft Panel Input language switch

How to make the on-screen keyboard use only English, even if the settings are selected some other languages ​​besides English?
Sorry, you cannot change the keyboard language in code on WP7.
Update
I did a bit of research on pulling the current language (Locale) from the app thread and came up with this post on in-app language selection in code. This looks like it was designed to localize text, however, I have a hunch the keyboard language will follow suit. Good Luck!
http://dotnetbyexample.blogspot.com/2011/07/mvvmlight-based-language-selection-for.html
Thread.CurrentThread.CurrentUICulture = new CultureInfo(CurrentLanguage.Locale);
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;

Win32 custom message box

I want to make a custom message box. What I want to customize is the button's text.
MessageBoxW(
NULL,
L"Target folder already exists. Do you want to overwrite the folder?",
L"No title",
MB_YESNOCANCEL | MB_ICONQUESTION
);
I'd like to just change the buttons text to Overwrite, Skip, Cancel.
What's the most simple way?
I have to make this as having same look and feel with Windows default messagebox.
As said by others, a typical way is to create a dialog resource and have a completely independent dialog, which GUI you need to design in the way that it looks like standard dialog (to meet your request for feel and look). If you want to accept text messages, you might probably need to add code which resizes the window appropriately.
Still, there is another option for those who feel like diving into advanced things. While MessageBox API does not offer much for fint tuning, you still have SetWindowsHookEx in your hands. Having registgered the hook, you can intercept standard MessageBox window procedure and subclass it in the way you like.
Typical things include:
changing button text
adding more controls
adding timed automatic close
Hooking standard window can do all of those.
UPD. Hey, I realized I have some code with SetWindowsHookEx to share: http://alax.info/blog/127
You could create an own dialog. Or you could use a window hook as described in this article.
An archived version of the article can be found on web.archive.com.
Make a dialog resource (with a GUI editor, or by hand) and call DialogBox on it. There's no way to alter MessageBox behaviour, other than what's supported by its arguments.
That said, your message box can very well use stock Yes/No options.
The task dialog functionality introduced in Vista does exactly what you want and follows the prevailing system theme. However, if you have to support XP, then this will be of little comfort to you.
I know this question is old, but I just stumbled upon it now.
I would like to expand the other answers in regards to using a TaskDialog instead of a MessageBox. Here's a concise example of using a TaskDialog to do precisely what was asked; change the button's texts:
const TASKDIALOG_BUTTON buttons[] = { {IDYES, L"Overwrite"}, {IDNO, L"Skip"}, {IDCANCEL, L"Cancel"} };
TASKDIALOGCONFIG taskDialogConfig = {
.cbSize = sizeof(TASKDIALOGCONFIG),
.pszMainIcon = TD_WARNING_ICON, // TaskDialog does not support a question icon; see below
.pButtons = buttons,
.cButtons = ARRAYSIZE(buttons),
.pszWindowTitle = L"No title",
.pszContent = L"Target folder already exists. Do you want to overwrite the folder?"
};
TaskDialogIndirect(&taskDialogConfig, NULL, NULL, NULL);
Some noteworthy things:
You need to use TaskDialogIndirect, not the basic TaskDialog function
when not specifying a parent window, the icon specified in pszMainIcon is displayed in the title bar as well
There is no equivalent to the MessageBox's MB_ICONQUESTION, quoting a quote from this forumpost: Don't use the question mark icon to ask questions. Again, use the question mark icon only for Help entry points. There is no need to ask questions using the question mark icon anyway—it's sufficient to present a main instruction as a question.
checking which button was selected would have to be done by passing a pointer to an int as the second argument of TaskDialogIndirect and checking its value on return (the documentation should be pretty clear)
Here is a small open source library that allows you to customize Message Boxes. Developed by Hans Ditrich.
I have successfully used it in another POC that allows embedding a custom icon in such MessageBox that can be called even from a Console application.
I should also point to the Task Dialog. Here is an example of using it:
int nButtonPressed = 0;
TaskDialog(NULL, hInst,
MAKEINTRESOURCE(IDS_APPLICATION_TITLE),
MAKEINTRESOURCE(IDS_DOSOMETHING),
MAKEINTRESOURCE(IDS_SOMECONTENT),
TDCBF_OK_BUTTON | TDCBF_CANCEL_BUTTON,
TD_WARNING_ICON,
&nButtonPressed);
if (IDOK == nButtonPressed)
{
// OK button pressed
}
else if (IDCANCEL == nButtonPressed)
{
// Cancel pressed
}

ListBoxItem background color

I want upon selection to change the background color of the item, however i can't seem to find a way to do it.
I'm trying to use the code below but can't find the completion for it, and i need to set the colror to a custom one such as "#8e8e8e":
private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (list.SelectedItem != null)
{
ListBoxItem a = sender as ListBoxItem;
//a.Background=
PhoneApplicationService.Current.State["test"] = list.SelectedItem;
NavigationService.Navigate(new Uri("/Detail", UriKind.Relative));
}
list.SelectedIndex = -1;
}
So i need to chnage the selected item background color to #8E8E8E from C# for simplicity because changing the states seems a bit complicated especially that all i need to do is change background color of the selected item.
Thanks in advance,
Although not recommend like Matt already pointed out you should be able to achieve this by using ControlTemplates and Visual States. Check out this article on windowsphonegeek to see how: Part 1 Part 2
Please let us know if you encounter any problems
You should look at the visual states for selected items if you wish to do this.
However, standard behaviour for Metro apps and recommended design best practices advise not to use colour to indicate selection in anythign other than the ListPicker.
Without seeing your app, how you're using the ListBox or the purpose of indicating selection it's hard to advise further though.

How to paste text from one app to another using Cocoa?

I have read about NSPasteBoard in the Apple documentation, and how it allows for applications to write into the PasteBoard and allow other applications to read that text and use it.
Could someone tell me how to paste text from am application (that sits in the status bar) into a NSTextField that is inside a different application.
What I am trying to do is something similar what Snippet and SnippetsApp do.
If I am completely stupid and missed the obvious in Apple Docs, could you please point me to the right path :)
Thanks!
Could someone tell me how to paste text from am application (that sits in the status bar) into a NSTextField that is inside a different application.
Pasting is what happens in the receiving application. Writing to a pasteboard is copying.
Furthermore, you can't assume that the user will want to paste into an NSTextField. It may be an NSTextView, or a textarea in a WebView, or a Carbon EditText or MLTE control, or some other text editor such as a Qt or wxWidgets text editor. They may even be using an app with a list view that lets them paste text directly into it.
So, there's no programmatic way to directly tell an application “here's some text — paste it, please”. You have to copy it to the general pasteboard and then forge an event that should generally cause the frontmost app to paste. Charlie's suggestion of ⌘V is one way, albeit tricky; the Dvorak layout puts V on another key, while the “Dvorak QWERTY ⌘” layout puts V-with-⌘ (as opposed to V-without-⌘) on the same key as QWERTY's V.
To forge that ⌘V event, look into CGEventTap. You'll need to use the CGEventCreateKeyboardEvent function to create the event itself, and since that function takes a key code, you'll need to look up the proper key code for the V part of the ⌘V combination, which will require going through Text Input Source Services or Keyboard Layout Services, depending on the layout.
You might at this point think of using Accessibility to find the Paste menu item in the Edit menu and send it an AXPress message, but “Paste” and “Edit” are only English's words for those concepts; if you did this, your app would not work in any other language. You could go by order (third menu, sixth menu item), but then your app would not work in applications without a File menu, or without a Redo menu item, or with two Undo menu items (Photoshop). Forging a ⌘V event really is the way to go.
Here's some working code to post the ⌘+key event (assuming a known keycode):
// some common keycodes
#define KEY_CODE_x ((CGKeyCode)7)
#define KEY_CODE_c ((CGKeyCode)8)
#define KEY_CODE_v ((CGKeyCode)9)
void DCPostCommandAndKey(CGKeyCode key)
{
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef keyDown = CGEventCreateKeyboardEvent(source, key, TRUE);
CGEventSetFlags(keyDown, kCGEventFlagMaskCommand);
CGEventRef keyUp = CGEventCreateKeyboardEvent(source, key, FALSE);
CGEventPost(kCGAnnotatedSessionEventTap, keyDown);
CGEventPost(kCGAnnotatedSessionEventTap, keyUp);
CFRelease(keyUp);
CFRelease(keyDown);
CFRelease(source);
}
Generally the only way is to write it to the NSPasteboard and then switch to another app and use some Carbon functions to press "Command-V"...

How to localize win32 dialogs?

I am working on Win32 dialogs with various controls like Static Text , Checkbox etc and all the strings need to be localized for different languages.
I have designed the dialogs for US intl . But when I put localized strings those, not fitting properly and I have to change layout for each intl.
Is there a better way to do this?
Can I create one dialogs with one layout that should work with all Intsl???
I remember reading somewhere that, during initial layout of GUI resources at MS, they create the dialogs initially in German, and then double check the layouts at least in English and Japanese.
Once a dialog layout accommodates those three languages, it typically did not require further layout changes.
You could consider using ShowHTMLDialog. If you can work out the black magic of getting data into and out of the dialog HTML does have the advantage of controls that automatically scale to accommodate their text bounds.
In the past I implemented the following derived class from CDialog called CLanguageDialog. Then I called loadLanguage() in the OnInitdialog(). Then all my dialogs in my application would derive from CLanguageDialog instead of CDialog.
void CLanguageDialog::loadLanguage()
{
CWnd *pChild = this->FindWindowEx(this->m_hWnd, NULL, NULL, NULL);
while(pChild)
{
theApp.languageLoader.loadStringForWnd(pChild);
pChild = pChild->GetNextWindow();
}
}

Resources