Windows Phone textbox limits to only accepts 8 characters - c#

I'm having a problem with a Windows Phone 7 application that I'm coding (Using Visual Studio 2010). My code looks like this:
private void Key1Input_TextChanged(object sender, EventArgs e)
{
string hexOnly= Key1Input.Text;
int n = 0;
if (!int.TryParse(hexOnly, System.Globalization.NumberStyles.HexNumber, System.Globalization.NumberFormatInfo.CurrentInfo, out n) &&
hexOnly!= String.Empty)
{
Key1Input.Text = hexOnly.Remove(hexOnly.Length - 1, 1);
Key1Input.SelectionStart = Key1Input.Text.Length;
}
}
which is a same code that can also be found from this web site: http://social.msdn.microsoft.com/Forums/windows/en-US/ec7b777d-deb1-45e1-b66e-e25daddf6497/text-box-which-accepts-only-hexadecimal-values
The code works fine, but when I tried to insert 9th character to the textbox, it didn't show up. My textbox's Maxlength value is 12. After inserting some breakpoints to the code and debugging the app, I noticed it goes inside the if statement when there's 8 hex numbers and trying to type more. Am I doing something wrong, or are there some kinds of limitations with WP7.
I also tried the other 2 codes from the same site. The second one works, while the third one does not.

The smallest 8-digit hex number is F0000000, which is greater than Int32.MaxValue. Try long instead of int.

Related

Making Raylib-cs work with extended ascii / unicode above 128 in windows?

I'm teaching C# and game dev to high school students, and we're using Raylib-cs as a simple introduction to graphics APIs and libraries.
We've hit a small snag: We're swedish, and we'd like to use some special non-ascii letters – namely å, ä and ö (lots of swedish words use them). However, I can't get Raylib-cs to display anything above codepoint 127 – at least not using DrawText.
Instead, all I get is ?.
This is on Windows 10, 64-bit, 20H2. Using dotnet 5 (latest) and primarily the Raylib-cs available as a nuget package.
What I've tried so far:
DrawText and DrawTextEx. Same result.
Loading different fonts, with or without explicit inclusion of codepoints up to 255. Same result.
Getting the latest Raylib-cs from the github page. Same result.
Running the same code but in a virtual Debian machine. THIS WORKS, so issue seems to be in Windows.
Asking a friend who's proficient in C/C++ to try using åäö using Raylib in C++. THIS WORKS, so the issue seems to be specific to Raylib-cs, even though it's just a wrapper?
DrawTextCodepoint. THIS WORKS, which means that for some reason the issue is specific to the DrawText methods (and InitWindow). Raylib is supposed to be Unicode-capable, and this proves that at least in theory, it is.
Here's my simple test code (just writes out characters 0-255):
static void Main(string[] args)
{
int font_size = 10;
Raylib.InitWindow(800, font_size * 64, "åäö");
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.BEIGE);
for (int i = 0; i < 255; i++)
{
int col = i / 64;
int x = col * 200;
int y = (i % 64) * font_size;
string text = i.ToString() + " | " + ((char)i).ToString();
Raylib.DrawText(text, x, y, font_size, Color.BLACK);
}
Raylib.EndDrawing();
}
}
Result in windows (at least for me): Window title bar is "???", as are all characters beyond the second column.
Result in debian: Window title bar is "åäö", and all characters are drawn as they should.
Has anyone come across this problem? Anyone got (tested) solutions?
Is there some known quirk in how C# specifically on windows handles strings or something?
The way the library imports DrawText is faulty; its default behavior "marshals" string values to fit a legacy character encoding, not necessarily UTF-8: https://github.com/ChrisDill/Raylib-cs/blob/b1f46d33071387800559523950aa131448251461/Raylib-cs/Raylib.cs#L2116 (Specifying CharSet=Unicode probably won't help because the DrawText function doesn't use wide-character strings, but rather ordinary char* pointers interpreted as UTF-8.)
I see you have posted an issue to the GitHub repository. And fortunately, the library's author suggested two workarounds. As they said, one of them is: "Using [MarshalAs(UnmanagedType.LPUTF8Str)] to marshal strings as UTF8."

Textbox Number Formating & start with zero & accept only Numbers

I am using below Property example to make some calculation on textbox and if textbox is null I am assigning zero to it so calculation won't fail as you can see I am using Math.Round and I want to make several checks on these textbox input like
textbox that only accepts numbers I searched and found method 1
I want my textbox to be formated I searched and found Method 2
Now my question is ..
Is there any way to mareg all these method in the property method I am using
so my code won't be like "spaghetti code" ?
is there any better ways to do these checks ?
Thank you in advance
Property example
public double ItemPriceResult
{
get
{
return Math.Round(ItemCost * RevenuePercentage / 100 + ItemCost, 0);
}
}
Method 1
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
{
MessageBox.Show("Please enter only numbers.");
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
Method 2
textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(textBox1.Text));
UPDATE after some answers
MaskedTextBox seems fit my needs I read and searched and below some question
if you kindly would like to help me
I need to use MaskedTextBox because I can set it to accept
number and I can also force number formating so
also I need to make number textboxs easer to read for users
so 1000 will be come 1,000
and 10000 will be come 10,000
then according to Microsoft Docs formating MaskedTextBox to fit my needs
Masked MaskedTextBox with 999,999,999,
second I do not want the PromptCharto be visible I google it but none of search result did it
Try this , it will accept only numbers and u can format the string as u want using regex.
public static string ToMaskedString(this String value)
{
var pattern = "^(/d{2})(/d{3})(/d*)$";
var regExp = new Regex(pattern);
return regExp.Replace(value, "$1-$2-$3");
}
You have a TextBox. Alas you don't tell what kind of TextBox you use. System.Windows.Forms.TextBox? System.Web.UI.MobileControls.TextBox?
You write "if text box is null I am assigning zero to it". I assume that you mean that if no text is entered in the text box you assume that 0 is entered.
Furthermore you want to format the output of the text box whenever the text is changed. So while the operator is typing text you want to change this text? For the operator this is very annoying.
Wouldn't you prefer that the operator is obliged to type his text in the format you desire, helping him visually. For this you may use the class MaskedTextBox
The MaskedTextBox has a property Mask, which forces the operator to type in a certain format. I'm not really familiar with what you do with the format {0:#,##0.00}, but I assume you want the output double in a real format with two digits after the decimal point using the decimal point and the thousand separator as common in the current culture.
via the designer put in initialize component:
this.maskedTextBox1.Mask = "99990.00";
after adding the event for text changed:
private void maskedtextBox1_TextChanged(object sender, EventArgs e)
{
double enteredValue = 0.0; // value to use when empty text box
if (!String.IsNullOrEmpty(this.maskedtextBox1.Text))
{
enteredValue = double.Parse(maskedTextBox1.Text, myFormatProvider)
}
ProcessEnteredValue(enteredValue);
}
}
After your edit, the specifications have changed.
While entering the number in the text box, the operator should not have any visual feedback of the formatting of his number.
The operator is free to enter the real number in any commonly used format.
The value of the text box should not be used while the operator is editing the text box.
Only after editing is finished, the value of the text box should be interpreted for correctness, and if correct it should be used.
The actually used value should be displayed in the text box in a defined format.
The desire not to show any visual feedback while entering is understandable. After all, the program doesn't care whether the operator types 1000, 1000.00, or even 1.0E3.
The MaskedTextBox is especially used to force the operator to enter his number in a given format. Since this is not desired, my advise would be to use a TextBox instead of aMaskedTextBox.
However, because you give the operator the freedom to enter his number in any way he wants, including copy-paste, repairing typing errors, etc. you'll have to add the possibility for the user to express to the program that he has finished entering the number.
An often used method in the windows UI would be a Button. Another possibility would be the enter button. Be aware though that this is not really standard within windows. It might make learning your program a little bit more difficult.
So after the operator notified that he finished editing and the corresponding event function is called, your code could be:
// Get the numberformat to use, use current culture, or your own format
private readonly IFormatProvider myNumberFormat = CultureInfo.CurrentCulture.NumberFormat
private void OperatorFinishedEditing(TextBox box)
{
// read the text and try to parse it to a double
// accepting all common formats of real numbers in the current culture
bool valueOk = true;
double resultValue = 0;
if (!String.IsNullOrEmpty(box.Text))
{
bool valueOk = Double.TryParse(box.Text, out resultValue);
}
if (valueOk)
{
box.Text = FormatResultValue(resultValue);
ProcessValue(resultValue);
}
else
{
ShowInputProblem();
}
}

