What is the difference between these three ways to clear a Textbox?

前端 未结 10 539
孤城傲影
孤城傲影 2021-02-01 17:11

I am bit confused between the below three ways to clear the contents of a textbox. I am working with WPF and found All are working, but I am unable to find the difference.

10条回答
  •  心在旅途
    2021-02-01 17:38

    If not going really deep:

    Clear: remove content from TextBox and may be delete resources allocated with it

        public void Clear()
        {
          using (this.TextSelectionInternal.DeclareChangeBlock())
          {
            this.TextContainer.DeleteContentInternal(this.TextContainer.Start, this.TextContainer.End);
            this.TextSelectionInternal.Select(this.TextContainer.Start, this.TextContainer.Start);
          }
        }
    

    Assigning empty string (because string.Empty and "" are equal) to Text property just assign empty string to attached property TextBox.TextProperty:

    public string Text
    {
      get
      {
        return (string) this.GetValue(TextBox.TextProperty);
      }
      set
      {
        this.SetValue(TextBox.TextProperty, (object) value);
      }
    }
    

提交回复
热议问题