0

I have a custom MessageBox that displays at the bottom right of the screen, but when the message is a bit long it doesn't display in full.

I've read several solutions here, but these allow manual line breaks by modifying the message itself.

However, I have several messages (Info, Alert, Error, etc.) and I'm looking for a solution so that the messages aren't cut off and are automatically rewound without changing each message.

Here is the code for the customized messageBox:

public void showAlert(string msg, enmType type)
{
    this.Opacity = 0.0;
    this.StartPosition = FormStartPosition.Manual;
    string fname;

    for (int i = 1; i < 10; i++)
    {
        fname = "alert" + i.ToString();
        Form_Alert frm = (Form_Alert)Application.OpenForms[fname];

        if (frm == null)
        {
            this.Name = fname;
            this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 15;
            this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
            this.Location = new Point(this.x, this.y);
            break;
        }
    }
    this.x = Screen.PrimaryScreen.WorkingArea.Width - base.Width - 5;

    switch(type)
    {
        case enmType.Success:
            this.pictureBox1.Image = Resources.success;
            this.BackColor = Color.SeaGreen;
            break;
        case enmType.Error:
            this.pictureBox1.Image = Resources.error;
            this.BackColor = Color.DarkRed;
            break;
        case enmType.Info:
            this.pictureBox1.Image = Resources.info;
            this.BackColor = Color.RoyalBlue;
            break;
        case enmType.Warning:
            this.pictureBox1.Image = Resources.warning;
            this.BackColor = Color.DarkOrange;
            break;
    }

    this.lblMsg.Text = msg;

    this.Show();
    this.action = enmAction.start;
    this.timer1.Interval = 1;
    this.timer1.Start();
}

I've tried "\n" and "Environment.NewLine" in the message itself, but I want the line break to be automatic.

Florian
  • 1,019
  • 6
  • 22
  • You didn't show any customized MessageBox, only code that opens an application form. A MessageBox is something *very* specific, provided by Windows itself, recorded in Event Viewer and allows copying the entire contents for easier troubleshooting. – Panagiotis Kanavos Jun 08 '23 at 08:56
  • 1
    You should check https://stackoverflow.com/questions/9509147/label-word-wrapping – kipy Jun 08 '23 at 09:01
  • `displays at the bottom right of the screen` that would be a popup, not a message box. In its simplest form, that's provided out of the boxy through [NotifyIcon.ShowBaloonTip](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.notifyicon.showballoontip?view=windowsdesktop-7.0) which displays a simple message, with a simple delay. `notifyIcon1.ShowBalloonTip(30000);` is enough to display the message for 30 seconds. Since Vista more complex popups are possible too, including custom icons, text, styling – Panagiotis Kanavos Jun 08 '23 at 09:01
  • Thank you, yes sorry it's a custom popup. I've tried the solutions in the link above but the displayed text doesn't appear in full. – Laffont Magalie Jun 08 '23 at 09:46
  • https://stackoverflow.com/questions/4672978/how-to-autosize-the-height-of-a-label-but-not-the-width – Hans Passant Jun 08 '23 at 14:11
  • You could try setting the AutoSize property to true , the MaximumSize property to limit the width of the label, and the TextAlign property optionally to ContentAlignment.MiddleCenter . – wenbingeng-MSFT Jun 09 '23 at 08:28

0 Answers0