How to know previously selected language in the localization? - c#

I have two languages, English and Gujarati for UI. If, At the time of closing UI language is Gujarati, then at the opening time also language should be Gujarati. How do I know this: ?? the code for language selection is here.
ComboBoxItem englishLanguageItem = new ComboBoxItem()
{
Content = Strings.MainWindow_Language_Selection_English_Label
};
ComboBoxItem gujaratiLanguageItem = new ComboBoxItem()
{
Content = Strings.MainWindow_Language_Selection_Gujarati_Label
};
Please give some code regarding it.

Not sure if I understood your problem. You need to know what was the last selected language, so when your program is re-open, it comes up with that language?
If so, you have to save it somewhere, maybe on registry.
Here you can check how to do it: http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

Related

C# Multi-language Localize resources Windows Forms - how to do not apply empty resource for controls?

I can't use normal localization for Windows Forms for project i am currently working on (it's not support multi-language resx files well due to merge system).
So i wrote workaround for that:
private static readonly ComponentResourceManager resources = new ComponentResourceManager(typeof(Form_GUI));
private void ApplyRes(Control.ControlCollection cc, CultureInfo ci)
{
foreach (Control c in cc)
{
if (excludeControls.Contains(c.Name))
continue;
resources.ApplyResources(c, c.Name, ci);
if (c.Controls.Count > 0)
ApplyRes(c.Controls, ci);
}
}
The problem is for some cases, i am not sure if this connected to deep level of item or something else. I am getting blank values after language change.
So if there a way to check if localize resource contains value for control than change it, if not - let it stay in default (English) language.
Totaling: do not try to apply empty value.
Also want to mention i am using in form custom component, for some reason it do not switch back to english language, once any other language was chosen, but normally switch to other localized languages. Other forms elements work ok.
Is there any way to do so? Will be appreciative for help! :)

AutoCorrect in WPF Richtextbox

I read on MSDN that .NET 4.6.1 supports auto correct now.
The files in %appdata%/Microsoft/Spelling// were created automatically and I added the following line to the default.acl (file is still UTF-16 with BOM):
tramampoline|trampoline
I have set the project to target 4.6.1 and enabled SpellCheck on a RichTextBox:
<RichTextBox SpellCheck.IsEnabled="True" Language="de-DE"/>
While it highlights the word when typed wrong in the usual manner, there is no autocorrection happening.
What am I missing here?
I don't quite understand the note:
Note: These new file-formats are not directly supported by the WPF spell checking API’s, and the custom dictionaries supplied to WPF in applications should continue to use .lex files.
I know this is old, but as far as I know you need to handle AutoCorrect on your own (if I'm wrong please correct me with an example). You can do this as follows:
var caretPosition = richTextBox.CaretPosition;
// Make sure you're passing a textpointer at the end of the word you want to correct, i.e. not like this ;)
errorPosition = richTextBox.GetNextSpellingErrorPosition(caretPosition, LogicalDirection.Backward);
if(errorPosition == null)
{
return;
}
var errors = richTextBox.GetSpellingError(errorPosition);
// Default would be to only replace the text if there is one available replacement
// but you can also measure which one is most likely with a simple string comparison
// algorithm, e.g. Levenshtein distance
if (errors.Suggestions.Count() == 1)
{
var incorrectTextRange = richTextBox.GetSpellingErrorRange(errorPosition);
var correctText = error.Suggestions.First();
var incorrectText = incorrectTextRange.Text;
// Correct the text with the chosen word...
errors.Correct(correctText);
}
// Set caret position...
An important note would be not to use the RTB's CaretPosition, but rather to use a textpointer at the end of the word you wish to correct. If your textpointer/caret is in a weird spot (e.g. the end of 20 whitespaces), the GetNextSpellingErrorPosition method may take up to 60 seconds before it returns (depending on the hardware/number of words in your RTB).

How to change the language in my WinForm?

I am developing an application in c#. The current language of my system is French. What i want is when i open my application the language should be changed to English. Is there anyway by which i can perform this task. I tried to change the language through code but nothing seems to work.
Here is my code
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I'm going to assume that you have your forms localized to both French and English. If that is the case, then once you run the code in your question, new forms you display will show in the new language.
If you want to change the language and have forms that are currently being shown redisplay their text in the new language, you have to put together something that responds to the culture being changed and update all the labels, radio buttons, and so on.
Fortunately, someone has already done this work for you:
http://www.codeproject.com/Articles/23694/Changing-Your-Application-User-Interface-Culture-O
Windows will not automatically translate your application, you need to provide these yourself and load them into language specific.
This article explains the process quite well. You're updating the culture which tells the system which resource files to use. Now you need to provide the text to show.
You have to reload forms (or switch language before you create any form)
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
Form1 form1 = new Form1();
form1.ShowDialog();
This assume, you are using satellite assemblies and already have form translated.
If you want to change keyboard layout (FR -> EN), then, while it is also possible, you better don't. As user may have his preference for which layout he want to use by default.
I can't replicate your problem here. The code should work fine in term of changing the way Double.Parse method works. Here is how I did the test :
string duit = "1.000.100"; //this is a valid number format in my current culture
string money = "1,000,100"; //but this is not valid
var culture = CultureInfo.CurrentCulture; //my current culture is indonesia (id-ID)
var duitDouble = double.Parse(duit); //parsed successfully
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
culture = CultureInfo.CurrentCulture; //now current culture is english (en-US)
var moneyDouble = double.Parse(money); //parsed successfully
Besides, I really suggest to rephrase your question and title to avoid misunderstanding. That will be good for you and people that intend to help. As you can see, most of the answers posted are not actually answering the question (the actual problem is indicated in OP's second comment in the question).

C# window application get word from the cursor position from the other application

I have C# window application and I want to get text where the cursor is currently located or text is selected from the other application like notepad, notepad++ or any browser etc.
Did you already have a look at this CodeProject article ? This could be a start even if this is not exactly what you are looking for.
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms632604(v=vs.85).aspx.
If it do not solve your issue, have a look at http://msdn.microsoft.com/en-us/library/system.windows.automation.textpattern.getselection(v=vs.110).aspx, as suggested in the comments.
Getting the text under the cursor (or from the caret) requires UI Automation and TextPattern support from the application. The problem is that not all applications support this, and the older the application, the less likely it is to have TextPattern support.
Getting selected text is, ironically enough, somewhat easier, although still not 100%. I outlined a solution in this answer. It does involve managing focus and manipulating the clipboard for the most general solution, and it is by no means perfect.
Another option, that involves a ton of work, is to use a mirror driver to capture the screen contents, and then use other technologies (OCR, etc.) to capture the text. I don't really recommend this; it's not supported in Windows 8 and above, but if you absolutely have to have 100% support across applications with the least impact, then it's a possibility. It's a lot of work, though. Definitely not for the squeamish.
This is possible using Accessibility technologies (like screen readers). However, it will require a great deal of troubleshooting:
The answer about MSAA on the following question is where you will need to start.
Best way to get the 'word before the cursor' in any open app's text field
Also, the following question is helpful about implementing it:
How to get the word under the cursor in Windows?
The problem is that you are trying to get data from another application. Unless that application supports a way to provide this to you it will be very difficult.
It would be much easier if the info could be retrieved from within the application, like from within a textbox or rich text control on a form
You can use clipborad to copy or get that text and then transfer it to your desired window.
You can use SendKeys class to demonstrate the keyboard.
For example you can use SendKeys.Send("^C") in your program and then a code to focus on Notepad++ and then SendKeys.Send("^V").
SendKeys.Send("^C");
// code to change active window and focus on Notepad++.
SendKeys.Send("^V");
Thanks for help me.
Still I'm not able to get the text from the caret position. So finally I get the all the text from the active window and fetch my text using Regex.
private string SelectText(IntPtr hWnd)
{
string text = string.Empty;
Regex regex = new Regex(#"(\d{3}-\w{5,8})");
if (InputSimulator.IsKeyDown(VirtualKeyCode.SHIFT))
{
InputSimulator.SimulateKeyUp(VirtualKeyCode.SHIFT);
}
if (InputSimulator.IsKeyDown(VirtualKeyCode.MENU))
{
InputSimulator.SimulateKeyUp(VirtualKeyCode.MENU);
}
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text) && regex.IsMatch(text))
{
Thread.Sleep(100);
text.Trim();
string[] textArr = text.Split(' ');
text = textArr[textArr.Length - 1];
}
else
{
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_A);
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
ClickOnPoint();
Thread.Sleep(100);
text = Clipboard.GetText();
MatchCollection matchCollection = regex.Matches(text);
if (matchCollection.Count > 0)
{
text = matchCollection[0].Value;
}
else
{
text = string.Empty;
}
}
Clipboard.Clear();
return text;
}

