read value from item array in user control
-
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
-
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
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.
-
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.
sorry I didn't get you...
Technology News @ www.JassimRahma.com
-
sorry I didn't get you...
Technology News @ www.JassimRahma.com
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?
-
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?
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
-
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
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.
-
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
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.
-
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
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.