views:

1322

answers:

4

The question is: How to make default button be focused on form focus and response on "Enter" hit, but not focused when the caret is in textbox with multiline property set to true for example?..I know that i can do some exceptions in code, but maybe there is some "best practices" i dont know for now :( thank you

+4  A: 

Maybe I got you wrong, but what I would do is:

  1. Set the "AcceptButton" of the form to the Button you want to respond on "Enter"
  2. Set the "AcceptsReturn" of the textbox with multiline to true

et voila

Peter
Nice and simple, like it ;-p
Marc Gravell
yes, almost the answer i wanted :) thanks
nihi_l_ist
+3  A: 

A Windows Form has two properties: AcceptButton and CancelButton. You can set these to refer button controls on your form. The AcceptButton tells which button should be clicked when the user presses the enter key, while the Cancel button tells which button should be clicked when the user presses the escape key.

Often you will set the DialogResult of the AcceptButton to DialogResult.OK or DialogResult.Yes and DialogResult.Cancel or DialogResult.No for the CancelButton. This ensures that you can easily check which button was clicked if you dispay the form modally.

Rune Grimstad
(note I've replied to your question on *my* reply; this comment just so it appears on your "Responses" tab)
Marc Gravell
+3  A: 

(edit - the answer here is very good for TextBox; this pattern might be useful for other controls that lack the AcceptsReturn or equivalent)

You can use the GotFocus and LostFocus events to change the AcceptButton fairly easily, for example:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    TextBox multi, single;
    Button btn;
    using(Form form = new Form {
            Controls = {
                (multi= new TextBox { Multiline = true, Dock = DockStyle.Fill}),
                (btn = new Button { Text = "OK", Dock = DockStyle.Bottom,
                    DialogResult = DialogResult.OK}),
                (single = new TextBox { Multiline = false, Dock = DockStyle.Top}),
            }, AcceptButton = btn                
        })
    {
        multi.GotFocus += delegate { form.AcceptButton = null; };
        multi.LostFocus += delegate { form.AcceptButton = btn; };
        btn.Click += delegate { form.Close(); };
        Application.Run(form);
    }
}
Marc Gravell
What a nice way to build a simple form!
Rune Grimstad
thank you for this explanation
nihi_l_ist
@Rune - gotta love collection/object-initializers. The sneaky bit is the "(varName = [initializer])" so that you can hook the variable to events etc ;-p
Marc Gravell
Could you set the event handlers in the initializers? So instead of adding the delegate inside the using you could do it in the object initializer?
Rune Grimstad
@Rune - no, there is no syntax for that. It would be nice, and has been suggested (somewhat similar to collection initializers, presumably) - but no joy yet. I'm not holding my breath for C# 4.0, either.
Marc Gravell
@Marc: Too bad, but it still is a really elegant way to create a form
Rune Grimstad
+1  A: 

or you can do it in de focus event of your textbox as in

_targetForm.AcceptButton = _targetForm.btnAccept;

and then ubind it in the other textbox with multilines

Dean
thanks :) i think the first answer in this topic is the best and simpliest
nihi_l_ist