C
C#6mo 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
OnkelTemOP6mo ago
I should clarify, that the text is getting printed one time, displaying the initial size, but it won't change aftewards
OnkelTem
OnkelTemOP6mo ago
No description
Memw
Memw6mo 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
OnkelTemOP6mo ago
Whose OnDraw? The OnPaint is called, that I can see in the console. I recorded a short video: https://youtu.be/zeRDKbGyxwU
OnkelTem
OnkelTemOP6mo ago
I recorded one more video with the problem
OnkelTem
OnkelTemOP6mo 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
leowest6mo 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
OnkelTemOP6mo ago
Thanks! I also asked this question on StackOverflow
OnkelTem
OnkelTemOP6mo 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
leowest6mo ago
no worries, sometimes it takes time to have time to answer some questions, thankfully yours was detailed enough.
MODiX
MODiX6mo 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