2017-06-20 79 views
0

我使用流動代碼來創建TextBoxButton控件。當我在主類的「TextBoxButton_Click」void中編寫代碼時,它工作正常。但是當我將這個自定義控件拖放到窗體上時,如何在雙擊此控件時管理點擊事件?如何確定用戶點擊文本框或文本框內的按鈕?TextBoxButton單擊事件

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) 
     { 
         } 
    } 
} 

回答

0

您可以在自定義控件中創建新事件,在將控件放置到窗體上後可以訪問該事件。

public EventHandler TextBoxClick; 
public EventHandler ButtonClick; 

// Attach this handler to your TextBox.Click event 
private void TextBox_Click(object sender, EventArgs e) 
{ 
    if (TextBoxClick != null) 
     TextBoxClick(sender, e); 
} 

// Attach this handler to your Button.Click event 
private void Button_Click(object sender, EventArgs e) 
{ 
    if (ButtonClick != null) 
     ButtonClick(sender, e); 
} 

另一種方法是使你的文本框和按鈕public,但是這似乎是一個黑客攻擊的一位。

0

如果你想在Windows窗體設計器在默認情況下爲控制創造Click事件時,在窗體設計器TextBoxButton程序員雙擊,使用DefaultEvent屬性。

下面的代碼還演示瞭如何創建自己的Click事件,該事件在單擊該按鈕時引發。

[DefaultEvent("Click")] 
public class TextBoxButton : TextBox 
{ 
    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) 
    { 
     // Raise our own Click event 
     OnClick(); 
    } 

    public event EventHandler Click; 

    protected void OnClick() { 
     var handler = Click; 
     if (handler != null) 
     { 
      handler(this, EventArgs.Empty); 
     } 
    } 
0

你怎麼樣在構造函數中添加事件處理程序:

public TextBoxButton(){ 
//... 
//... 
mbutton.Click += Mbutton_Click; 
} 

private void Mbutton_Click(object sender, EventArgs e) { 
    MessageBox.Show("StackOverFlow"); 
}