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. problem with custom control properties value

problem with custom control properties value

Scheduled Pinned Locked Moved C#
csharpwinformshelptutorialquestion
6 Posts 3 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.
  • L Offline
    L Offline
    Lyon Sun
    wrote on last edited by
    #1

    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

    L A 2 Replies Last reply
    0
    • L Lyon 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Add in your SET, if value<1 or value>100 -> throw exception.

      L 2 Replies Last reply
      0
      • L Lyon 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

        A Offline
        A Offline
        Abhishek Sur
        wrote on last edited by
        #3

        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 of CurrentLine

        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>

        L 1 Reply Last reply
        0
        • L Lost User

          Add in your SET, if value<1 or value>100 -> throw exception.

          L Offline
          L Offline
          Lyon Sun
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Abhishek Sur

            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 of CurrentLine

            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>

            L Offline
            L Offline
            Lyon Sun
            wrote on last edited by
            #5

            Hi, Sur, Thank you so much! This solved my problem. Really, thanks a lot!! Br! Sun

            1 Reply Last reply
            0
            • L Lost User

              Add in your SET, if value<1 or value>100 -> throw exception.

              L Offline
              L Offline
              Lyon Sun
              wrote on last edited by
              #6

              Hi, stancrm, You are right. I did this exception in the wrong place, and with a wrong exception name as well :rolleyes: . I noticed it after I read Sur's reply. Anyway, Thank you two!! A lot! Br! Sun

              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