Minor issues with TextBox Style
Minor issues with TextBox Style
06 Jul 2024, 20:30
Hellooow peeps,
I just want to report that BorderThickness, BorderColor and TextAlignment properties won't apply to a TextBox if we try to set these within a style.Set()
var input = new TextBox
{
BorderThickness = 1, // that will work
BorderColor = "#1FFFFFFF", // that will work
Margin = "0 5 0 0",
Text = "0",
TextAlignment = TextAlignment.Center, // that will work
Style = Styles.CreateInputStyle() // that will not work for the mentioned properties
};
public static Style CreateInputStyle()
{
var style = new Style();
style.Set(ControlProperty.BorderColor, Color.FromHex("#1FFFFFFF"));
style.Set(ControlProperty.BorderThickness, 1);
style.Set(ControlProperty.TextAlignment, TextAlignment.Center);
}
Also, it would be nice to have SelectionStart (get or set caret position) and CornerRadius properties, and some more event for the TextBox control, especially HasFocus.
I'm facing an issue I can't seem to find a solution for, and I believe I could work around it with these. The caret is sometimes jumping in front of the chars when I'm typing which is really annoying. It's not directly due to the TextBox itself, I think it's related to multi-threading and the way the Chart.ObjectsUpdated occurs under the hood but I'm unsure of the real “problem” there.
Basically my ObjectsUpdated event handler will get the Y value of a horizontal line being dragged by the user, and set this value to a TextBox. On the other side, my TextChanged event handler will, among other things, get the text value of a textbox and draw (or modify the Y value of) a line on the chart. Since the TextChanged event only gets fired when manual input occurs, there is no infinite loop between the two of them, which is good. So I thought I could just unsub/resub to the ObjectsUpdated event handler and that would solve the problem but it isn't. I tried to put a Task.Delay(500) followed by the resub inside a BeginInvokeOnMainThread but that doesn't solve anything either, although in that case the problem occurs less frequently. When the event still gets fired, the log timestamp shows that event did not properly get delayed. I'm sure the rest of the code isn't guilty cause if I comment out the resub, the caret is never jumping while typing. On top of that, I can't find a pattern to reproduce that behavior 100% of the time, it happens every now and then. I let you imagine what it feels like lolol.
Sea yah!
Replies
cindyarhodia
10 Jul 2024, 22:22
RE: Minor issues with TextBox Style
Thank you, you the boss!
What about the SelectionStart property, any chance your team will implement it ? I managed to solve my initial problem using a lock + countdown to prevent the ObjectsUpdated event from firing when user is typing in a TextBox, but I now face a similar issue (aka the caret is jumping in front of the chars when typing).
I made a simple ValidateInput(TextBox) method to ensure the value entered in a TextBox matches some parameters: only one ‘.’ is allowed, max amount of decimal digits equals symbol.Digits, and append a ‘0’ if string starts with a ‘.’ I'm using this inside the TextChanged event handler. The method works fine, except the caret is jumping to the left if we append a ‘0’ or if the amount of decimal digits is too big. Since the text gets set in every case, I assume it really gets updated only if there is a difference between original text and filtered text, right? Here is the code, self-explanatory I believe :
private void ValidateInput(TextBox textBox)
{
string text = textBox.Text;
bool decimalPointFound = false;
int decimalDigits = 0;
StringBuilder filteredText = new();
foreach (char c in text) {
if (char.IsDigit(c)) {
if (decimalPointFound && decimalDigits < _symbol.Digits) {
filteredText.Append(c);
decimalDigits++;
} else if (!decimalPointFound) {
filteredText.Append(c);
}
} else if (c == '.' && !decimalPointFound) {
if (filteredText.Length == 0) {
filteredText.Append(0);
}
filteredText.Append(c);
decimalPointFound = true;
}
}
textBox.Text = filteredText.ToString();
textBox.SelectionStart = filteredText.Length; // Would be nice to be able to do that, effectively setting the caret position to end of text
}
Tyty, sea yah !
@cindyarhodia
PanagiotisCharalampous
11 Jul 2024, 06:43
RE: RE: Minor issues with TextBox Style
cindyarhodia said:
Thank you, you the boss!
What about the SelectionStart property, any chance your team will implement it ? I managed to solve my initial problem using a lock + countdown to prevent the ObjectsUpdated event from firing when user is typing in a TextBox, but I now face a similar issue (aka the caret is jumping in front of the chars when typing).
I made a simple ValidateInput(TextBox) method to ensure the value entered in a TextBox matches some parameters: only one ‘.’ is allowed, max amount of decimal digits equals symbol.Digits, and append a ‘0’ if string starts with a ‘.’ I'm using this inside the TextChanged event handler. The method works fine, except the caret is jumping to the left if we append a ‘0’ or if the amount of decimal digits is too big. Since the text gets set in every case, I assume it really gets updated only if there is a difference between original text and filtered text, right? Here is the code, self-explanatory I believe :
private void ValidateInput(TextBox textBox)
{
string text = textBox.Text;
bool decimalPointFound = false;
int decimalDigits = 0;
StringBuilder filteredText = new();
foreach (char c in text) {
if (char.IsDigit(c)) {
if (decimalPointFound && decimalDigits < _symbol.Digits) {
filteredText.Append(c);
decimalDigits++;
} else if (!decimalPointFound) {
filteredText.Append(c);
}
} else if (c == '.' && !decimalPointFound) {
if (filteredText.Length == 0) {
filteredText.Append(0);
}
filteredText.Append(c);
decimalPointFound = true;
}
}
textBox.Text = filteredText.ToString();
textBox.SelectionStart = filteredText.Length; // Would be nice to be able to do that, effectively setting the caret position to end of text
}
Tyty, sea yah !
Hi there,
We do not have such plans at the moment, we can consider it in a later update.
Best regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
10 Jul 2024, 05:26
Hi there,
Thank you for reporting this issue. It will be fixed in an upcoming update.
Best regards,
Panagiotis
@PanagiotisCharalampous