Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why i am getting MessageBox twice

Why i am getting MessageBox twice

Scheduled Pinned Locked Moved C#
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    netJP12L
    wrote on last edited by
    #1

    I want to user only enter integer for the employeeID property but when i enter alpha-numeric the messagebox appears twice. I want to display once what's wrong. [TypeConverter(typeof(TypeConverters.MyIntConverter)),Browsable(true)] public int EmployeeID { get { return _empID; } set { _empID = value; } } =========MyIntConverter Class============================== public class MyIntConverter : System.ComponentModel.Int32Converter { public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { return int.Parse(value.ToString()); } catch(Exception) { MessageBox.Show("Invalid Number"); return 0; } } return base.ConvertFrom(context, culture, value); } }

    C P V 3 Replies Last reply
    0
    • N netJP12L

      I want to user only enter integer for the employeeID property but when i enter alpha-numeric the messagebox appears twice. I want to display once what's wrong. [TypeConverter(typeof(TypeConverters.MyIntConverter)),Browsable(true)] public int EmployeeID { get { return _empID; } set { _empID = value; } } =========MyIntConverter Class============================== public class MyIntConverter : System.ComponentModel.Int32Converter { public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { return int.Parse(value.ToString()); } catch(Exception) { MessageBox.Show("Invalid Number"); return 0; } } return base.ConvertFrom(context, culture, value); } }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      This code will only show one, so it must be getting called twice. Try setting a breakpoint and checking the call stack to see why it's called twice. Also, if you're going to use a value, use as instead of is, it's more efficient. And I'd recommend int.TryParse instead of a try/catch.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      N 1 Reply Last reply
      0
      • N netJP12L

        I want to user only enter integer for the employeeID property but when i enter alpha-numeric the messagebox appears twice. I want to display once what's wrong. [TypeConverter(typeof(TypeConverters.MyIntConverter)),Browsable(true)] public int EmployeeID { get { return _empID; } set { _empID = value; } } =========MyIntConverter Class============================== public class MyIntConverter : System.ComponentModel.Int32Converter { public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { return int.Parse(value.ToString()); } catch(Exception) { MessageBox.Show("Invalid Number"); return 0; } } return base.ConvertFrom(context, culture, value); } }

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I'm guessing you have more than one event handler calling the method. At any rate, consider using a NumericUpDown rather than a TextBox, or write a KeyPress event handler to ignore characters you don't want.

        1 Reply Last reply
        0
        • C Christian Graus

          This code will only show one, so it must be getting called twice. Try setting a breakpoint and checking the call stack to see why it's called twice. Also, if you're going to use a value, use as instead of is, it's more efficient. And I'd recommend int.TryParse instead of a try/catch.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          N Offline
          N Offline
          netJP12L
          wrote on last edited by
          #4

          I feel like pulling my hairs have already spent 2 hours... I have only one propertyGrid on a form and even test on a brand new project still am getting the Messagebox twice. What i notice is that somehow it's coming twice in the Catch block. I do'nt know where else to dig.

          S 1 Reply Last reply
          0
          • N netJP12L

            I feel like pulling my hairs have already spent 2 hours... I have only one propertyGrid on a form and even test on a brand new project still am getting the Messagebox twice. What i notice is that somehow it's coming twice in the Catch block. I do'nt know where else to dig.

            S Offline
            S Offline
            Skippums
            wrote on last edited by
            #5

            Look at the call stack to see who is calling the method each time. One of the callers is presumably unintentional.

            Sounds like somebody's got a case of the Mondays -Jeff

            N 1 Reply Last reply
            0
            • S Skippums

              Look at the call stack to see who is calling the method each time. One of the callers is presumably unintentional.

              Sounds like somebody's got a case of the Mondays -Jeff

              N Offline
              N Offline
              netJP12L
              wrote on last edited by
              #6

              What i have discovered is that when the when the debugger hits to the MessageBox.Show inside the catch block it goes back to the start of the function and then start all over again. I couldn't figure out why is it behaving like this. If i remove the MessageBox.show then it works fine but i want to display my customize error message. Line 24 is where my MessageBox.show is defined ========Below are the call stack i got================== myHelp.exe!TypeConverters.MyIntConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context = {System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry EmployeeID}, System.Globalization.CultureInfo culture = {en-US}, object value = "9s") Line 33 C# [External Code] myHelp.exe!TypeConverters.MyIntConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context = {System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry EmployeeID}, System.Globalization.CultureInfo culture = {en-US}, object value = "9s") Line 24 + 0xb bytes C# [External Code] myHelp.exe!TypeConverters.MyIntConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context = {System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry EmployeeID}, System.Globalization.CultureInfo culture = {en-US}, object value = "9s") Line 24 + 0xb bytes C# [External Code] myHelp.exe!myHelp.Program.Main() Line 17 + 0x1a bytes C# [External Code] I get this message when i do MessageBox.Show(e.StackTrace.Tostring()); at System.Number.StringToNumber(String str,NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean ParseDecimal at System.Number.ParseInt32(string s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(string) at TypeConverters.MyIntConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) in Document\Project\MyIntConverter.cs line 20

              1 Reply Last reply
              0
              • N netJP12L

                I want to user only enter integer for the employeeID property but when i enter alpha-numeric the messagebox appears twice. I want to display once what's wrong. [TypeConverter(typeof(TypeConverters.MyIntConverter)),Browsable(true)] public int EmployeeID { get { return _empID; } set { _empID = value; } } =========MyIntConverter Class============================== public class MyIntConverter : System.ComponentModel.Int32Converter { public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { return int.Parse(value.ToString()); } catch(Exception) { MessageBox.Show("Invalid Number"); return 0; } } return base.ConvertFrom(context, culture, value); } }

                V Offline
                V Offline
                visualhint
                wrote on last edited by
                #7

                Hi, To investigate your issue, I tried your TypeConverter with my product, Smart PropertyGrid.Net. I also got the form twice and this is for this reason: the grid reacts to validation with the Enter key and also to the loss of focus. When Enter is pressed, your ConvertFrom method is called and the dialog box is displayed. Because the grid does not expect any UI to be shown at that moment, it also reacts to the loss of focus and tries to validate again the new value. Therefore, the ConvertFrom is called again and the form is displayed a second time. It won't happen indefinitely since at this time, the focus has been definitely lost. I guess that the MS PropertyGrid acts in the same way. By showing a message box, you are going against the way it's supposed to work. No UI should be displayed by a TypeConverter whose goal is just to convert. If it fails to convert a value, it's supposed to throw an exception and the grid will show it. Smart PropertyGrid has of course more flexible mechanisms to validate and show errors. Anyway, in SPG, I was able to stop this problem by defining a static bool isInsideConvertFrom. This way I was able to stop reentrant calls. Even if I think you should not show a UI from your converter, you can try this technique with the MS grid. Maybe it will work. However, I think your grid won't gain the focus back when the message box is closed. But this is to be tested. Best regards, Nicolas Cadilhac @ VisualHint Smart PropertyGrid.Net Microsoft PropertyGrid Resource List Free PropertyGrid for MFC Smart FieldPackEditor.Net / DateTimePicker

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups