文本框值为Slider.Value

本文关键字:Slider Value 文本 | 更新日期: 2023-09-27 17:53:13

将滑块绑定到文本框:

textbox.Text = Slider1.Value.ToString();

但是如何将文本框绑定到滑块?

文本框值为Slider.Value

以@Dmitry为基础,尝试这样做:

private void textbox_TextChanged(object sender, EventArgs e)
{
   try
   {
      // Convert the text to a Double and assign to slider
      double value;
      if (double.TryParse(textbox.Text, out value))
      {
         Slider1.Value = value;
         // If the number is valid, display it in Black.
         textBox.ForeColor = Color.Black;
      }
      else
      {
         // If the number is invalid, display it in Red.
         textBox.ForeColor = Color.Red;
      }
   }
   catch
   {
      // If there is an error, display the text using the system colors.
      textBox.ForeColor = SystemColors.ControlText;
   }
}

,不要忘记将这个事件代码连接到TextChanged事件。

textbox.TextChanged += new System.EventHandler(this.textBox_TextChanged);

如果你谈论的是WPF,那么你可以在你的控制器上创建一个属性:

    private int _Text;
    public int Text
    {
        get
        {
            return this._Text;
        }
        set
        {
            if (this._Text != value)
            {
                this._Text = value;
                this.OnPropertyChanged("Text");
            }
        }
    }

然后像这样结合SliderTextBox:

<TextBox Text={Binding Text}" />
<Slider Value={Binding Text}" />

确保你的控制器是你的DataContext…在您的xaml.cs文件中设置它:

Controller = new MyController();
this.DataContext = Controller;