1.添加TextBox的屬性MaxLenth爲3,Text爲1. HscrollBar的Maximum爲100,Minimum爲0.
2.設置TextBox的TextChanged事件
double num;
//驗證輸入TextBox的值是否爲數值型
if (double.TryParse(txtNodeSize.Text.Trim(), out num))
{
//驗證輸入TextBox的數值不能大於10
if (double.Parse(txtNodeSize.Text.Trim()) > 10)
{
//輸入的值大於10默認爲10
txtNodeSize.Text = "10";
}
}
else
{
//如果輸入的值爲非數值型,默認爲10
txtNodeSize.Text = "10";
}
//把TextBox輸入值賦值給HscrollBar(HscrollBar的value爲整形)
hScrollBar1.Value = Convert.ToInt32(double.Parse(txtNodeSize.Text.Trim()) * 10);
3. 設置 HscrollBar的ValueChanged事件
//把取得的hScrollBar值轉換爲double類型
double aa = hScrollBar1.Value;
//hScrollBar的頻率爲1,除以10變爲0.1
txtNodeSize.Text = (aa / 10).ToString();
最終效果