setwindowtext does not set control text

  • Thread starter Thread starter de Kekse
  • Start date Start date
D

de Kekse

Guest
Hello,

is there anybody who can explain me why the function below in the timer section will not update the labelDateTimePinvoke by calling

the "SetWindowText(LabelPinvokeHandle, DateTime.Now.ToString());" function?

There is no error reported but the label is not updated.

The label labelDateTime is correct updated.

Regards de-Kekse



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

namespace TestFunctions
{
public partial class Form1 : Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.labelDateTime = new System.Windows.Forms.Label();
this.labelDateTimePinvoke = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// labelDateTime
//
this.labelDateTime.AutoSize = true;
this.labelDateTime.Location = new System.Drawing.Point(52, 59);
this.labelDateTime.Name = "labelDateTime";
this.labelDateTime.Size = new System.Drawing.Size(51, 20);
this.labelDateTime.TabIndex = 0;
this.labelDateTime.Text = "label1";
//
// labelDateTimePinvoke
//
this.labelDateTimePinvoke.AutoSize = true;
this.labelDateTimePinvoke.Location = new System.Drawing.Point(54, 150);
this.labelDateTimePinvoke.Name = "labelDateTimePinvoke";
this.labelDateTimePinvoke.Size = new System.Drawing.Size(51, 20);
this.labelDateTimePinvoke.TabIndex = 1;
this.labelDateTimePinvoke.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.labelDateTimePinvoke);
this.Controls.Add(this.labelDateTime);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}
private System.Windows.Forms.Label labelDateTime;
private System.Windows.Forms.Label labelDateTimePinvoke;

Timer TimeDate = new Timer();

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
public static extern bool SetWindowText(IntPtr hWnd, string Text);

IntPtr LabelHandle;
IntPtr LabelPinvokeHandle;

public Form1()
{
InitializeComponent();

LabelHandle = labelDateTime.Handle;
LabelPinvokeHandle = labelDateTimePinvoke.Handle;

TimeDate.Tick += TimeDate_Tick;
TimeDate.Interval = 500;
TimeDate.Enabled = true;
}

private void TimeDate_Tick(object sender, EventArgs e)
{
TimeDate.Enabled = false;

Control c = Control.FromHandle(LabelHandle);
c.Text = DateTime.Now.ToString();

bool Result = SetWindowText(LabelPinvokeHandle, DateTime.Now.ToString());
if(!Result)
{
int Error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
Debug.Write("Error:" + Error);
}

TimeDate.Enabled = true;
}
}
}

Continue reading...
 
Back
Top