Question The value is not in the textbox of a form when a form is opened

Lin100

Well-known member
Joined
Dec 12, 2022
Messages
69
Programming Experience
10+
The code below ran without any error. When I double-click on the cell in the dataGridView2,
the form Reservation1 opened, but the three value below are not in the textbox. The three
text boxes are blank. The other two values are in the form Reservation1.

R1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString(); --> This value is in the form Reservation1
R1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString(); --> This value is in the form Reservation1

R1.Date_Reserved.Text = Date_Reserved.ToString(); --> This value is not in the textbox of form Reservation1
R1.Lease_Months.Text = "6"; --> This value is not in the textbox of form Reservation1
R1.Reservation_Cancelled.Text = "No"; --> This value is not in the textbox of form Reservation1

The value is not in the textbox of a form when a form is opened:
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
   {
     string Apartment_Status;
     DateTime Date_Reserved = DateTime.Now.Date;
     MessageBox.Show("Date_Reserved =  " + Date_Reserved);

     object value = this.dataGridView2.CurrentRow.Cells[7].Value.ToString();
     if (value is DBNull) { return; }
       Apartment_Status = value.ToString();
       MessageBox.Show("Apartment_Status =  " + Apartment_Status);

     switch (Apartment_Status)
      {
          case "Reserved":
            Reservation1 R1 = new Reservation1();
            R1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();  --> This value is in the form Reservation1
            R1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();    --> This value is in the form Reservation1
            R1.Date_Reserved.Text = Date_Reserved.ToString();     --> This value is not in the textbox of form Reservation1
            R1.Lease_Months.Text = "6";                           --> This value is not in the textbox of form Reservation1
            R1.Reservation_Cancelled.Text = "No";                 --> This value is not in the textbox of form Reservation1
            R1.Show();
          break;

          case "Occupied":
            Tenant T1 = new Tenant();
            T1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
            T1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
            T1.Show();
          break;

          case "Vacant":
            Reservation1 R2 = new Reservation1();
            R2.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
            R2.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
            R2.Show();
          break;

          default:
          break;
       }
   }
 
I did just that as shown below. When I double-click on the DataGridView2 in the form Unit_2, the Reservation1 form comes up but the messageBox did not appear, which means nothing goes into the textbox in form Reservation1.

Lease_Months_TextChanged in form Reservation1:
private void Lease_Months_TextChanged(object sender, EventArgs e)
   {
      MessageBox.Show(Lease_Months.Text);
   }
 
Last edited by a moderator:
Please don't enclose line numbers in quotes when highlighting lines of code. It fails to highlight and it also breaks the rest of the page, making it impossible for us to reply.
 
Here is the code for form Reservation1.

Code for form Reservation1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
    public partial class Reservation1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AMS_2007.accdb");
        public Reservation1()
        {
            InitializeComponent();
        }

        private void Reservation1_Load(object sender, EventArgs e)
        {
            //If I put the two code here then it will showed up in the textbox.
            //In the original question that I have posted these two code were
            //in the form Unit_2 and it did not showed up in the textbox when
            //form Reservation1 is opened.
            
            Lease_Months.Text = "6";
            Reservation_Cancelled.Text = "No";
        }

        private void Lease_Months_TextChanged(object sender, EventArgs e)
            {
                MessageBox.Show(Lease_Months.Text);
            }
    }
}
 
Firstly, if you have code to populate those controls in the Load event handler, why would you think that code to populate them that gets executed before that would have any effect? You also haven't address post #5. You should probably just show us the entire designer code file.
 
My reading of post #7 was that the OP was trying to show that if he had lines 29-30 in Reservation1.cs, then his textboxes would be filled. If removed lines 29-30 in Reservation1.cs, and only had lines 18-20 in his double click handler for post #1, then his textboxes would be left empty.

My first guess would have been something in the designer code was blanking out the textboxes, but lines 18-20 execute after the InitializeComponent() as called by the constructor on line 15. So the my next guess is that there is something in the Shown event handler which clears out the textboxes, but that would also mean that it would also blow away anything done by his lines 29-30.

More curious is why the text change event isn't firing.

Interesting mystery.
 
