none
read value from item array in user control RRS feed

  • Question

  • 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)..


    Friday, July 25, 2014 7:18 PM

Answers

  • When passing data between forms you need to use an instance of the class like in my code below

           Form2 form2 = null;
            DataTable dt = new DataTable();
            public Form1()
            {
                InitializeComponent();
                form2 = new Form2();
                dt.Columns.Add("FirstName", typeof(string));
                dt.Columns.Add("LastName", typeof(string));
                dt.Columns.Add("dateOfBrith", typeof(DateTime));
                dt.Columns.Add("PassportNumber", typeof(int));
                dataGridView1.DataSource = dt;
            }
    


    jdweng

    Saturday, July 26, 2014 1:03 PM

All replies

  • I don't think putting the code into the load method is a great idea.  I gave you my solution in a previous posting.  You need a button for the user to press each time one new passenger is added.  So you can't have a button event run inside a load event.  This would required nested events.  Your approach would also would require returning a collection object (one for each passenger) back to your first form.  This can easily be done but not within the Load method which has a void as the return object. You need a 2nd method in the form which can return an object.  So that is why in your previous posting I captured the Closing Event so the form wouldn't get disposed when the user exited the form.

    jdweng

    Friday, July 25, 2014 9:24 PM
  • yeah but this is a different form. They need the number of passengers to be passed to the form and form will populate the controls accordingly just like airlines websites.

    What about moving it to the form class like below code, is it a better practice?

    public frmPassengerDetails()
    {
        InitializeComponent();
    
        BaseLayoutItem prevItem = layoutPassengers.Root;
    
        for (int i = 1; i < total_adults; i++)
        {
            ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
    
            LayoutControlItem item = layoutPassengers.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
            item.TextVisible = false;
            item.SizeConstraintsType = SizeConstraintsType.Custom;
            item.MinSize = new Size(800, 120);
            item.MaxSize = new Size(800, 120);
            prevItem = item;
        }
    }
    

    Friday, July 25, 2014 10:40 PM
  • Now you put it in the constructor which has the same issues as the load method.  Look at my code from your other posting. You can move the for loop from Form1 to Form 2 and instead of returning a Passenger you can return a List<Passenger>.  You can also add a parameter to the method for the number of passengers to return.

    One of my first software project in college was to write a simple database program which students, classes, and grades.  The teacher created input instructions in a text file to enter new students and grades, and to list results.  The first set of inputs had no errors.  If you got the program to run you got a B.  The 2nd set of inputs had error like input 5 new students and then the instructions only contained 4 students to input.  The final program was 600 punch cards and I had more card in the garbage pail then in the final program.

    Your design will need a button for the user to press after each passenger info is entered in the form so you can save one passenger info before entering the next passenger info.  How do you plan to exit the for loop if the user decides to enter only 4 passengers instead of 5 passengers.  I learned this lesson almost 40 years ago and still remember the garbage pail full of punch cards I had to throw away. 


    jdweng

    Saturday, July 26, 2014 12:01 AM
  • Here is another version of my 1st approach

    Form1

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Form2 form2 = null;
            DataTable dt = new DataTable();
            public Form1()
            {
                InitializeComponent();
                form2 = new Form2();
                dt.Columns.Add("FirstName", typeof(string));
                dt.Columns.Add("LastName", typeof(string));
                dt.Columns.Add("dateOfBrith", typeof(DateTime));
                dt.Columns.Add("PassportNumber", typeof(int));
                dataGridView1.DataSource = dt;
            }
    
    
    
            private void get4passengers_Click(object sender, EventArgs e)
            {
                List<Passenger> newPassengers = form2.getPassenger(4);
                foreach (Passenger newPassenger in newPassengers)
                {
                    DataRow newRow = dt.Rows.Add();
                    newRow["FirstName"] = newPassenger.FirstName;
                    newRow["LastName"] = newPassenger.LastName;
                    newRow["dateOfBrith"] = newPassenger.dateOfBrith;
                    newRow["PassportNumber"] = newPassenger.PassportNumber;
                }
            }
        }
        public class Passenger
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime dateOfBrith { get; set; }
            public int PassportNumber { get; set; }
        }
    
    }

    Form2

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            int count = 0;
            List<Passenger> passengers = null;
            public Form2()
            {
                InitializeComponent();
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
                this.Load += new EventHandler(Form2_Load);
            }
    
            public List<Passenger> getPassenger(int quant)
            {
    
                passengers = new List<Passenger>();
                count = quant;
                this.ShowDialog();
                return passengers;
            }
    
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                //stops for from closing
                e.Cancel = true;
                this.Hide();
            }
    
    
            private void Form2_Load(object sender, EventArgs e)
            {
                ClearForm();
    
            }
            private void ClearForm()
            {
                txtFirstName.Clear();
                txtLastName.Clear();
                cbodateOfBrith.Clear();
                txtPassportNumber.Clear();
            }
    
            private void submit_Click(object sender, EventArgs e)
            {
                Passenger passeneger = new Passenger();
                passeneger.FirstName = txtFirstName.Text;
                passeneger.LastName = txtLastName.Text;
                passeneger.dateOfBrith = DateTime.Parse(cbodateOfBrith.Text);
                passeneger.PassportNumber = int.Parse(txtPassportNumber.Text);
                passengers.Add(passeneger);
                count--;
                if (count == 0)
                {
    
                    this.Close();
                }
                ClearForm();
    
            }
    
    
        }
    }


    jdweng

    Saturday, July 26, 2014 2:12 AM
  • Hi jdweng

    I used your code within my code but still getting blank value.

    here is my user control:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    
    namespace Travel_Agency_System
    {
        public partial class ctlPassengers : DevExpress.XtraEditors.XtraUserControl
        {
            public ctlPassengers()
            {
                InitializeComponent();
            }
    
            public string PassText
            {
                get { return groupPassenger.Text; }
                set
                {
                    groupPassenger.Text = value;
                }
            }
    
            public class Passenger
            {
                public string FirstName { get; set; }
                public string LastName { get; set; }
                public string Passport { get; set; }
            }
    
        }
    }
    

    and this is for2 where user control will be display:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using MySql.Data;
    using MySql.Data.MySqlClient;
    using System.Configuration;
    using Application_Class_Library;
    using DevExpress.XtraLayout;
    
    namespace Travel_Agency_System
    {
        public partial class frmPassengerDetails : DevExpress.XtraEditors.XtraForm
        {
            public public_class public_var;
            Application_Class_Library.xml_class xmlClass = new Application_Class_Library.xml_class();
    
            MySqlConnection sql_connection = null;
            MySqlCommand sql_command = null;
            MySqlDataAdapter sql_adapter = null;
            DataTable data_table = null;
            MySqlDataReader sql_reader = null;
            DataSet data_set = null;
    
            public int flight_table_id;
            public int flight_class_id;
    
            public int booking_id;
            public string booking_code = null;
    
            public int total_adults = 5;
            public int total_children = 0;
            public int total_infants = 0;
    
            List<ctlPassengers.Passenger> passenger = null;
            
            public frmPassengerDetails()
            {
                InitializeComponent();
    
                BaseLayoutItem prevItem = layoutPassengers.Root;
    
                for (int i = 1; i < total_adults; i++)
                {
                    ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
    
                    LayoutControlItem item = layoutPassengers.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
                    item.TextVisible = false;
                    item.SizeConstraintsType = SizeConstraintsType.Custom;
                    item.MinSize = new Size(800, 120);
                    item.MaxSize = new Size(800, 120);
                    prevItem = item;
                }
            }
    
            public frmPassengerDetails(public_class public_variable)
                : this()
            {
                this.public_var = public_variable;
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void btnSave_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("do you want to add new passenger?", "Passenger", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                {
                    this.Close();
                }
            }
    
            private void frmPassengerDetails_Load(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
    
            }
    
            private void frmPassengerDetails_Shown(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
                
                this.Cursor = Cursors.Default;
            }
    
            private void btnClose_ItemClick(object sender, TileItemEventArgs e)
            {
                
                
                this.Close();
            }
    
            
            private void btnMakeBooking_ItemClick(object sender, TileItemEventArgs e)
            {
                ctlPassengers.Passenger Passenger = new ctlPassengers.Passenger();
                MessageBox.Show(Passenger.FirstName);
                
    
            }
        }
    }

    Saturday, July 26, 2014 8:59 AM
  • When passing data between forms you need to use an instance of the class like in my code below

           Form2 form2 = null;
            DataTable dt = new DataTable();
            public Form1()
            {
                InitializeComponent();
                form2 = new Form2();
                dt.Columns.Add("FirstName", typeof(string));
                dt.Columns.Add("LastName", typeof(string));
                dt.Columns.Add("dateOfBrith", typeof(DateTime));
                dt.Columns.Add("PassportNumber", typeof(int));
                dataGridView1.DataSource = dt;
            }
    


    jdweng

    Saturday, July 26, 2014 1:03 PM