Jooj
Jooj

Reputation: 707

How to add button to textbox?

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

public partial class TextBoxButton : TextBox
{
    [Category("Button")]
    [Description("Button in textbox")]
    public Button Button
    {
        get
        {
            return this.btn;
        }
        set
        {
            this.btn = value;
        }
    }

    protected override void OnCreateControl()
    {
        if (!this.Controls.Contains(this.btn))
        {
            this.Controls.Add(this.btn);
            this.btn.Dock = DockStyle.Right;
        }

        base.OnCreateControl();
    }
}

Everytime when I start my appi and set some text or image in the button it's empty. Some ideas?

Best regards.

Upvotes: 0

Views: 19054

Answers (5)

user113476
user113476

Reputation:

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!

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942518

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.

Upvotes: 2

francescoSardo
francescoSardo

Reputation: 11

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

Upvotes: 1

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.

Upvotes: 0

TcKs
TcKs

Reputation: 26642

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

Upvotes: 6

Related Questions