key to parent

A

Allen Anderson

I have a control that has a textbox embedded in it. This control is
sitting on a form that also has an ok button that is the default
button that closes the form (OK button). When that textbox has focus
and a user presses enter, the default button fires. So to stop this I
am overriding cmd key. However, the processing for the enter key
actually needs to happen in the parent. I have tried hooking the
onkey events and such from the textbox into the parent control but
none of them fire when there is a default button on the same form.

To fix this I could hang a delegate off the textbox and subscribe to
it from the parent and call it when the processcmdmessage is fire in
the textbox, but I'd rather avoid that. What I would really like to
do is find some way to send the key from the textbox to the parent
control cleanly through the message que. How exactly would I go about
doing this AND is this even the best approach for the end result that
I am needing?
 
J

Jeffrey Tan[MSFT]

Hi Allen,

Based on my understanding, you want to eliminate the Default button
behavior in a textbox which is embeded in a usercontrol.

Actually, there is an AcceptsReturn property in TextBox control, which
indicats whether pressing ENTER in a multiline TextBox control creates a
new line of text in the control or activates the default button for the
form. But this property only take effect with multiline TextBox, not
singleline textbox.

For this issue, I think the suitable way to workaround is something like
you do:
1. In the usercontrol, expose an AcceptsReturn property for the user of
usercontrol to control the ENTER key of multiline TextBox behavior for
Default button.(This proeprty just wraps TextBox's AcceptsReturn property)

2. Create a public event for textbox at usercontrol level, which is called
in TextBox.ProcessCmdKey method. The ProcessCmdKey method determines if
textbox is in singleline mode, and if the form developer(who uses the
usercontrol) wanted to disable the enter key, return true to disable the
behavior.

Code snippet lists below:

// In UserControl
public event KeyEventHandler TextBoxKeyDown;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(TextBoxKeyDown!=null)
{
KeyEventArgs kea=new KeyEventArgs(keyData);
TextBoxKeyDown(this.textBox1, kea);
if((this.textBox1.Multiline==false)&&(kea.Handled==true))
{
return true;
}
}
return base.ProcessCmdKey (ref msg, keyData);
}

public bool AcceptsReturn
{
get
{
return this.textBox1.AcceptsReturn;
}
set
{
this.textBox1.AcceptsReturn=value;
}
}

So in the form, we may disable the behavior through both setting the
AcceptsReturn to true and handle userControl.TextBoxKeyDown event:
private void userControl11_TextBoxKeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
e.Handled=true;
}
}
======================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
A

Allen Anderson

Hi Jeff, thanks for the response. I am trying to stop default button
behavior for controls (not always a textbox) that are embedded on my
main control. What I'm trying to find out is if there is a way to
send a keystroke directly to a control. As I said in my earlier
statement, I wanted to avoid having to add an additional delegate and
subscribe to that. I want to just push a keystroke directly down to
the parent control.

maybe it would help if I explained exactly what I'm doing. I have a
treelist control that you can embed things like datetime pickers,
textboxes, numeric up/down controls, etc. When they double click on a
cell in the listview, it brings up that control on the listview
surface. If someone presses the default button while that sub control
is active, I want to 'close' that control. Therefore, I'm trying to
stop the default button behavior while that sub control is up AND send
the key to the parent.
 
J

Jeffrey Tan[MSFT]

Hi Allen,

Thanks very much for your feedback!

Yes, I see your concern. I still have a question, as you said, you want to
disable the default button behavior, and send a enter key to the parent
treelist control, yes? What is the effect of the enter key in parent
control?

For your issue, I think we can override the parent control's ProcessCmdKey
method(not the textbox's), then in this method, just return true for the
enter key. Then when we pressed enter key in any of the child controls in
the parent control, it will be disabled to be dispatch to the default
button. So the default button behavior is disabled.

Then, we can call the enter key function directly in this method to
simulate the enter key effect.
======================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi,
Another solution would be to inherit form the text box and override
IsInputKey method. In the overriden mehtod return *true* if the keys is
Enter or pass to the base implementation for the rest of the keys. This way
the Enter will be processed by the text box rather than the form.

This would work for any control. The downside is that you need to inherit
those controls. I think Jeffrey's solution might be more suitable in your
case, but I just wanted to throw one more idea.
 
J

Jeffrey Tan[MSFT]

Hi Stoitcho,

Thanks for sharing useful information!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
A

Allen Anderson

Hi Jeff, well I guess I learn something new every day. I was unaware
of the ProcessCmdKey! I had thought PreProcessMsg would catch
anything that moved through the parent. Trapping that from the parent
control fixes my problem %100. I am able to catch the return, handle
it by destroying the embedded control and moving on. This is a
perfect solution for me since I don't have to build things into any
embedded control someone uses.

Thanks to you both for your good advice!

Cheers,
Allen
 
J

Jeffrey Tan[MSFT]

Hi Allen,

You are welcome!! I am glad my reply makse sense to you. If you need
further help, please feel free to tell me. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top