自定义数值向上
Custom Numericupdown
我正在尝试创建自定义 numericUpDown 控件。我正在尝试向 numericUpDown 添加文本,但效果很好。但是,现在我想找到一种方法来 select 只有点击控件时的数字,因为此时当我手动 "edit" 值时,我可以删除我添加到控件中的单位。我在 google 上的一个软件中发现了这个控件,它类似于 "ranged" numericUpDown。它按照我需要的方式工作,可以单击并 select 只有数字。当您单击文本时,它应该 select 要编辑的数字而不是文本:
这是我现在拥有的:
public class RangedNumericUpDown : NumericUpDown
{
public int ValueMin = 0;
public int ValueMax = 10;
public RangedNumericUpDown()
{
}
protected override void UpdateEditText()
{
if(this.Text != this.ValueMin + " to " + this.ValueMax)
this.Text = this.ValueMin + " to " + this.ValueMax;
}
}
我找不到像这样的 windows 表单的任何自定义控件,我想可以创建类似该图像的东西。
嗯,你的代码不够。不太确定我会推荐这个,将 "focus" 从一个值移动到另一个值并不是很直观。只需使用两个 NUD,你就会变得更好 UI。
Anyhoo,一些可以玩的代码:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
class RangeUpDown : NumericUpDown {
public decimal LowValue { get; set; }
public decimal HighValue { get; set; }
private bool lowFocused = true;
private const string separator = " to ";
protected override void UpdateEditText() {
if (base.UserEdit) ParseText();
var lotext = FormatValue(LowValue);
var hitext = FormatValue(HighValue);
this.Text = lotext + separator + hitext;
if (lowFocused) {
EditBox.SelectionStart = 0;
EditBox.SelectionLength = lotext.Length;
}
else {
EditBox.SelectionStart = lotext.Length + separator.Length;
EditBox.SelectionLength = hitext.Length;
}
EditBox.Focus();
}
public override void UpButton() {
if (base.UserEdit) ParseText();
if (IsLowValueFocused()) {
LowValue = Math.Min(this.Maximum, LowValue + this.Increment);
if (HighValue < LowValue) HighValue = LowValue;
}
else {
HighValue = Math.Min(this.Maximum, HighValue + this.Increment);
}
this.OnValueChanged(EventArgs.Empty);
UpdateEditText();
}
public override void DownButton() {
if (base.UserEdit) ParseText();
if (IsLowValueFocused()) {
LowValue = Math.Max(this.Minimum, LowValue - this.Increment);
}
else {
HighValue = Math.Max(this.Minimum, HighValue - this.Increment);
if (LowValue > HighValue) LowValue = HighValue;
}
this.OnValueChanged(EventArgs.Empty);
UpdateEditText();
}
private bool IsLowValueFocused() {
lowFocused = EditBox.Text.Substring(EditBox.SelectionStart).Contains(separator.Trim());
return lowFocused;
}
private string FormatValue(decimal value) {
var fmt = (this.ThousandsSeparator ? "N" : "F");
var frac = this.DecimalPlaces.ToString(CultureInfo.CurrentCulture);
return value.ToString(fmt + frac, CultureInfo.CurrentCulture);
}
private TextBox EditBox { get { return (TextBox)Controls[1]; } }
private void ParseText() {
// Not implemented
base.UserEdit = false;
}
}
我正在尝试创建自定义 numericUpDown 控件。我正在尝试向 numericUpDown 添加文本,但效果很好。但是,现在我想找到一种方法来 select 只有点击控件时的数字,因为此时当我手动 "edit" 值时,我可以删除我添加到控件中的单位。我在 google 上的一个软件中发现了这个控件,它类似于 "ranged" numericUpDown。它按照我需要的方式工作,可以单击并 select 只有数字。当您单击文本时,它应该 select 要编辑的数字而不是文本:
这是我现在拥有的:
public class RangedNumericUpDown : NumericUpDown
{
public int ValueMin = 0;
public int ValueMax = 10;
public RangedNumericUpDown()
{
}
protected override void UpdateEditText()
{
if(this.Text != this.ValueMin + " to " + this.ValueMax)
this.Text = this.ValueMin + " to " + this.ValueMax;
}
}
我找不到像这样的 windows 表单的任何自定义控件,我想可以创建类似该图像的东西。
嗯,你的代码不够。不太确定我会推荐这个,将 "focus" 从一个值移动到另一个值并不是很直观。只需使用两个 NUD,你就会变得更好 UI。
Anyhoo,一些可以玩的代码:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
class RangeUpDown : NumericUpDown {
public decimal LowValue { get; set; }
public decimal HighValue { get; set; }
private bool lowFocused = true;
private const string separator = " to ";
protected override void UpdateEditText() {
if (base.UserEdit) ParseText();
var lotext = FormatValue(LowValue);
var hitext = FormatValue(HighValue);
this.Text = lotext + separator + hitext;
if (lowFocused) {
EditBox.SelectionStart = 0;
EditBox.SelectionLength = lotext.Length;
}
else {
EditBox.SelectionStart = lotext.Length + separator.Length;
EditBox.SelectionLength = hitext.Length;
}
EditBox.Focus();
}
public override void UpButton() {
if (base.UserEdit) ParseText();
if (IsLowValueFocused()) {
LowValue = Math.Min(this.Maximum, LowValue + this.Increment);
if (HighValue < LowValue) HighValue = LowValue;
}
else {
HighValue = Math.Min(this.Maximum, HighValue + this.Increment);
}
this.OnValueChanged(EventArgs.Empty);
UpdateEditText();
}
public override void DownButton() {
if (base.UserEdit) ParseText();
if (IsLowValueFocused()) {
LowValue = Math.Max(this.Minimum, LowValue - this.Increment);
}
else {
HighValue = Math.Max(this.Minimum, HighValue - this.Increment);
if (LowValue > HighValue) LowValue = HighValue;
}
this.OnValueChanged(EventArgs.Empty);
UpdateEditText();
}
private bool IsLowValueFocused() {
lowFocused = EditBox.Text.Substring(EditBox.SelectionStart).Contains(separator.Trim());
return lowFocused;
}
private string FormatValue(decimal value) {
var fmt = (this.ThousandsSeparator ? "N" : "F");
var frac = this.DecimalPlaces.ToString(CultureInfo.CurrentCulture);
return value.ToString(fmt + frac, CultureInfo.CurrentCulture);
}
private TextBox EditBox { get { return (TextBox)Controls[1]; } }
private void ParseText() {
// Not implemented
base.UserEdit = false;
}
}