get rid of focus

S

Serge Wautier

Env.: WM6 / VS2008 / CF 3.5

Hi All,

I have this winform used to view info (not edit it). It contains a set of
labels and textboxes set in readonly mode.

The problem is that the focus is set automatically to first text box and the
caret is displayed. This is confusing to users because you think you can
type something in there. How can I get rid of this caret?

My dirty workaround is as such: ((Control)MyLabel).Focus();

-> Sets the focus to a label. It's dirty because Label hides Focus() (hence
the typecast). Which means I'm not supposed to use it. (BTW the compiler
emits a warning to remind me so).

Does anyone know of a more elegant/'legal' solution?

Note: Turning the textboxes into labels is not a good option here because
the form is sometimes used in 'view' mode and sometimes in 'edit' mode. And
I want to keep a single layout for both modes.

TIA,

Serge.
 
A

Armando Rocha

Hi,

focus on form dont work, but if you define textbox.enable = false; then
focus is not set.
 
A

Arun

Hi Serge,

Set the textbox tabstop property to false.
this.textBox1.TabStop = false;

But still it will allow user to tap on the textbox to get the focus,
you can add gotfocus event and put the focus to form, soemthing like
this.
This is because, you cannot capture mouse events and click event on CF
textbox control.

private void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1.TopLevelControl.Focus();
}

If you have multiple textboxes to do the same stuff, create a extended
textbox control and override the gotfocus.

public class TextBoxEx : TextBox
{

public TextBoxEx()
{
ReadOnly = true;
}

protected override void OnGotFocus(EventArgs e)
{
this.TopLevelControl.Focus();
base.OnGotFocus(e);
}

}

Hope this helps,
Cheers,
Arun
 

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