一个WinForms文本框里面的按钮 [英] Button inside a winforms textbox

查看:133
本文介绍了一个WinForms文本框里面的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做的WinForms文本框有成为嵌入式按钮的任何属性,在盒子的结束,可能吗?

喜欢的东西收藏夹按钮在Chrome地址栏:

我也见过类似的东西在一些Excel的表格如下:


修改

我已经按照汉斯帕桑特的回答与另外一个click事件处理程序,它似乎工作确定:

 保护覆盖无效的OnLoad(EventArgs的发送){
        VAR BTN =新按钮();
        btn.Size =新尺寸(25,textBoxFolder.ClientSize.Height + 2);
        btn.Location =新的点(textBoxFolder.ClientSize.Width - btn.Width,-1);
        btn.Cursor = Cursors.Default;
        btn.Image = Properties.Resources.arrow_diagright;
        btn.Click + = btn_Click;
        textBoxFolder.Controls.Add(BTN);
        //发送到EM_SETMARGINS prevent从文字下方的按钮消失
        SendMessage消息(textBoxFolder.Handle,0xd3(IntPtr的)2,(IntPtr的)(btn.Width&所述;&下; 16));
        base.OnLoad(E);
    }    [System.Runtime.InteropServices.DllImport(user32.dll中)]
    私人静态外部的IntPtr SendMessage函数(IntPtr的的HWND,INT味精,IntPtr的WP,IntPtr的LP);    私人无效btn_Click(对象发件人,EventArgs的发送){
        的MessageBox.show(世界你好);
    }


解决方案

获取文本框里面的按钮,只需要它添加的框控件集合。您还需要做一些合理的prevent框消失在按钮下方内的文本;需要的PInvoke一丁点儿。像这样的:

 保护覆盖无效的OnLoad(EventArgs的发送){
        VAR BTN =新按钮();
        btn.Size =新尺寸(25,textBox1.ClientSize.Height + 2);
        btn.Location =新的点(textBox1.ClientSize.Width - btn.Width,-1);
        btn.Cursor = Cursors.Default;
        btn.Image = Properties.Resources.star;
        textBox1.Controls.Add(BTN);
        //发送到EM_SETMARGINS prevent从文字下方的按钮消失
        SendMessage消息(textBox1.Handle,0xd3(IntPtr的)2,(IntPtr的)(btn.Width&所述;&下; 16));
        base.OnLoad(E);
    }    [System.Runtime.InteropServices.DllImport(user32.dll中)]
    私人静态外部的IntPtr SendMessage函数(IntPtr的的HWND,INT味精,IntPtr的WP,IntPtr的LP);

这个样子,而我所测试的右边距(应该挑了prettier位图):

Do winforms textboxes have any properties that make an embedded button, at the end of the box, possible?

Something like the favorites button on the Chrome address box:

I've also seen something like the following in some Excel forms:


EDIT

I've followed Hans Passant's answer with the addition of a click event handler and it seem to work ok:

    protected override void OnLoad(EventArgs e) {
        var btn = new Button();
        btn.Size = new Size(25, textBoxFolder.ClientSize.Height + 2);
        btn.Location = new Point(textBoxFolder.ClientSize.Width - btn.Width, -1);
        btn.Cursor = Cursors.Default;
        btn.Image = Properties.Resources.arrow_diagright;
        btn.Click += btn_Click;     
        textBoxFolder.Controls.Add(btn);
        // Send EM_SETMARGINS to prevent text from disappearing underneath the button
        SendMessage(textBoxFolder.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);

    private void btn_Click(object sender, EventArgs e) {
        MessageBox.Show("hello world");
    }

解决方案

Getting the button inside the TextBox just requires adding it to the box' Controls collection. You'll also need to do something reasonable to prevent the text inside the box disappearing underneath the button; that requires a wee bit of pinvoke. Like this:

    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);

Looked like this while I tested the right margin (should have picked a prettier bitmap):

这篇关于一个WinForms文本框里面的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