Select text in NumericUpDown control

P

PeterB

Is it possible to select text in a NumericUpDown control, just like you use
SelectionStart and SelectionLength in a Text box??

I've seen questions about this for the full Framework so I guess it doesn't
work without some kind of workaround :)

regards,

Peter
 
A

Alex Feinman [MVP]

Numeric Up/Down control is a combination of two controls. There is a real
edit control inside it. Try getting its hWnd (as usually Capture/GetCapture)
and then sending it UDM_GETBUDDY to obtain hWnd of the edit control. Then
you can send it EM_SETSEL. Haven't tried it myself, but it should work
 
P

PeterB

Ok thanks, I seem to have some issues with the SendMessage function using
the UDM_GETBUDDY message, is there anything obvious I am doing wrong in my
code? (I'm using csharp)

public const UInt32 WM_USER = 0x0400;
public const UInt32 EM_SETSEL = 0x00B1;
public const UInt32 UDM_GETBUDDY = (WM_USER+106);

[DllImport("coredll.dll", SetLastError=true)]
private extern static IntPtr GetCapture();

[DllImport("coredll.dll", EntryPoint="SendMessage", SetLastError=true)]
private extern static IntPtr SendMessage(IntPtr hWnd, UInt32 nMsg, Int32
wParam,
Int32 lParam);

private void SelectText( NumericUpDown ctrl )
{
ctrl.Capture = true;
IntPtr hWndCtrl = GetCapture();
ctrl.Capture = false;
IntPtr hWndBuddy = SendMessage(hWndCtrl, UDM_GETBUDDY, 0, 0);
SendMessage(hWndBuddy, EM_SETSEL, 0, -1); // Select all text
}

The problem seem to start with the GetCapture function. The result from it
doesn't match the m_hwn value of the NumUpDown control, when I take a
"QuickLook". The fact I am using an IntPtr could the reason for this.
Atleast GetCapture returns a valid value.

The second problem is that SendMessage with UDM_GETBUDDY returns 0. I am not
sure I have done a correct implementation of SendMessage wrapper...

thanks,

Peter

PS 0 and -1 in SendMessage with EM_SETSEL means selecting all text according
to the SDK docs. DS
 
A

Alex Feinman [MVP]

It appears that CF implements a custom updown control, so UDM_GETBUDDY does nothing. Here is a workaround:

void SetFocusToUpDown(UpDownBase ctl)
{
ctl.Capture = true;
IntPtr hWnd = GetCapture();
ctl.Capture = false;
IntPtr hBuddy = GetWindow(hWnd, GW_CHILD);
SetFocus(hBuddy);
SendMessage(hBuddy, EM_SETSEL, 0, -1);
}

const int EM_SETSEL = 0x00B1;
const int WM_USER = 0x400;
const int UDM_GETBUDDY = (WM_USER+106);
const int GW_CHILD = 5;

[DllImport("coredll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

[DllImport("coredll")]
public static extern IntPtr GetCapture();

[DllImport("coredll")]
public static extern IntPtr GetWindow(IntPtr hWnd, int nIndex);

[DllImport("coredll")]
public static extern void SetFocus(IntPtr hWnd);


--
Alex Feinman
---
Visit http://www.opennetcf.org
PeterB said:
Ok thanks, I seem to have some issues with the SendMessage function using
the UDM_GETBUDDY message, is there anything obvious I am doing wrong in my
code? (I'm using csharp)

public const UInt32 WM_USER = 0x0400;
public const UInt32 EM_SETSEL = 0x00B1;
public const UInt32 UDM_GETBUDDY = (WM_USER+106);

[DllImport("coredll.dll", SetLastError=true)]
private extern static IntPtr GetCapture();

[DllImport("coredll.dll", EntryPoint="SendMessage", SetLastError=true)]
private extern static IntPtr SendMessage(IntPtr hWnd, UInt32 nMsg, Int32
wParam,
Int32 lParam);

private void SelectText( NumericUpDown ctrl )
{
ctrl.Capture = true;
IntPtr hWndCtrl = GetCapture();
ctrl.Capture = false;
IntPtr hWndBuddy = SendMessage(hWndCtrl, UDM_GETBUDDY, 0, 0);
SendMessage(hWndBuddy, EM_SETSEL, 0, -1); // Select all text
}

The problem seem to start with the GetCapture function. The result from it
doesn't match the m_hwn value of the NumUpDown control, when I take a
"QuickLook". The fact I am using an IntPtr could the reason for this.
Atleast GetCapture returns a valid value.

The second problem is that SendMessage with UDM_GETBUDDY returns 0. I am not
sure I have done a correct implementation of SendMessage wrapper...

thanks,

Peter

PS 0 and -1 in SendMessage with EM_SETSEL means selecting all text according
to the SDK docs. DS


Alex Feinman said:
Numeric Up/Down control is a combination of two controls. There is a real
edit control inside it. Try getting its hWnd (as usually Capture/GetCapture)
and then sending it UDM_GETBUDDY to obtain hWnd of the edit control. Then
you can send it EM_SETSEL. Haven't tried it myself, but it should work
 
P

PeterB

Works like a charm :)

I'm not sure the SetFocus call is necessary though, it seems to work without
it...

Thanks!

/ Peter


"Alex Feinman [MVP]" <[email protected]> skrev i meddelandet
It appears that CF implements a custom updown control, so UDM_GETBUDDY does
nothing. Here is a workaround:

void SetFocusToUpDown(UpDownBase ctl)
{
ctl.Capture = true;
IntPtr hWnd = GetCapture();
ctl.Capture = false;
IntPtr hBuddy = GetWindow(hWnd, GW_CHILD);
SetFocus(hBuddy);
SendMessage(hBuddy, EM_SETSEL, 0, -1);
}
const int EM_SETSEL = 0x00B1;
const int WM_USER = 0x400;
const int UDM_GETBUDDY = (WM_USER+106);
const int GW_CHILD = 5;
[DllImport("coredll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
int lParam);
[DllImport("coredll")]
public static extern IntPtr GetCapture();
[DllImport("coredll")]
public static extern IntPtr GetWindow(IntPtr hWnd, int nIndex);
[DllImport("coredll")]
public static extern void SetFocus(IntPtr hWnd);

--
Alex Feinman
---
Visit http://www.opennetcf.org
PeterB said:
Ok thanks, I seem to have some issues with the SendMessage function using
the UDM_GETBUDDY message, is there anything obvious I am doing wrong in my
code? (I'm using csharp)

public const UInt32 WM_USER = 0x0400;
public const UInt32 EM_SETSEL = 0x00B1;
public const UInt32 UDM_GETBUDDY = (WM_USER+106);

[DllImport("coredll.dll", SetLastError=true)]
private extern static IntPtr GetCapture();

[DllImport("coredll.dll", EntryPoint="SendMessage", SetLastError=true)]
private extern static IntPtr SendMessage(IntPtr hWnd, UInt32 nMsg, Int32
wParam,
Int32 lParam);

private void SelectText( NumericUpDown ctrl )
{
ctrl.Capture = true;
IntPtr hWndCtrl = GetCapture();
ctrl.Capture = false;
IntPtr hWndBuddy = SendMessage(hWndCtrl, UDM_GETBUDDY, 0, 0);
SendMessage(hWndBuddy, EM_SETSEL, 0, -1); // Select all text
}

The problem seem to start with the GetCapture function. The result from it
doesn't match the m_hwn value of the NumUpDown control, when I take a
"QuickLook". The fact I am using an IntPtr could the reason for this.
Atleast GetCapture returns a valid value.

The second problem is that SendMessage with UDM_GETBUDDY returns 0. I am not
sure I have done a correct implementation of SendMessage wrapper...

thanks,

Peter

PS 0 and -1 in SendMessage with EM_SETSEL means selecting all text according
to the SDK docs. DS


Alex Feinman said:
Numeric Up/Down control is a combination of two controls. There is a real
edit control inside it. Try getting its hWnd (as usually Capture/GetCapture)
and then sending it UDM_GETBUDDY to obtain hWnd of the edit control. Then
you can send it EM_SETSEL. Haven't tried it myself, but it should work
 

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