Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.
I'm trying to make a TextBox with two button on the right side.
How do i do?
thank you.

What I have tried:

protected override void OnLoad(EventArgs e) {
var btn = new Button();
btn.Size = new Size(25, textBox1.ClientSize.Height + 2);
btn.Location = new Point(textBox1.ClientSize.Width - btn.Width, -1);
btn.Cursor = Cursors.Default;
btn.Image = Properties.Resources.star;
textBox1.Controls.Add(btn);
// Send EM_SETMARGINS to prevent text from disappearing underneath the button
SendMessage(textBox1.Handle, 0xd3, (IntPtr)2, (IntPtr)(btn.Width << 16));
base.OnLoad(e);
}

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
Posted
Updated 23-Jul-16 1:00am
v2
Comments
[no name] 23-Jul-16 6:44am    
What is the Problem exactly?

I see several Problems, one of them you add the Button to the TextBox... Is this the OnLoad from the form?
Hadi Basiri 23-Jul-16 7:21am    
no.this is the onload from the usercontrol.
Richard MacCutchan 23-Jul-16 6:50am    
You should calculate the location relative to the textbox location in your form, not the textbox size. Then add the button to the form, not the textbox.
Ralf Meier 23-Jul-16 7:03am    
For me the goal is not clear - but if you try to do what I believe the it would be better to create your own UserControl which has the Textbox and both Buttons in it. Every action from the Buttons which should manipulate the Textbox (or it's content) could also be done with the methods you create which belong to the UserControl.
Perhaps you think about it and tell us, what you exactly want to do ...

1 solution

The way I would do it is to create a UserControl, which contains a textbox and two buttons.
 ____________  __  __
|  Text box  ||B1||B2|
 ------------  --  --
The textbox has it's Anchor property set to Top, Left, and Right, and the buttons have Anchor Top and Right.

That way, the textbox always has the buttons in the same position - to the right of the textbox - regardless of what happens to the control as a whole.
Add a few properties to access the text, set the button captions, and a few events to signal that the buttons have been clicked, and you are done.
[edit]I hate markdown! :mad:[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900