Files
Common-VS2019/SacsStandardMob/Mobile/Forms/Controls/SaxEntry.cs
T
2021-05-05 15:36:28 +01:00

153 lines
3.9 KiB
C#

using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;
namespace Sarand.Net.Standard.Mobile.Forms.Controls
{
//=======================================================================
public class SaxEntry : Entry
{
//-------------------------------------------------------------------
// Definitions.
public enum TextCaseType
{
Normal,
Upper,
Lower,
Title
}
//-------------------------------------------------------------------
// Data members.
private bool trimTextOnCompletion = true;
private TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
private TextCaseType textCase = TextCaseType.Normal;
private bool numericOnly = false;
private byte decimals = 0;
//-------------------------------------------------------------------
// Construction.
public SaxEntry ()
{
DefaultFontSize = FontSize;
TextChanged += SaxEntry_TextChanged;
Completed += SaxEntry_Completed;
}
//-------------------------------------------------------------------
// Properties.
public bool TrimText
{
get { return trimTextOnCompletion; }
set
{
if (trimTextOnCompletion == value) return;
trimTextOnCompletion = value;
}
}
public double DefaultFontSize { get; }
public TextCaseType TextCase
{
get { return textCase; }
set
{
if (textCase == value) return;
textCase = value;
FormatText();
}
}
public bool NumericOnly
{
get { return numericOnly; }
set
{
if (numericOnly == value) return;
numericOnly = value;
Keyboard = Keyboard.Numeric;
FormatText();
}
}
public byte Decimals
{
get { return decimals; }
set
{
if (numericOnly && decimals == value) return;
numericOnly = true;
decimals = value;
Keyboard = Keyboard.Numeric;
FormatText();
}
}
//-------------------------------------------------------------------
// F o r m a t T e x t
//-------------------------------------------------------------------
private void FormatText (bool trimText = false)
{
String fn = GetType().Name + ".FormatText";
String originalText = Text;
String formattedText = originalText;
if (StringUtil.IsEmpty(formattedText)) return;
if (trimText)
formattedText = StringUtil.TrimAll(formattedText);
if (numericOnly)
{
if (decimals > 0)
{
decimal value = StringUtil.ToDecimal(formattedText);
if (value != 0.0m)
formattedText = value.ToString("N" + decimals);
else if (!formattedText.Equals("0"))
formattedText = "";
}
else
{
long value = StringUtil.ToLong(formattedText);
if (value != 0)
formattedText = value.ToString();
else if (!formattedText.Equals("0"))
formattedText = "";
}
}
else
{
if (textCase == TextCaseType.Upper)
formattedText = textInfo.ToUpper(formattedText);
else if (textCase == TextCaseType.Lower)
formattedText = textInfo.ToLower(formattedText);
else if (textCase == TextCaseType.Title)
formattedText =
StringUtil.ToTitleCase(formattedText, textInfo);
}
if (!originalText.Equals(formattedText))
{
SaLogger.Debug(fn + " Text: " + originalText + ". Formatted: " + formattedText);
Text = formattedText;
}
}
//-------------------------------------------------------------------
// C o n t r o l E v e n t s
//-------------------------------------------------------------------
private void SaxEntry_TextChanged (object sender, TextChangedEventArgs e)
{
FormatText();
}
private void SaxEntry_Completed (object sender, EventArgs e)
{
FormatText(trimTextOnCompletion);
}
//-------------------------------------------------------------------
}
//=======================================================================
}