[C#] Is there a way to prevent a Button to getting the focus?

E

EricL

C# question:

Is there a way to prevent a button from getting the focus and from being
painted with a rectangle of small dots when clicking on it?

Specifically, I have a form with 2 TextBox and a Button: say I am in the
first TextBox and the Button is the next control in the form.

What do I have to do so that tabbing out of the first TextBox skips the
Button over and sets focus to the second Textbox?

Thanks for help.

Eric
 
G

Guest

Is there a way to prevent a button from getting the focus and from being
painted with a rectangle of small dots when clicking on it?

Set the button's TabStop property to False.
 
N

Norman Yuan

You simply need to set TabIndex and/or TabStop property of a control (in
Properties Window, or menu "View->Tab Order").
 
E

EricL

Yes, thank you. I had tried that.

But that's not enough.

I'd also like to avoid seeing the small dots signifying focus, appear on the
button when I click on it.

Is that possible?

Thanks.
 
M

Morten Wennevik

Hi Eric,

You can handle the Click event and pass the focus somewhere else.

this.GetNextControl(button2, false).Focus();

It needs a tiny bit of coding to get it to work intelligently, but you will always get the dottet rectangle whenever you hold the mouse button down.

If you don't want the rectangle whatsoever I suggest you create a dummy button out of a label, panel or even Control.
 
G

Guest

Eric,

You could use interoperablity services to get at some features available in
the user32.dll. Look at the following code for some ideas. The public
methods below allow you to control the appearance of focus effects on
controls.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using namespace DllImports
{
public class User32DllImports
{
public User32DllImports()
{}

protected const int WM_CHANGEUISTATE = 0x00000127;
protected const int UIS_SET = 1;
protected const int UIS_CLEAR = 2;

protected const short UISF_HIDEFOCUS = 0x0001;
protected const short UISF_HIDEACCEL = 0x0002;
protected const short UISF_ACTIVE = 0x0004;

[DllImport( "user32.dll" )]
public extern static int SendMessage( IntPtr hWnd, int wMsg, int
wParam, int lParam );

public static void MakeAcceleratorsVisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR,
UISF_HIDEACCEL), 0 );
}

public static void MakeAcceleratorsInvisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET,
UISF_HIDEACCEL), 0 );
}

public static void MakeFocusVisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR,
UISF_HIDEFOCUS), 0 );
}

public static void MakeFocusInvisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET,
UISF_HIDEFOCUS), 0 );
}

public static void MakeActiveVisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET,
UISF_ACTIVE), 0 );
}

public static void MakeActiveInvisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR,
UISF_ACTIVE), 0 );
}

private static short LOWORD( int dw )
{
short loWord = 0;
ushort andResult = (ushort)(dw & 0x00007FFF);
ushort mask = 0x8000;
if ( (dw & 0x8000) != 0 )
{
loWord = (short)( mask | andResult );
}
else
{
loWord = (short)andResult;
}
return loWord;
}

private static int MAKELONG( int wLow, int wHigh )
{
int low = (int)LOWORD( wLow );
short high = LOWORD( wHigh );
int product = 0x00010000 * (int)high;
int makeLong = (int)( low | product );
return makeLong;
}
}
}

Hope that helps,
Dave
 
E

EricL

Dave,

Thank you very much.

Eric

Dave Leach said:
Eric,

You could use interoperablity services to get at some features available
in
the user32.dll. Look at the following code for some ideas. The public
methods below allow you to control the appearance of focus effects on
controls.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using namespace DllImports
{
public class User32DllImports
{
public User32DllImports()
{}

protected const int WM_CHANGEUISTATE = 0x00000127;
protected const int UIS_SET = 1;
protected const int UIS_CLEAR = 2;

protected const short UISF_HIDEFOCUS = 0x0001;
protected const short UISF_HIDEACCEL = 0x0002;
protected const short UISF_ACTIVE = 0x0004;

[DllImport( "user32.dll" )]
public extern static int SendMessage( IntPtr hWnd, int wMsg, int
wParam, int lParam );

public static void MakeAcceleratorsVisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR,
UISF_HIDEACCEL), 0 );
}

public static void MakeAcceleratorsInvisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET,
UISF_HIDEACCEL), 0 );
}

public static void MakeFocusVisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR,
UISF_HIDEFOCUS), 0 );
}

public static void MakeFocusInvisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET,
UISF_HIDEFOCUS), 0 );
}

public static void MakeActiveVisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET,
UISF_ACTIVE), 0 );
}

public static void MakeActiveInvisible( Control c )
{
SendMessage( c.Handle, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR,
UISF_ACTIVE), 0 );
}

private static short LOWORD( int dw )
{
short loWord = 0;
ushort andResult = (ushort)(dw & 0x00007FFF);
ushort mask = 0x8000;
if ( (dw & 0x8000) != 0 )
{
loWord = (short)( mask | andResult );
}
else
{
loWord = (short)andResult;
}
return loWord;
}

private static int MAKELONG( int wLow, int wHigh )
{
int low = (int)LOWORD( wLow );
short high = LOWORD( wHigh );
int product = 0x00010000 * (int)high;
int makeLong = (int)( low | product );
return makeLong;
}
}
}

Hope that helps,
Dave

EricL said:
Yes, thank you. I had tried that.

But that's not enough.

I'd also like to avoid seeing the small dots signifying focus, appear on
the
button when I click on it.

Is that possible?

Thanks.
 

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