コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
namespace AppName.Controls
{
[System.Diagnostics.DebuggerStepThrough()]
public class BaseTextBox : Entry
{
public BaseTextBox() : base()
{
this.TextChanged += (sender, e) =>
{
string value = e.NewTextValue;
//入力文字数制限
if (MaxLength > 0 &&
value.Length > MaxLength)
{
//入力文字数の制限値を超えた場合は、制限値までの文字列までを表示する
value = value.Substring(0, MaxLength);
}
//入力フォーマット
if (!String.IsNullOrEmpty(this.Format))
{
//数値以外の文字と+-.以外の記号を取り除きます。
//(カンマもとりあえず取り除きます)
value = Regex.Replace(value, @"[^\d-+.]", "");
//フォーマットを指定して文字列を成形します。
decimal ret = Decimal.Zero;
if (Decimal.TryParse(value, out ret))
{
value = ret.ToString(this.Format);
}
}
//余計なTextChangedイベントを発生させないため、最後に1行のみ値を戻す処理を行う
if (value != e.NewTextValue)
{
this.Text = value;
}
};
}
public int MaxLength { get; set; }
public string Format { get; set; }
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:base="clr-namespace:AppName.Controls;assembly=AppName"
x:Class="AppName.Views.TestPage">
<ContentPage.Content>
<base:BaseTextBox x:Name="txtPrice"
Text=""
Keyboard="Numeric"
MaxLength="5"
Format="#,##0"
HorizontalTextAlignment="End"
HorizontalOptions="End" /> <!--Format:N0/C0/###0-->
</ContentPage.Content>
</ContentPage>
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。