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. Add a Property for a UserControls

Add a Property for a UserControls

Scheduled Pinned Locked Moved C#
csscomdesigntoolsxml
9 Posts 4 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.
  • P Offline
    P Offline
    Pedram Behroozi
    wrote on last edited by
    #1

    Hello all and Happy new Year :) I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value? I hope I could explain well. Thanks a lot :)

    I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

    P U 2 Replies Last reply
    0
    • P Pedram Behroozi

      Hello all and Happy new Year :) I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value? I hope I could explain well. Thanks a lot :)

      I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

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

      Have you tried it?

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        Have you tried it?

        P Offline
        P Offline
        Pedram Behroozi
        wrote on last edited by
        #3

        Actually I didn't know from where I should start. I tried constructor but I have error adding UC in my windows form. I defined the Label publicly but I could only access the Label this way: UserControl.Label. I searched MSDN for Property and Attribute but I couldn't find anything yet. Thanks for reply

        I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

        1 Reply Last reply
        0
        • P Pedram Behroozi

          Hello all and Happy new Year :) I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value? I hope I could explain well. Thanks a lot :)

          I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

          U Offline
          U Offline
          User 4467139
          wrote on last edited by
          #4

          You can override the Text property of the usercontrol so as to set the entered text into the label. Something like what is given below. [Browsable(true)] public override string Text { set { label1.Text = value; } } This will let you set the text in design time.

          P 2 Replies Last reply
          0
          • U User 4467139

            You can override the Text property of the usercontrol so as to set the entered text into the label. Something like what is given below. [Browsable(true)] public override string Text { set { label1.Text = value; } } This will let you set the text in design time.

            P Offline
            P Offline
            Pedram Behroozi
            wrote on last edited by
            #5

            Yes, that's the answer: [Browsable(true)] You made my day, thanks a lot :)

            I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

            1 Reply Last reply
            0
            • U User 4467139

              You can override the Text property of the usercontrol so as to set the entered text into the label. Something like what is given below. [Browsable(true)] public override string Text { set { label1.Text = value; } } This will let you set the text in design time.

              P Offline
              P Offline
              Pedram Behroozi
              wrote on last edited by
              #6

              :( I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:

              [Browsable(true)]
              public string LabelText
              {
              get { return label1.Text; }
              set { label1.Text = value; }
              }

              I couldn't understand what is the problem, do you have any idea?

              I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

              U W 2 Replies Last reply
              0
              • P Pedram Behroozi

                :( I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:

                [Browsable(true)]
                public string LabelText
                {
                get { return label1.Text; }
                set { label1.Text = value; }
                }

                I couldn't understand what is the problem, do you have any idea?

                I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

                U Offline
                U Offline
                User 4467139
                wrote on last edited by
                #7

                I just tried using the same code that u gave and it seems to work fine for me. Can you give me the code for the control and also the code for ur form. It might help.

                1 Reply Last reply
                0
                • P Pedram Behroozi

                  :( I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:

                  [Browsable(true)]
                  public string LabelText
                  {
                  get { return label1.Text; }
                  set { label1.Text = value; }
                  }

                  I couldn't understand what is the problem, do you have any idea?

                  I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  How is the label1 defined in your UC, is it static or any other reasons why it could be shared along the instances?

                  The need to optimize rises from a bad design.My articles[^]

                  P 1 Reply Last reply
                  0
                  • W Wendelius

                    How is the label1 defined in your UC, is it static or any other reasons why it could be shared along the instances?

                    The need to optimize rises from a bad design.My articles[^]

                    P Offline
                    P Offline
                    Pedram Behroozi
                    wrote on last edited by
                    #9

                    Solved Mika and Member 4470354, I think I was changed PropertyBinding, DatBinding or I don't know, something similar... :laugh: I reset the values. Thank you both.

                    I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

                    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