C
C#7mo ago
OnkelTem

DrawString doesn't update text / Windows Forms

Hi. I'm new to C# development. I create a control on a form and want to print its ClientRectangle. Whenever I resize the parent form, OnPaint gets called but text doesn't change. Yet the console message is of course changing. Why it happens?
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Black, ClientRectangle);
Console.WriteLine($"-->{ClientRectangle}");
e.Graphics.DrawString($"==>{ClientRectangle}", Font, Brushes.White, ClientRectangle);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Black, ClientRectangle);
Console.WriteLine($"-->{ClientRectangle}");
e.Graphics.DrawString($"==>{ClientRectangle}", Font, Brushes.White, ClientRectangle);
}
13 Replies
OnkelTem
OnkelTemOP7mo ago
I should clarify, that the text is getting printed one time, displaying the initial size, but it won't change aftewards
OnkelTem
OnkelTemOP7mo ago
No description
Memw
Memw7mo ago
I'd assume you need to use this because OnDraw seems to only be called when drawing for the first time and then you'd need to use the Resize event to change whatever you want to change
OnkelTem
OnkelTemOP7mo ago
Whose OnDraw? The OnPaint is called, that I can see in the console. I recorded a short video: https://youtu.be/zeRDKbGyxwU
OnkelTem
OnkelTemOP7mo ago
I recorded one more video with the problem
OnkelTem
OnkelTemOP7mo ago
Could someone have a look at this weird crap? This is so discouraging. A simple thing doesn't work If it starts this way then maybe C# or Windows Forms is just no the right tool
leowest
leowest7mo ago
after InitializeComponent add
SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
OnkelTem
OnkelTemOP7mo ago
Thanks! I also asked this question on StackOverflow
OnkelTem
OnkelTemOP7mo ago
Stack Overflow
Graphics.DrawString doesn't update text in OnPaint in Windows Forms C#
Sorry if I ask something simple. I asked in Discord and IRC and for two days nobody has provied a solution. I have the following form: public partial class Form1 : Form { public Form1() ...
leowest
leowest7mo ago
no worries, sometimes it takes time to have time to answer some questions, thankfully yours was detailed enough.
MODiX
MODiX7mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server