I\'m trying to make a TextBox with a button on the right side. My code:
public partial class TextBoxButton : TextBox
{
[Category(\"Button\")]
[Descri
Instead of adding Button into the TextBox, use some container (Control or UserControl and add the TextBox and Button in in).
If you really want to add the button to the textbox then follow these steps:
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!
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:
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.
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: