problem with custom control properties value
-
Hi, all, I am doing with a custom control in WinForms, and there is one customed integer type property, say,
currentLine
, declared in that custom control class with the following code:private int currentLine; public int CurrentLine { get { return currentLine; } set { currentLine = value; } }
but this property's value should be restricted between 1 and 100. How to make this happen, so that a value smaller than 1, or bigger than 100 is rejected to be entered in properties window at designer time? Any suggestions are so much appreciated!! Thanks. Br! Sun
-
Hi, all, I am doing with a custom control in WinForms, and there is one customed integer type property, say,
currentLine
, declared in that custom control class with the following code:private int currentLine; public int CurrentLine { get { return currentLine; } set { currentLine = value; } }
but this property's value should be restricted between 1 and 100. How to make this happen, so that a value smaller than 1, or bigger than 100 is rejected to be entered in properties window at designer time? Any suggestions are so much appreciated!! Thanks. Br! Sun
-
Hi, all, I am doing with a custom control in WinForms, and there is one customed integer type property, say,
currentLine
, declared in that custom control class with the following code:private int currentLine; public int CurrentLine { get { return currentLine; } set { currentLine = value; } }
but this property's value should be restricted between 1 and 100. How to make this happen, so that a value smaller than 1, or bigger than 100 is rejected to be entered in properties window at designer time? Any suggestions are so much appreciated!! Thanks. Br! Sun
Leapsword Sun wrote:
public int CurrentLine { get { return currentLine; } set { currentLine = value; } }
change this to
public int CurrentLine
{
get { return currentLine; }
set {
if(value < 1 || value > 100) throw new ApplicationException("Invalid Input");
currentLine = value;
}
}This will throw an exception for invalid values. You can modify this according to your need. Just modify the
set
portion ofCurrentLine
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Microsoft Bing MAP using Javascript
CLR objects in SQL Server 2005
Uncommon C# Keywords/xml> -
thank you, stancrm, I've tried this, I added one method,
OnCurrentLineChanged()
in the SET of that property, and the method is as follows:private void OnCurrentLineChanged() { if (currentLine < 1 || currentLine > 100) { throw new System.ArgumentException("Property value is not valid", "original"); } }
However, I don't know for what reason, it doesn't work when I go designer window and try a number like 123. Any idea with this? Br! Sun
-
Leapsword Sun wrote:
public int CurrentLine { get { return currentLine; } set { currentLine = value; } }
change this to
public int CurrentLine
{
get { return currentLine; }
set {
if(value < 1 || value > 100) throw new ApplicationException("Invalid Input");
currentLine = value;
}
}This will throw an exception for invalid values. You can modify this according to your need. Just modify the
set
portion ofCurrentLine
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Microsoft Bing MAP using Javascript
CLR objects in SQL Server 2005
Uncommon C# Keywords/xml>