能够使用try-catch检查在自定义属性中输入的值

时间:2022-01-30 15:52:38

I've been created the user control based on Button and the custom property inside. Now I'm trying to create the ability to check the value entered in the custom property using try-catch on set in design mode of Visual Studio, but when I enter not Int32 value, it displays standart message box, not mine... How to solve this problem?

我已经基于Button和里面的自定义属性创建了用户控件。现在我正在尝试创建在Visual Studio的设计模式下使用try-catch设置检查自定义属性中输入的值的功能,但是当我输入的不是Int32值时,它会显示标准消息框,而不是我的...如何解决这个问题呢?

The code:

代码:

    private int timeDisplayTip;
    [CategoryAttribute("My properties")]
    public int TimeDisplayTip
    {
        get
        {
            return timeDisplayTip;
        }
        set
        {               
            try
            {
                if (Convert.ToInt32(value).ToString() != value.ToString())
                    throw new FormatException();

                timeDisplayTip = value;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

1 个解决方案

#1


1  

try this

尝试这个

private int timeDisplayTip;
[CategoryAttribute("My properties")]
public string TimeDisplayTip
{
    get
    {
        return timeDisplayTip.ToString();
    }
    set
    {
        try
        {
            if (Convert.ToInt32(value).ToString() != value.ToString())
                throw new FormatException();

            timeDisplayTip = Convert.ToInt32(value);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

hope it helps...

希望能帮助到你...

#1


1  

try this

尝试这个

private int timeDisplayTip;
[CategoryAttribute("My properties")]
public string TimeDisplayTip
{
    get
    {
        return timeDisplayTip.ToString();
    }
    set
    {
        try
        {
            if (Convert.ToInt32(value).ToString() != value.ToString())
                throw new FormatException();

            timeDisplayTip = Convert.ToInt32(value);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

hope it helps...

希望能帮助到你...