Use MessageBox without using Form's libraries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    Use MessageBox without using Form's libraries

    Hi guys,

    I need show an exception with a MessageBox but it take place in one project which mustn't use Form libraries. Maybe something like this.

    Code:
    public static void Main()
            {
                try
                {
                    hallo(4);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    MessageBox.Show(ex);
                }
                
            }
    
            public static int hallo(int i)
            {
                int a;
                try
                {
                    int[] hola = {1, 2, 3};
                    a = hola[i];
                }
                catch (Exception ex){ throw ex; }
                return a;            
            }
    Thank you in advance.
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    #2
    Solved, using API MessageBox you void this.

    Comment

    • mbewers1
      New Member
      • Feb 2009
      • 68

      #3
      Are you designing a web form or a Windows Forms application?

      If it's the latter, I recommend using a DialogResult object to embed the Messagebox and it can be coded like this example:

      Code:
      DialogResult photoWarning =
                      MessageBox.Show("A photo already existis for this user." +
                              " Do you want to overwrite it with a new one?",
                               "Work Staff Directory",
                               MessageBoxButtons.YesNo,
                               MessageBoxIcon.Exclamation);
      However, I think its better to actually have a try/catch clause within your method as well as a message Box, if you have time to implement it.

      Cheers
      Matt

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by raulbolanos
        Solved, using API MessageBox you void this.
        That still uses form libraries, just not the .NET wrapper libraries

        Comment

        • mbewers1
          New Member
          • Feb 2009
          • 68

          #5
          Sorry for not reading all of your initial question raulbolanos but why, exactly, do you not want to use the library classes?

          Have you thought of using JavaScript boxes like so:

          Code:
          string msg = "<script> confirm('" + here is my message + "')</script>";
          Response.Write(msg);
          I know this works on web forms but I don't know if it does on Windows forms.
          Worth a try though

          Matt

          Comment

          • IanWright
            New Member
            • Jan 2008
            • 179

            #6
            mbewers1,

            Javascript code certainly won't work on WinForms.

            Comment

            Working...