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. read value from item array in user control

read value from item array in user control

Scheduled Pinned Locked Moved C#
8 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.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I have created a user control with txtFirstName and txtLastName fields on it. On my frmPassengers, I am populating the user control more than one time based on the number user decided to add like this:

    private void frmPassengerDetails_Load(object sender, EventArgs e)
    {
    this.Cursor = Cursors.WaitCursor;

    BaseLayoutItem prevItem = layoutControlPassenger.Root;
    
    for (int i = 1; i < total\_adults; i++)
    {
        ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
    
        LayoutControlItem item = layoutControlPassenger.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
        item.TextVisible = false;
        item.SizeConstraintsType = SizeConstraintsType.Custom;
        item.MinSize = new Size(830, 75);
        item.MaxSize = new Size(830, 75);
        prevItem = item;
    }
    

    }

    so if user choosed 5 for the total_adult value then the user control will be populated five times. Now I want to know how can I read the value of every txtFirstName and txtLastName on the form (the five of it for example)..

    Technology News @ www.JassimRahma.com

    L K 3 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I have created a user control with txtFirstName and txtLastName fields on it. On my frmPassengers, I am populating the user control more than one time based on the number user decided to add like this:

      private void frmPassengerDetails_Load(object sender, EventArgs e)
      {
      this.Cursor = Cursors.WaitCursor;

      BaseLayoutItem prevItem = layoutControlPassenger.Root;
      
      for (int i = 1; i < total\_adults; i++)
      {
          ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
      
          LayoutControlItem item = layoutControlPassenger.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
          item.TextVisible = false;
          item.SizeConstraintsType = SizeConstraintsType.Custom;
          item.MinSize = new Size(830, 75);
          item.MaxSize = new Size(830, 75);
          prevItem = item;
      }
      

      }

      so if user choosed 5 for the total_adult value then the user control will be populated five times. Now I want to know how can I read the value of every txtFirstName and txtLastName on the form (the five of it for example)..

      Technology News @ www.JassimRahma.com

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

      Add a list to your form (e.g. List) to which you add your user controls as you create them. This avoids having to traverse the visual tree later to get at them. Once you're ready to read back the name values, iterate over the controls in the previously created list.

      J 1 Reply Last reply
      0
      • L Lost User

        Add a list to your form (e.g. List) to which you add your user controls as you create them. This avoids having to traverse the visual tree later to get at them. Once you're ready to read back the name values, iterate over the controls in the previously created list.

        J Offline
        J Offline
        Jassim Rahma
        wrote on last edited by
        #3

        sorry I didn't get you...

        Technology News @ www.JassimRahma.com

        L 1 Reply Last reply
        0
        • J Jassim Rahma

          sorry I didn't get you...

          Technology News @ www.JassimRahma.com

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

          txtFirstName and txtLastName are "text boxes" in your "user control"; right? To access the text box contents (.Text) you need a reference to the users controls and the text boxes; no? Aren't you creating these "user controls" on the fly and adding them to your (devExpress) form? If not, what was the purpose of the code you posted?

          J 1 Reply Last reply
          0
          • L Lost User

            txtFirstName and txtLastName are "text boxes" in your "user control"; right? To access the text box contents (.Text) you need a reference to the users controls and the text boxes; no? Aren't you creating these "user controls" on the fly and adding them to your (devExpress) form? If not, what was the purpose of the code you posted?

            J Offline
            J Offline
            Jassim Rahma
            wrote on last edited by
            #5

            You are right in all but in this case I am creating multiple controls from the same user control. The user control has just one txtFirstName but when users sets total_adults=5 in Form1 then five txtFirstName controls will be created on the Form2 Thats why I am asking how can I change the property class to return the value and from Form2 I need when clicking Save to save read the value of all fives txtFirstName controls. Thanks Jassim

            Technology News @ www.JassimRahma.com

            L 1 Reply Last reply
            0
            • J Jassim Rahma

              You are right in all but in this case I am creating multiple controls from the same user control. The user control has just one txtFirstName but when users sets total_adults=5 in Form1 then five txtFirstName controls will be created on the Form2 Thats why I am asking how can I change the property class to return the value and from Form2 I need when clicking Save to save read the value of all fives txtFirstName controls. Thanks Jassim

              Technology News @ www.JassimRahma.com

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

              Add a list to your form:

              list mylist = new list.

              As you create and add uc's to the form, add them to the list also:

              mylist.add(myuc)

              When you click "save", iterate the list to access controls and their contents:

              foreach ( uc myuc in mylist){

              .... myuc.txtFirstName.Text ...
              etc.
              }

              One set of controls; multiple "references" to those controls: form; list.

              1 Reply Last reply
              0
              • J Jassim Rahma

                Hi, I have created a user control with txtFirstName and txtLastName fields on it. On my frmPassengers, I am populating the user control more than one time based on the number user decided to add like this:

                private void frmPassengerDetails_Load(object sender, EventArgs e)
                {
                this.Cursor = Cursors.WaitCursor;

                BaseLayoutItem prevItem = layoutControlPassenger.Root;
                
                for (int i = 1; i < total\_adults; i++)
                {
                    ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
                
                    LayoutControlItem item = layoutControlPassenger.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
                    item.TextVisible = false;
                    item.SizeConstraintsType = SizeConstraintsType.Custom;
                    item.MinSize = new Size(830, 75);
                    item.MaxSize = new Size(830, 75);
                    prevItem = item;
                }
                

                }

                so if user choosed 5 for the total_adult value then the user control will be populated five times. Now I want to know how can I read the value of every txtFirstName and txtLastName on the form (the five of it for example)..

                Technology News @ www.JassimRahma.com

                K Offline
                K Offline
                KUMAR619
                wrote on last edited by
                #7

                I think you have to create a method form doing this. If you have limit for total adults its easy else you have go for Max value. If you say 10 is the maximum adult capacity.

                List lstFirstName=new List();
                List lstSecondName=new List();

                void RetrieveValues()
                {
                if(adultCount>0)
                {
                for(int i=1;i<=adultCount;i++)
                {
                string first=txtFirstName.Name+i.ToString();
                string second=txtSecondName.Name+i.ToString();
                lstFirstName.Add((TextBox)first.Text);
                lstFirstName.Add((TextBox)second.Text);
                }

                }
                }

                This may work. If its not working please reply with the error you are facing.

                1 Reply Last reply
                0
                • J Jassim Rahma

                  Hi, I have created a user control with txtFirstName and txtLastName fields on it. On my frmPassengers, I am populating the user control more than one time based on the number user decided to add like this:

                  private void frmPassengerDetails_Load(object sender, EventArgs e)
                  {
                  this.Cursor = Cursors.WaitCursor;

                  BaseLayoutItem prevItem = layoutControlPassenger.Root;
                  
                  for (int i = 1; i < total\_adults; i++)
                  {
                      ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
                  
                      LayoutControlItem item = layoutControlPassenger.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
                      item.TextVisible = false;
                      item.SizeConstraintsType = SizeConstraintsType.Custom;
                      item.MinSize = new Size(830, 75);
                      item.MaxSize = new Size(830, 75);
                      prevItem = item;
                  }
                  

                  }

                  so if user choosed 5 for the total_adult value then the user control will be populated five times. Now I want to know how can I read the value of every txtFirstName and txtLastName on the form (the five of it for example)..

                  Technology News @ www.JassimRahma.com

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

                  public partial class Form1 : Form
                  {
                  public Form1()
                  {
                  InitializeComponent();
                  }
                  List<myControls> lst;

                      private void button1\_Click(object sender, EventArgs e)
                      {
                          int max = int.Parse(txtInput.Text);
                          lst = new List<myControls>();
                          for (int i = 0; i < max; i++)
                          {
                              new myControls();
                              lst.Add(new myControls());
                          }
                      }
                  
                      private void button2\_Click(object sender, EventArgs e)
                      {
                          foreach (var item in lst)
                          {
                              MessageBox.Show(item.Name);
                          }
                      }
                  }
                  class myControls
                  {
                      public string Name;
                      public TextBox t;
                      public Label l;
                  
                  }
                  

                  //by default control's modifiers property is set to private. Make it public to access from other classes.

                  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