Windows Develop Bookmark and Share   
 index > Windows Forms General > Delete - menu / textbox
 

Delete - menu / textbox

I have some textbox in my form, and I have some option in menu toat is fired with delete. When I press delete in textbox, my option from menu is fired instead of deleting text in textbox.

How to handle that?

bucz  Wednesday, September 12, 2007 3:57 PM

Hi bucz,

May way to do this is implement the IMessageFilter interface. This allows the application to capture a message before it is dispatched to a control or form. Try something like the following:

Code Snippet

MenuStripP

public partial class Form2 : Form, IMessageFilter

{

public Form2()

{

InitializeComponent();

Application.AddMessageFilter(this);

}

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)

{

MessageBox.Show("deleteing");

}

private void Form2_Load(object sender, EventArgs e)

{

this.deleteToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete;

}

#region const int WM_KEYDOWN = 0x100;

[DllImport("user32.dll", EntryPoint = "SendMessage")]

public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

public bool PreFilterMessage(ref Message m)

{

if (m.Msg == WM_KEYDOWN)

{

Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;

if (keyCode == Keys.Delete && (this.ActiveControl is TextBoxBase))

{

SendMessage(this.ActiveControl.Handle, (uint)m.Msg, (int)m.WParam, (int)m.LParam);//delete function in TextBox

return true;

}

}

return false;

}

#endregion

private void Form2_FormClosed(object sender, FormClosedEventArgs e)

{

Application.RemoveMessageFilter(this);

}

}

Best Regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, September 20, 2007 3:52 AM

I am trying to understand your problem. so you have a button to delete text in textbox?

then you should have event handler hooked to that button click event to delete text from textbox.

also you want the event hooked with text deleted not fired when you click that button, is that right?

I assume that you used TextChanged event on textbox, you probably need to check (it could be some flag) if the text change is fired from textbox or the delete button click then perform the operation only when it's from textbox itself.

H. _冬_ Tony  Wednesday, September 12, 2007 5:16 PM
I wasnt precise. I have some menu and in this menu there is an option that is fired when you press delete (deleting some objects on screen). In my form I have also a text box. When I enter some test to textbox and when I want to delete it - I press delete. But instead of deleting text in textbox - the option from my menu is fired. It is like this "delete" is directed to menu , not to textbox that I am editing
bucz  Thursday, September 13, 2007 5:44 AM

Hi bucz,

Based on your post, you want fire the menu item’s click event instead of deleting the text of a textbox when pressing the ‘Delete�key, right? If so, you can set the ShortcutKeys for the menu item. Try something like the following:

Code Snippet

.deleteToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete;

Best regards.
Rong-Chun Zhang.

Rong-Chun Zhang  Monday, September 17, 2007 7:29 AM
No. contrary to that Smile

I have set some menu option - deleting objects on DEL button, and when I am in some text and I want to delete text, this option is fired. But as far as I am in a textbox, I dont want this to happen
bucz  Wednesday, September 19, 2007 2:35 PM

Hi bucz,

May way to do this is implement the IMessageFilter interface. This allows the application to capture a message before it is dispatched to a control or form. Try something like the following:

Code Snippet

MenuStripP

public partial class Form2 : Form, IMessageFilter

{

public Form2()

{

InitializeComponent();

Application.AddMessageFilter(this);

}

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)

{

MessageBox.Show("deleteing");

}

private void Form2_Load(object sender, EventArgs e)

{

this.deleteToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete;

}

#region const int WM_KEYDOWN = 0x100;

[DllImport("user32.dll", EntryPoint = "SendMessage")]

public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

public bool PreFilterMessage(ref Message m)

{

if (m.Msg == WM_KEYDOWN)

{

Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;

if (keyCode == Keys.Delete && (this.ActiveControl is TextBoxBase))

{

SendMessage(this.ActiveControl.Handle, (uint)m.Msg, (int)m.WParam, (int)m.LParam);//delete function in TextBox

return true;

}

}

return false;

}

#endregion

private void Form2_FormClosed(object sender, FormClosedEventArgs e)

{

Application.RemoveMessageFilter(this);

}

}

Best Regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, September 20, 2007 3:52 AM

You can use google to search for other answers

Custom Search

More Threads

• Why this is not working? Method (Delegate, array<Object>[]()[])
• MenuStrip scrolls with hidden items and separators
• Tree view in windows application using c#
• PictureBox Image scaling issue
• BUG in NotifyIcon Control...!?
• changing propertygrid's description text at runtime
• cannot reload a listview once I have cleared all entries
• How to Photo Printing with Fullpage fax print and fullpage photo print programatically?
• Anyone know how to change color for a 3D button like this button control does:
• a simple printing problem, thanks for your help!