WinAPI method SetWindowText does not change the button text. Moreover, if you try to get the text through GetWindowText, it will return, exactly, the text that I tried to set, but not displayed after the fact. It's funny, but the form header changes, unlike the button, calling a method from another thread (with CheckForIlligalCross turned off) and the Invalidate buttons are not expected to work.

Upd. Code examples:

WinAPI class

[DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool SetWindowText(IntPtr hWnd, [MarshalAs(UnmanagedType.LPTStr)] string lpString); 

Window class

 public static Window Find(IntPtr hwnd) { return new Window() { Handle = hwnd }; } public string GetText() { int capacity = WinAPI.GetWindowTextLength(new HandleRef(this, Handle)) * 2; StringBuilder stringBuilder = new StringBuilder(capacity); WinAPI.GetWindowText(new HandleRef(this, Handle), stringBuilder, stringBuilder.Capacity); return stringBuilder.ToString(); } public bool SetText(string text) { return WinAPI.SetWindowText(Handle, text); } 

Form Code:

 button1.Click += (s, e) => { var btn = Window.Find(button1.Handle); btn.SetText("hello"); MessageBox.Show(btn.GetText()); }; 

The result on the screen.

Screen

  • Show your code, and preferably the minimum reproducible example . - VladD
  • Updated. I apologize for a little redundant code, but everything happens in as many as two projects. - Uranus
  • btn.SetText("hello"); - where is Button.SetText ? - Igor
  • A little did not understand the comments, can you elaborate more? - Uranus

0