Firstly, if you have code to populate those controls in the Load event handler, why would you think that code to populate them that gets executed before that would have any effect? You also haven't address post #5. You should probably just show us the entire designer code file.
Here is the entire code. I made some small changes and now it work,
though I have not figured out how it work. The only thing I did was
I created another form named Reservation2.

1) When the Apartment_Status = "Vacant" then it will fill in the the
textbox in the form Reservation1. The code to fill in the textbox
is in the Unit_2 form.
2) I created a form named Reservation2 and this form does not have
any code that fill in the text box.


Code for Unit_2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
//using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
   public partial class Unit_2 : Form
   {
      public Unit_2()
      {
         InitializeComponent();
      }

      private void comboBox_property_name()
        {
          OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AMS_2007.accdb");
          OleDbDataAdapter da = new OleDbDataAdapter("SELECT Property.Property_Name, Property.Property_ID, Property.Property_Phone, Property.Property_Manager FROM Property", con);
          con.Open();
          DataSet dt = new DataSet();
          da.Fill(dt, "Property_Name");

          comboBox_Name_2.DataSource = dt.Tables["Property_Name"];
          comboBox_Name_2.DisplayMember = "Property_Name";
          comboBox_Name_2.ValueMember = "Property_ID";
          con.Close();
        }

        private void display_datagrid()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Apartment_Management.Properties.Settings.AMS_2007ConnectionString"].ToString();
            con.Open();
            //MessageBox.Show("Connection Sucess");
            OleDbCommand cmd = new OleDbCommand();

            if (comboBox_Name_2.Text == "Select All Properties")
                cmd.CommandText = "SELECT DISTINCTROW Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, " +
                                  "Unit.Rental_Cost, Unit.Security_Deposit, Unit.Bathroom, Unit.Square_Feet, " +
                                  "Unit.Status, Tenant.Date_Lease_Expired, Tenant.Late_On_Payment, " +
                                  "Tenant.Days_Delinquent FROM Unit LEFT JOIN " +
                                  "Tenant ON(Unit.Unit_Number = Tenant.Unit_Number) " +
                                  "AND(Unit.Property_Name = Tenant.Property_Name) " +
                                  "ORDER BY Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, Unit.Rental_Cost";
            else
            {
                MessageBox.Show("else condition");
                cmd.CommandText = "SELECT DISTINCTROW Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, " +
                                  "Unit.Rental_Cost, Unit.Security_Deposit, Unit.Bathroom, Unit.Square_Feet, " +
                                  "Unit.Status, Tenant.Date_Lease_Expired, Tenant.Late_On_Payment, " +
                                  "Tenant.Days_Delinquent FROM Unit LEFT JOIN " +
                                  "Tenant ON(Unit.Unit_Number = Tenant.Unit_Number) " +
                                  "AND(Unit.Property_Name = Tenant.Property_Name) " +
                                  "WHERE Unit.Property_Name = '" + comboBox_Name_2.Text + "'" +
                                  "ORDER BY Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, Unit.Rental_Cost";
            }

            cmd.Connection = con;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
        }

        private void Unit_2_Load(Object Sender, EventArgs e)
        {
           comboBox_property_name();
           display_datagrid();
        }

        private void comboBox_Name_2_SelectedIndexChanged(Object Sender, EventArgs e)
        {
           display_datagrid();
        }

        private void DataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
           if (e.ColumnIndex == 7 & e.Value != null)
             {
               //int sum1 = convert.ToInt32(e.Value);
               String Apartment_Status = Convert.ToString(e.Value);
               if (Apartment_Status == "Reserved")
                  e.CellStyle.BackColor = Color.Yellow;
               else if (Apartment_Status == "Occupied")
                  e.CellStyle.BackColor = Color.Red;
               else if (Apartment_Status == "Occupied/Reserved")
                  e.CellStyle.BackColor = Color.LightCyan;
               else if (Apartment_Status == "Vacant")
                  e.CellStyle.BackColor = Color.LightGreen;
              }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           string Apartment_Status;
           DateTime Date_Reserved = DateTime.Today;
           MessageBox.Show("Date_Reserved =  " + Date_Reserved);

           object value = this.dataGridView2.CurrentRow.Cells[7].Value.ToString();
           if (value is DBNull) { return; }
           Apartment_Status = value.ToString();
           MessageBox.Show("Apartment_Status =  " + Apartment_Status);

           switch (Apartment_Status)
             {
                case "Reserved":
                  Reservation2 R2 = new Reservation2();
                  R2.Show();
                break;

                case "Occupied":
                  Tenant T1 = new Tenant();
                  T1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
                  T1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  T1.Show();
                break;

                case "Occupied/Reserved":
                  Tenant T2 = new Tenant();
                  T2.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
                  T2.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  T2.Show();
                break;

                // The code below does fill in the textbox when form 
                case "Vacant":
                  Reservation1 R1 = new Reservation1();
                  R1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
                  R1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  R1.Date_Reserved.Text = Date_Reserved.ToString();
                  R1.Lease_Months.Text = "6";
                  R1.Reservation_Cancelled.Text = "No";
                  R1.Show();
                break;

                default:
                break;
             }
        }
    }
}