Is there a free tool/plugin that can format code ect on the fly for VS C#?

Ive just moved from VB.Net to C#. I dont understand why people are using it/prefer it as it is soo much more time consuming!
In VB.NET, you simply type your code and VB.NET formats is as you go, For example:
removes unneccessary whitespace,
automatically puts in brackets,
tabs blocks of code,
automatically creates the NEXT, END IF, statements for blocks.
and the opposite/nuiances in C#
if you change the name of an event handler it creates a new one, doesnt rename the existing one
you must have the () at the end of a method
and im sure theres more.
Why is C# backwards like this? Surely there must be a way to improve productivity somehow. Any ideas or free tools out there?
This has nothing to do with the language, and everything to do with the editor.
Regardless, the editor for C# in visual studio does support automatic formatting in several ways.
If you delete and reinsert the closing brace }, it will reformat/reindent automatically.
There are several menu items and corresponding keyboard shortcuts that will reformat code for you:
Ctrl+k+d - this will reformat the whole document.
Ctrl+k+f - this will reformat the selection.
There are also extensive refactoring capabilities - the rename refactoring will rename a member everywhere it is mentioned, even if it is in other projects.
Not automatic BUT.....
Use
Ctrl+K+Ctrl+D
to format document keystroke
Use
Ctrl+K+Ctrl+F
to format selection keystroke
From Visual Studio Format entire file?
I'm using C# in Visual Studio 2008 and it behaves exactly as you describe. Pretty much every time I type a semi-colon or curly brace, it corrects all formatting within the context.
For example...
if (myValue!= null) {
someValue = myValue;
If I type the closing curly brace, it turns into this:
if (myValue != null)
{
someValue = myValue;
}
All dependent on the style settings in Tools > Options
Also there exits some so called code snippets. If you simply type if and press tab tab this will automatically result into
if (true)
{
}
setting the cursor directly onto the true.
Even better is the switch snippet. If you enter switch and press the tab twice you'll get
switch (switch_on)
{
default:
}
where your cursor stands on switch_on.
If you now enter something meaningful like a variable name that holds an enum value (e.g. var color = Color.Red;) and press Enter it will automatically fill in all possible cases.
There are more code snippets available and some are very handy like foreach, try, prop, propg.

Categories

Resources