Reputation: 31
i`m vb.net programmer and i found solution for my problem but in c# on this address: Button inside a winforms textbox
I have converted code to vb.net and it works good except text goes underneath the button here is my converted and c# code, pls tell me where i`m wrong
C# CODE
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);
My VB.NET CODE
btn.Size = New Size(25, Me.ClientSize.Height + 2)
btn.Location = New Point(Me.ClientSize.Width - btn.Width - 1)
btn.FlatStyle = FlatStyle.Flat
btn.Cursor = Cursors.Default
btn.Image = Image.FromFile("C:\ansoft\Soljica\texture\tone.png")
btn.FlatAppearance.BorderSize = 0
textbox1.Controls.Add(btn)
SendMessage(textbox1.Handle, &HED3, CType(2, IntPtr), CType((btn.Width << 16), IntPtr))
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
End Function
What is wrong with vb.net code if anyone can tell me pls ?
For c# code credits goes to: Hans Passant
Upvotes: 2
Views: 4063
Reputation: 1820
It looks like some of the code related to Size
and Location
is wrong. Try this:
btn.Size = New Size(25, textBox1.ClientSize.Height + 2)
btn.Location = New Point(textBox1.ClientSize.Width - btn.Width - 1, -1)
btn.FlatStyle = FlatStyle.Flat
btn.Cursor = Cursors.Default
btn.Image = Image.FromFile("C:\ansoft\Soljica\texture\tone.png")
btn.FlatAppearance.BorderSize = 0
textBox1.Controls.Add(btn)
SendMessage(textBox1.Handle, &HD3, CType(2, IntPtr), CType((btn.Width << 16), IntPtr))
Upvotes: 3