【发布时间】:2026-01-23 17:10:01
【问题描述】:
我使用流动代码来创建 TextBoxButton Control 。当我在主类的“TextBoxButton_Click”void 中编写代码时,它工作正常。但是当我在表单上拖动这个自定义控件时,当我双击这个控件时如何管理点击事件?如何确定用户点击的文本框或文本框内的按钮?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomControl
{
public class TextBoxButton : TextBox
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
public Button mbutton;
public Button button
{
get { return mbutton; }
set { this.mbutton = value; }
}
protected override void OnCreateControl()
{
this.Controls.Add(this.mbutton);
SetRightToLeft();
base.OnCreateControl();
}
private void SetRightToLeft()
{
if (RightToLeft == RightToLeft.Yes)
{
mbutton.Dock = DockStyle.Left;
}
else
{
mbutton.Dock = DockStyle.Right;
}
}
protected override void OnRightToLeftChanged(EventArgs e)
{
SetRightToLeft();
base.OnRightToLeftChanged(e);
}
public TextBoxButton()
{
mbutton = new Button();
mbutton.Width = 20;
mbutton.Cursor = Cursors.Hand;
mbutton.Click += TextBoxButton_Click;
// Send EM_SETMARGINS to prevent text from disappearing underneath the button
SendMessage(Handle, 0xd3, (IntPtr)2, (IntPtr)(mbutton.Width << 16));
}
private void TextBoxButton_Click(object sender, EventArgs e)
{
}
}
}
【问题讨论】:
标签: c# winforms events button textbox