Button inside a WinForms textbox

前端 未结 7 1079
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 14:31

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 bo

相关标签:
7条回答
  • 2020-11-27 15:12

    Here's the answer wrapped in a TextBox subclass.

    public class ButtonTextBox : TextBox {
        private readonly Button _button;
    
        public event EventHandler ButtonClick { add { _button.Click += value; } remove { _button.Click -= value; } }
    
        public ButtonTextBox() {
            _button = new Button {Cursor = Cursors.Default};
            _button.SizeChanged += (o, e) => OnResize(e);
            this.Controls.Add(_button); 
        }
    
        public Button Button {
            get {
                return _button;
            }
        }
    
        protected override void OnResize(EventArgs e) {
            base.OnResize(e);
            _button.Size = new Size(_button.Width, this.ClientSize.Height + 2);
            _button.Location = new Point(this.ClientSize.Width - _button.Width, -1);
            // Send EM_SETMARGINS to prevent text from disappearing underneath the button
            SendMessage(this.Handle, 0xd3, (IntPtr)2, (IntPtr)(_button.Width << 16));
        }
    
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    
    }
    
    0 讨论(0)
提交回复
热议问题