Changing the text of a Command Button using SetWindowText

  • Thread starter Thread starter support
  • Start date Start date
S

support

Hi,

I am trying to change the text of a Command button using the Windows
API Function SetWindowText, which I have declared as follows:

<DllImport("User32")> _
Public Function SetWindowText(ByVal hWnd As IntPtr, ByVal
lpString As String) As Boolean
End Function

To change the text I call the function as follows in the Load event of
the form.
SetWindowText(Me.Button1.Handle.ToInt32, "TEST")
Nothing appears to happen however when I examine the Button using Spy++
it tells me the window has the text "TEST".

When I change the text of a label as follows
SetWindowText(Me.Label1.Handle.ToInt32, "TEST")
it works as expected, i.e. the change is immediately visible

I know of course that I can change the text of the button using the
syntax Button1.Text="TEST" however my objective is to use SetWindowText
as I want to change the text later in an external DLL.

It looks to me like this may be a window update issue. I have tried
invalidating the window and repainting it with no success. Can anyone
help me out on this?

Thanks in advance

Joe
 
I am trying to change the text of a Command button using the Windows
API Function SetWindowText, which I have declared as follows:

<DllImport("User32")> _
Public Function SetWindowText(ByVal hWnd As IntPtr, ByVal
lpString As String) As Boolean
End Function

Specify the character set in 'DllImport' and set it to 'Auto'.
To change the text I call the function as follows in the Load event of
the form.
SetWindowText(Me.Button1.Handle.ToInt32, "TEST")

Remove the '.ToInt32'.
Nothing appears to happen however when I examine the Button using Spy++
it tells me the window has the text "TEST".

When I change the text of a label as follows
SetWindowText(Me.Label1.Handle.ToInt32, "TEST")
it works as expected, i.e. the change is immediately visible
It looks to me like this may be a window update issue.

Maybe the reason is that .NET draws the button. You may want to test it
with native Win32 buttons and/or set the .NET button's 'FlatStyle' property
to 'System'.
 
Hi,

thanks for your help.

I tried what you suggested, it worked when I set the .NET button's
'FlatStyle' property to 'System'. It is not an option for me to
change the property however.

Can I force .NET to redraw the button using a Windows API call? I have
tried InvalidateRect and UpdateWindow without any luck.

Regards
Joe
 
Hello (e-mail address removed),

Why not simply pass the button as a parameter to your DLL ?

-Boo
 
Joe,
Is there a reason you need to use SetWindowText as opposed to simply using
Control.Text?

| Can I force .NET to redraw the button using a Windows API call? I have
| tried InvalidateRect and UpdateWindow without any luck.
I'm not certain, however I would question the need to, as Control.Text
causes the button to be redrawn...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi,
|
| thanks for your help.
|
| I tried what you suggested, it worked when I set the .NET button's
| 'FlatStyle' property to 'System'. It is not an option for me to
| change the property however.
|
| Can I force .NET to redraw the button using a Windows API call? I have
| tried InvalidateRect and UpdateWindow without any luck.
|
| Regards
| Joe
|
| Herfried K. Wagner [MVP] schrieb:
|
| > > I am trying to change the text of a Command button using the Windows
| > > API Function SetWindowText, which I have declared as follows:
| > >
| > > <DllImport("User32")> _
| > > Public Function SetWindowText(ByVal hWnd As IntPtr, ByVal
| > > lpString As String) As Boolean
| > > End Function
| >
| > Specify the character set in 'DllImport' and set it to 'Auto'.
| >
| > > To change the text I call the function as follows in the Load event of
| > > the form.
| > > SetWindowText(Me.Button1.Handle.ToInt32, "TEST")
| >
| > Remove the '.ToInt32'.
| >
| > > Nothing appears to happen however when I examine the Button using
Spy++
| > > it tells me the window has the text "TEST".
| > >
| > > When I change the text of a label as follows
| > > SetWindowText(Me.Label1.Handle.ToInt32, "TEST")
| > > it works as expected, i.e. the change is immediately visible
| >
| > > It looks to me like this may be a window update issue.
| >
| > Maybe the reason is that .NET draws the button. You may want to test it
| > with native Win32 buttons and/or set the .NET button's 'FlatStyle'
property
| > to 'System'.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
 
Doh! missed the lower half of the question here.


| I know of course that I can change the text of the button using the
| syntax Button1.Text="TEST" however my objective is to use SetWindowText
| as I want to change the text later in an external DLL.
I missed the part of the external DLL.

Is the external DLL written in unmanaged C/C++ or is it a managed (VB, C#)
dll?

If its a managed Dll; Why not simply pass Button or Control to the external
DLL allowing it to use Control.Text?

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|
| Hi,
|
| I am trying to change the text of a Command button using the Windows
| API Function SetWindowText, which I have declared as follows:
|
| <DllImport("User32")> _
| Public Function SetWindowText(ByVal hWnd As IntPtr, ByVal
| lpString As String) As Boolean
| End Function
|
| To change the text I call the function as follows in the Load event of
| the form.
| SetWindowText(Me.Button1.Handle.ToInt32, "TEST")
| Nothing appears to happen however when I examine the Button using Spy++
| it tells me the window has the text "TEST".
|
| When I change the text of a label as follows
| SetWindowText(Me.Label1.Handle.ToInt32, "TEST")
| it works as expected, i.e. the change is immediately visible
|
| I know of course that I can change the text of the button using the
| syntax Button1.Text="TEST" however my objective is to use SetWindowText
| as I want to change the text later in an external DLL.
|
| It looks to me like this may be a window update issue. I have tried
| invalidating the window and repainting it with no success. Can anyone
| help me out on this?
|
| Thanks in advance
|
| Joe
|
 
Hi,

that is how my DLL is supposed to work. It takes the window handle as a
parameter and changes the text using the Windows API call
SetWindowText.

That doesn't work either though, so to localise the problem I
simplified it to a direct call to SetWindowText in vb.net.

Joe
 
Hi Jay,

its an unmanaged C++ DLL.

The curious thing is if I set the text of the button using
SetWindowText to say "XYZ" and then read it back using GetWindowText I
get "XYZ" as a result.

It just doesn't display the text I set even if I repaint the screen. If
however I change the flatstyle of the button to system, then it works.
Problem is that is not an option.


Regards
Joe
 
Back
Top