Code for form Reservation1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
    public partial class Reservation1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AMS_2007.accdb");
        public Reservation1()
        {
            InitializeComponent();
        }

        private void Lease_Months_TextChanged(object sender, EventArgs e)
        {
           MessageBox.Show(Lease_Months.Text);
        }
    }
}

Code for form Reservation2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
    public partial class Reservation2 : Form
    {
        public Reservation2()
        {
            InitializeComponent();
        }
    }
}
 
Here is the entire code. I made some small changes and now it work,
though I have not figured out how it work. The only thing I did was
I created another form named Reservation2.

1) When the Apartment_Status = "Vacant" then it will fill in the the
textbox in the form Reservation1. The code to fill in the textbox
is in the Unit_2 form.
2) I created a form named Reservation2 and this form does not have
any code that fill in the text box.


Code for Unit_2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
//using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
   public partial class Unit_2 : Form
   {
      public Unit_2()
      {
         InitializeComponent();
      }

      private void comboBox_property_name()
        {
          OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AMS_2007.accdb");
          OleDbDataAdapter da = new OleDbDataAdapter("SELECT Property.Property_Name, Property.Property_ID, Property.Property_Phone, Property.Property_Manager FROM Property", con);
          con.Open();
          DataSet dt = new DataSet();
          da.Fill(dt, "Property_Name");

          comboBox_Name_2.DataSource = dt.Tables["Property_Name"];
          comboBox_Name_2.DisplayMember = "Property_Name";
          comboBox_Name_2.ValueMember = "Property_ID";
          con.Close();
        }

        private void display_datagrid()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Apartment_Management.Properties.Settings.AMS_2007ConnectionString"].ToString();
            con.Open();
            //MessageBox.Show("Connection Sucess");
            OleDbCommand cmd = new OleDbCommand();

            if (comboBox_Name_2.Text == "Select All Properties")
                cmd.CommandText = "SELECT DISTINCTROW Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, " +
                                  "Unit.Rental_Cost, Unit.Security_Deposit, Unit.Bathroom, Unit.Square_Feet, " +
                                  "Unit.Status, Tenant.Date_Lease_Expired, Tenant.Late_On_Payment, " +
                                  "Tenant.Days_Delinquent FROM Unit LEFT JOIN " +
                                  "Tenant ON(Unit.Unit_Number = Tenant.Unit_Number) " +
                                  "AND(Unit.Property_Name = Tenant.Property_Name) " +
                                  "ORDER BY Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, Unit.Rental_Cost";
            else
            {
                MessageBox.Show("else condition");
                cmd.CommandText = "SELECT DISTINCTROW Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, " +
                                  "Unit.Rental_Cost, Unit.Security_Deposit, Unit.Bathroom, Unit.Square_Feet, " +
                                  "Unit.Status, Tenant.Date_Lease_Expired, Tenant.Late_On_Payment, " +
                                  "Tenant.Days_Delinquent FROM Unit LEFT JOIN " +
                                  "Tenant ON(Unit.Unit_Number = Tenant.Unit_Number) " +
                                  "AND(Unit.Property_Name = Tenant.Property_Name) " +
                                  "WHERE Unit.Property_Name = '" + comboBox_Name_2.Text + "'" +
                                  "ORDER BY Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, Unit.Rental_Cost";
            }

            cmd.Connection = con;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
        }

        private void Unit_2_Load(Object Sender, EventArgs e)
        {
           comboBox_property_name();
           display_datagrid();
        }

        private void comboBox_Name_2_SelectedIndexChanged(Object Sender, EventArgs e)
        {
           display_datagrid();
        }

        private void DataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
           if (e.ColumnIndex == 7 & e.Value != null)
             {
               //int sum1 = convert.ToInt32(e.Value);
               String Apartment_Status = Convert.ToString(e.Value);
               if (Apartment_Status == "Reserved")
                  e.CellStyle.BackColor = Color.Yellow;
               else if (Apartment_Status == "Occupied")
                  e.CellStyle.BackColor = Color.Red;
               else if (Apartment_Status == "Occupied/Reserved")
                  e.CellStyle.BackColor = Color.LightCyan;
               else if (Apartment_Status == "Vacant")
                  e.CellStyle.BackColor = Color.LightGreen;
              }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           string Apartment_Status;
           DateTime Date_Reserved = DateTime.Today;
           MessageBox.Show("Date_Reserved =  " + Date_Reserved);

           object value = this.dataGridView2.CurrentRow.Cells[7].Value.ToString();
           if (value is DBNull) { return; }
           Apartment_Status = value.ToString();
           MessageBox.Show("Apartment_Status =  " + Apartment_Status);

           switch (Apartment_Status)
             {
                case "Reserved":
                  Reservation2 R2 = new Reservation2();
                  R2.Show();
                break;

                case "Occupied":
                  Tenant T1 = new Tenant();
                  T1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
                  T1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  T1.Show();
                break;

                case "Occupied/Reserved":
                  Tenant T2 = new Tenant();
                  T2.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
                  T2.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  T2.Show();
                break;

                // The code below does fill in the textbox when form
                case "Vacant":
                  Reservation1 R1 = new Reservation1();
                  R1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();
                  R1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  R1.Date_Reserved.Text = Date_Reserved.ToString();
                  R1.Lease_Months.Text = "6";
                  R1.Reservation_Cancelled.Text = "No";
                  R1.Show();
                break;

                default:
                break;
             }
        }
    }
}

