I have a code were a form with no border changes it size depending on a string.
It's like a notification form,
Some times the form does not resize correctly, and part of it looks transparent.
I know its transparent because that transparent part calls all the events, like click or mouse wheel even when I am seeing the program on the background of it. And have checked the Width property of the form and its OK, its greater than the part its showing.
Here is the code where the form changes its size, it's the only method where it changes color or size.
private void ChangeNotification(string Noti, Color C)
{
string[] Lines = Noti.Split(new[] { '\r', '\n' });
string Max = "";
Lines.ToList().ForEach(s =>
{
if (s.Length > Max.Length)
Max = s;
});
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
SizeF size = graphics.MeasureString(Max, lblInfo.Font);
Width = (int)size.Width + scroll.Width + 40;
Height = Lines.Count() * (int)size.Height;
Top = Screen.PrimaryScreen.WorkingArea.Height - Height;
Left = Screen.PrimaryScreen.WorkingArea.Width - Width;
}
this.BackColor = C;
lblInfo.Text = Noti;
}
I send the exact same text and some times it changes its size correctly.
Example Fails:
Example OK:
Just let the Label AutoSize to the contents of the String, then resize your form based on that. It works with multi-line strings as well. Why all the measuring?!...
public void ChangeNotification(string Noti, Color C)
{
lblInfo.Text = Noti;
this.BackColor = C;
this.Size = new Size(lblInfo.Size.Width + lblInfo.Location.X * 2, lblInfo.Size.Height + lblInfo.Location.Y * 2);
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, Screen.PrimaryScreen.WorkingArea.Height - Height);
}