How to add button to textbox?

前端 未结 5 1766
死守一世寂寞
死守一世寂寞 2021-01-16 16:35

I\'m trying to make a TextBox with a button on the right side. My code:

public partial class TextBoxButton : TextBox
{
    [Category(\"Button\")]
    [Descri         


        
5条回答
  •  情深已故
    2021-01-16 17:03

    If you really want to add the button to the textbox then follow these steps:

    • Create a new C# WinForms Project
    • Place new multiline textbox on the form
    • Paste the following code in the Load event:

      Button btn = new Button();
      btn.Parent = textBox1;
      btn.BringToFront();            
      textBox1.Controls.Add(btn);
      
      btn.BackColor = Color.Gray;
      btn.Text = "Help!";
      

    As you can see the button hides the unerlying text, however it does appear to be fully functional.

    I believe that what you really mean to do is to place the button along side the textbox. Perhaps use a Panel control to contain both of the controls.

    Also whenever you add a control to another controls - control collection you must set the parent control to the control you've added it to. Capiche? (Sorry for the wordiness ;)

    In other words, you are not setting the textbox as the parent control of the button.

    As an aside, WPF might have the ability to do this And to flow the text around the button!

提交回复
热议问题