How to add button to textbox?

前端 未结 5 1735
死守一世寂寞
死守一世寂寞 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 16:58

    Instead of adding Button into the TextBox, use some container (Control or UserControl and add the TextBox and Button in in).

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-16 17:16

    Not sure what you mean by "in the text box". Do you mean you want to have the button covering the right side of the text box, or located to the right of the text box (but not overlapping)?

    I'm not sure what your requirements are, but I would probably do this in the Visual Designer rather than try to do it in code:

    1. create a new control
    2. create a table container with two columns and 1 row.
    3. Put the text box in the left cell, and the button in the right cell.
    0 讨论(0)
  • 2021-01-16 17:17

    You have to tell the designer that it should also serialize the properties of the button:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Button Button {
        //...
    }
    

    The default is Hidden so none of the button properties would be written to the Designer.cs file. Setting, say, the Text property works in the designer but the property value is lost after you start the app or reload the form.

    0 讨论(0)
  • 2021-01-16 17:24

    At first a Question: Do you need one or more Button at the side of your TextBox? So you need a Property Button or Buttons for one Button or a Collection

    If the Button is not so complex that you have to make a lot of specific functions, you use the UserControl. It is the right way. Otherwise you have to take a CustomControl, more work.

    Logical Steps:

    • Create one of the given Controls from above
    • Make a Property Button or Buttons
    • Use Docking-Layout, it is easier
    0 讨论(0)
提交回复
热议问题