Home All Groups Group Topic Archive Search About

Changing the text of a Command Button using SetWindowText

Author
1 Sep 2006 6:41 PM
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

Author
1 Sep 2006 7:05 PM
Herfried K. Wagner [MVP]
<supp***@airetec.com> 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/>
Author
1 Sep 2006 7:38 PM
support
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:

Show quoteHide quote
> <supp***@airetec.com> 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/>
Author
1 Sep 2006 9:22 PM
GhostInAK
Hello supp***@airetec.com,

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

-Boo

Show quoteHide quote
> 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:
>
>> <supp***@airetec.com> 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/>
Author
1 Sep 2006 10:20 PM
support
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


GhostInAK schrieb:

Show quoteHide quote
> Hello supp***@airetec.com,
>
> Why not simply pass the button as a parameter to your DLL ?
>
> -Boo
>
> > 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:
> >
> >> <supp***@airetec.com> 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/>
Author
1 Sep 2006 9:45 PM
Jay B. Harlow [MVP - Outlook]
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


<supp***@airetec.com> wrote in message
Show quoteHide quote
news:1157139501.091271.88450@74g2000cwt.googlegroups.com...
| 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:
|
| > <supp***@airetec.com> 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/>
|
Author
1 Sep 2006 9:49 PM
Jay B. Harlow [MVP - Outlook]
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


<supp***@airetec.com> wrote in message
Show quoteHide quote
news:1157136111.300218.80310@b28g2000cwb.googlegroups.com...
|
| 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
|
Author
1 Sep 2006 10:27 PM
support
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




Jay B. Harlow [MVP - Outlook] schrieb:

Show quoteHide quote
> 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
>
>
> <supp***@airetec.com> wrote in message
> news:1157136111.300218.80310@b28g2000cwb.googlegroups.com...
> |
> | 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
> |