TextBox Controls in WinForms and WPF

  • How to make a TextBox only accept numbers in WinForms:

You can handle the KeyPress event of the TextBox control and check if the input is a number. If it's not a number, you can set the Handled property of the KeyPressEventArgs parameter to true, to prevent the input from being entered. Here is an example:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

In the above example, we handle the KeyPress event of the TextBox control, and use the char.IsDigit method to check if the input is a number. If it's not a number, we set the Handled property of the KeyPressEventArgs parameter to true, to prevent the input from being entered.

  • How to make a TextBox only accept numeric input in WPF:

You can use the PreviewTextInput event of the TextBox control and check if the input is a number. If it's not a number, you can set the Handled property of the TextCompositionEventArgs parameter to true, to prevent the input from being entered. Here is an example:

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (!char.IsDigit(e.Text, e.Text.Length - 1))
    {
        e.Handled = true;
    }
}

In the above example, we handle the PreviewTextInput event of the TextBox control, and use the char.IsDigit method to check if the input is a number. If it's not a number, we set the Handled property of the TextCompositionEventArgs parameter to true, to prevent the input from being entered.

  • How to make a TextBox scroll to cursor in WinForms:

You can use the ScrollToCaret method of the TextBox control to scroll to the current cursor position. Here is an example:

private void textBox1_Click(object sender, EventArgs e)
{
    textBox1.ScrollToCaret();
}

In the above example, we handle the Click event of the TextBox control, and use the ScrollToCaret method to scroll to the current cursor position. This will make sure that the cursor is always visible when the user clicks on the TextBox.

  1. TextBox events in Windows Forms:

    • Describe handling events like TextChanged, KeyPress, etc.
    • Example code:
      private void textBox1_TextChanged(object sender, EventArgs e)
      {
          // Handle TextChanged event
      }
      
  2. Multiline TextBox in WinForms:

    • Enable multiline property in the TextBox.
    • Example code:
      textBox1.Multiline = true;
      
  3. TextBox input validation in C#:

    • Use events like Validating and Validated for validation.
    • Example code:
      private void textBox1_Validating(object sender, CancelEventArgs e)
      {
          // Validation logic
      }
      
  4. Password TextBox in Windows Forms:

    • Set the PasswordChar property for password input.
    • Example code:
      textBoxPassword.PasswordChar = '*';
      
  5. C# TextBox placeholder text in WinForms:

    • Use the GotFocus and LostFocus events to show/hide placeholder text.
    • Example code:
      private void textBox1_GotFocus(object sender, EventArgs e)
      {
          if (textBox1.Text == "Enter your text here")
              textBox1.Text = "";
      }
      
  6. Limiting characters in a TextBox in WinForms:

    • Handle KeyPress event and check the character count.
    • Example code:
      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (textBox1.Text.Length >= 10 && e.KeyChar != (char)Keys.Back)
              e.Handled = true;
      }
      
  7. Auto-complete TextBox in C# WinForms:

    • Use AutoCompleteCustomSource for auto-complete.
    • Example code:
      AutoCompleteStringCollection source = new AutoCompleteStringCollection();
      source.AddRange(new string[] { "Option1", "Option2", "Option3" });
      textBox1.AutoCompleteCustomSource = source;
      textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
      textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
      
  8. Customizing TextBox appearance in Windows Forms:

    • Modify properties like Font, BackColor, ForeColor, etc.
    • Example code:
      textBox1.Font = new Font("Arial", 12, FontStyle.Bold);
      textBox1.BackColor = Color.LightGray;
      textBox1.ForeColor = Color.Blue;
      

WPF Examples:

  1. TextBox control in WPF example:

    • Describe XAML syntax for TextBox in WPF.
    • Example code:
      <TextBox Name="textBox1" Text="Hello, WPF!" />
      
  2. TextBox events in WPF:

    • Utilize events like TextChanged in XAML.
    • Example code:
      <TextBox Name="textBox1" TextChanged="textBox1_TextChanged" />
      
  3. Multiline TextBox in WPF:

    • Set the AcceptsReturn property to true.
    • Example code:
      <TextBox Name="textBox1" AcceptsReturn="True" />
      
  4. TextBox input validation in XAML:

    • Use DataValidation in XAML.
    • Example code:
      <TextBox>
          <TextBox.Text>
              <Binding Path="YourProperty" UpdateSourceTrigger="PropertyChanged">
                  <Binding.ValidationRules>
                      <YourValidationRule />
                  </Binding.ValidationRules>
              </Binding>
          </TextBox.Text>
      </TextBox>
      
  5. Password TextBox in WPF:

    • Use the PasswordBox control for secure password entry.
    • Example code:
      <PasswordBox Name="passwordBox1" />
      
  6. WPF TextBox placeholder text:

    • Utilize a combination of VisualTreeHelper and GotFocus/LostFocus events.
    • Example code:
      // Handle GotFocus and LostFocus events to show/hide placeholder text
      
  7. Limiting characters in a TextBox in WPF:

    • Use PreviewTextInput event for character count limitation.
    • Example code:
      <TextBox Name="textBox1" PreviewTextInput="textBox1_PreviewTextInput" />
      
  8. Auto-complete TextBox in WPF:

    • Implement auto-complete functionality using behaviors or attached properties.
    • Example code:
      // Implement auto-complete logic
      
  9. Customizing TextBox appearance in XAML:

    • Modify TextBox properties like FontFamily, Background, Foreground, etc.
    • Example code:
      <TextBox Name="textBox1" FontFamily="Arial" Background="LightGray" Foreground="Blue" />