2

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

2
  • It looks like you added an 'E' when converting the message from C# to VB. &HED3 should be &HD3. Commented Nov 19, 2013 at 16:24
  • Tried but still dont work :( Commented Nov 19, 2013 at 20:55

1 Answer 1

3

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.