Code for form Reservation1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
    public partial class Reservation1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AMS_2007.accdb");
        public Reservation1()
        {
            InitializeComponent();
        }

        private void Lease_Months_TextChanged(object sender, EventArgs e)
        {
           MessageBox.Show(Lease_Months.Text);
        }
    }
}

Code for form Reservation2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
    public partial class Reservation2 : Form
    {
        public Reservation2()
        {
            InitializeComponent();
        }
    }
}

I think I know what I did. Let see if you folks can verify it to be true.
It used to be that Unit_2 and Reservation2 form both have dataGridView with the same name dataGridView2. I would think that having the same name for dataGridView for both form would be okay, but apparently
it causes the textbox in Reservation1 to be blank. So then I renamed
the dataGridView in the form Reservation2 from dataGridView2 to
dataGridViewReservation2.

Unit form
dataGridView Name: dataGridView1

Unit_2 form
dataGridView Name: dataGridView2

Reservation1 form
Textbox and no dataGridView

Reservation2 form
dataGridView Name: dataGridViewReservation2

Tenant form
Textbox and no dataGridView
 
I didn't ask for the entire code. I asked a specific question that you haven't answered and I asked for a specific code file that you haven't provided.
You said "You should probably just show us the entire designer code file."
This is the reason why I included all of the relevant code.
 
Yeah, but I didn't ask for those other files and you didn't provide the designer code file for that form. Each form has two code files: user and designer. The user file is where you write your code and the designer file is where the designer-generated code is placed. If you Ctrl+clicked the variable in your original code, as I said, then you'd have gone straight to that file if everything was set up correctly. If Ctrl+click doesn't work (some systems may be configured differently) then you should be able to right-click and go to the declaration.
 
If you look in solution explorer:

1671703113074.png


Mostly solution explorer always looks like the red outline; BlahForm.cs - you double click it and see the designer, you right click and View Code and see the code file that you write your code in

The windows forms designer writes another code file that is merged into your one during compilation. If you look in the expanded blue box you can see this BlahForm.Designer.cs - that's where the forms designer puts all the code it writes as you drag things round the UI and use the properties grid to set things up. Seeing that will be handy to use because it will tell us exactly what the full code of the form is, so we can try and repicate your problem or see it just with a quick review on the forum
 

Latest posts

Back
Top Bottom