C# parsing integer from text box correctly

I am learning C# (in Visual Studio Express 2013 for Windows Desktop) by converting a simple Windows Form application I previously wrote in Visual Basic.
The following code sends a text box entry and two integers to a method that returns a boolean, but throws an exception at runtime whenever the text box doesn't contain an integer (e.g 155 is OK, but 155.67 isn't).
if (!(rangeOK(int.Parse(cmTextBox.Text), 50, 250))) return false;
I've tried using TryParse to resolve this, but despite trying lots of online tips (and others' questions in here) I haven't been able to understand how I should do it.
If it helps the original VB code was:
If Not (rangeOK(Val(cmTextBox.Text), 50, 250)) Then Return False
Many thanks
Rob
This is how you use TryParse:
int result; // does not need to be initialized
if (int.TryParse(cmTextBox.Text, out result))
{
if (!(rangeOK(result, 50, 250)))
return false;
// todo
} else
{
// process error
}
More information here:
http://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx
Good luck with it!
UPDATE
You can do the same with double.TryParse of coure, if you want to work with non integer numbers. More information here:
http://msdn.microsoft.com/en-us/library/994c0zb1%28v=vs.110%29.aspx

How to erase the comma in a calculator when the user has numbers such as 233,232 when they hit the 3 that is on the left of the comma [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to handle when the backspace button is pressed and the input panel has digit grouped numbers. E.g. 434,343,334.232.
Basically the question says it all, I'm making a calculator in c# and I'm stuck on this problem. When digit grouping is hit the numbers get separated by commas, when the user hits the backspace button, the commas act like characters that get deleted.
inputPanelTextBox.Text = inputPanelTextBox.Text.Replace(",", "") I put this piece of code under my backspace click event. The problem is that say the panel has 234,232 and the user presses the backspace button, the comma is gone as well as the 2 on the far right.
What I want to happen is the same way the windows 7 calculator does when the digit grouped menu is checked and the user hits the backspace.
Any idea of how to go about this? Thank you so much in advance.
How about you store the number just as a number then format the number to look nice when you output it. The same thing will work with string.Format(). You could reformat the number into the display string every time the user hits backspace or adds a new digit. There are lots of the string format modifiers listed here.
int numWithComma = 3000;
int numWithoutComma = 50;
Console.WriteLine("numWithComma: {0:n}", numWithComma);
Console.WriteLine("numWithoutComma: {0:n}", numWithoutComma);
// prints:
// numWithComma: 3,000.00
// numWithoutComma: 50.00
If I were making this calculator, I would save the number in a double, then when the number needs to be displayed I would run it through a function like this and display the string to the user:
private static string FormatForDisplay(double number)
{
return string.Format("{0:n}", number);
}
How about using a format string when you output the number, that way you don't have to manage commas at all.
double myDouble = 500000.012345
inputPanelTextBox.Text = myDouble.ToString("N", CultureInfo.InvariantCulture);
Its been a while since I've used Visual Studio but I'm pretty sure you can set these format strings directly on text boxes so its simply automatically handled when they type. And its easy as pie to set in the properties if I remember correctly. Check out Standard Numeric Format Strings for a bit more on it.
Also a MaskedTextBox might work as well, set the mask to something like "999,999,999,999.99" and see how that formats different numbers.
Good Luck! :D

Char and graphical visibility

running this code I see that not all values of char (from 0 to 65534) correspond to a symbol. It is rigth? Or I've to do something differernt to show symbols different from a 'square'?
Thank you all!
private void Form1_Load(object sender, EventArgs e)
{
char x = Char.MinValue;
do
{
listBox1.Items.Add(x + " - " + (int)x);
x++;
} while ((int)x < (int)Char.MaxValue);
}
That's normal. There are at least three possible explanations for a character being displayed as a square:
Some characters may not be supported by the font the control uses.
Not all Unicode codepoints are assigned.
Reserved Code Point. Any code point of the Unicode Standard that is reserved for future assignment. Also known as an unassigned code point. (See definition D15 in Section 3.4, Characters and Encoding, and Section 2.4, Code Points and Characters.)
One of the characters is actually supposed to be a square (□) when displayed correctly.
It's worth checking what fonts you have available to your application, and cross-referencing what you wan to be displayed (or expect) with the Unicode code charts.

Categories

Resources