Forum guidelines
AuthorQuestion
acces textbox from another form
Posted on: 16 Mar 2013
Hello :) I have two forms and i want to acces in the main form a textbox which is placed in the second form..but the textbox isn't visible for the main form. how can i do that? thanks

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: acces textbox from another form
Posted on: 20 Mar 2013   Accepted Answer
I suspect that your form's Name property is cautaClient rather than just cauta.

So, I'd try changing the first line to:

cauta = Application.OpenForms["cautaClient"] as cautaClient;
 

Re: acces textbox from another form
Posted on: 16 Mar 2013  
The procedure to do this is similar to your previous thread.

First you need to change the Modifiers property of the TextBox from private to internal so that it is visible to the main form.

Second, you need a reference to the second form. If you've opened the second form from the main form, then you'll already have a reference though it needs to be saved to a private field of the form rather than a local variable.


private Form2 f2;

// in some method

f2 = new Form2();
f2.Show();

// in some other method

if (f2 != null)
{
  TextBox tb = f2.textBox1;
  string text = tb.Text;
  // do something with text
}

If you haven't opened the second form from the main form, then you could get a reference as you did in the previous thread:

f2 = Application.OpenForms["Form2"] as Form2;
if (f2 != null)
{  
  // etc
}

Re: acces textbox from another form
Posted on: 16 Mar 2013  
another problem..i have a form which shows in some textboxes, the selected row in the dgv, and also i added some picturebox for the next and previous record. i don't know what's wrong..it shows the next record only at the first click, then stops. Here's the code:

private void pictureBox1_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = "..";
            conn.Open();

            Acasa main = (Acasa)Application.OpenForms["Acasa"];
            DataGridView dgv = main.clientGridView;
            int i = dgv.CurrentRow.Index;
            int maxRows = dgv.Rows.Count;

            if (i != maxRows - 1)
            {
                i++;

                textBox1.Text = dgv.Rows[i].Cells[0].Value.ToString();
                textBox2.Text = dgv.Rows[i].Cells[1].Value.ToString();
                textBox3.Text = dgv.Rows[i].Cells[2].Value.ToString();
                textBox4.Text = dgv.Rows[i].Cells[3].Value.ToString();
                textBox5.Text = dgv.Rows[i].Cells[4].Value.ToString();

                conn.Close();
                
            }
            else
            {
                MessageBox.Show("..");
            }
        }

i really appreciate your help..

Re: acces textbox from another form
Posted on: 16 Mar 2013  
The problem is that you're not changing the selected row in the DGV after each click of the picturebox and so it always starts at the originally selected row.

Try this instead:

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = "..";
            conn.Open();

            Acasa main = (Acasa)Application.OpenForms["Acasa"];
            DataGridView dgv = main.clientGridView;
            int i = dgv.CurrentRow.Index;
            int maxRows = dgv.Rows.Count;

            if (i != maxRows - 1)
            {
                i++;

                textBox1.Text = dgv.Rows[i].Cells[0].Value.ToString();
                textBox2.Text = dgv.Rows[i].Cells[1].Value.ToString();
                textBox3.Text = dgv.Rows[i].Cells[2].Value.ToString();
                textBox4.Text = dgv.Rows[i].Cells[3].Value.ToString();
                textBox5.Text = dgv.Rows[i].Cells[4].Value.ToString();

                dgv.Rows[i].Selected = true;
                dgv.Rows[i -1].Selected = false; // make sure only a single row is selected
            }
            else
            {
                MessageBox.Show("..");
            }

            conn.Close(); // move this to the end so you always close the connection
        }

Re: acces textbox from another form
Posted on: 17 Mar 2013  
hy again...i understand the changes you've made, but still doesn't work properly..
for example in the datagrid i have 

1
2
3
4
5

if my selected row is 3, click next and record no 4 is shown, but when i click next again doesn't show 5, it's still 4.

Re: acces textbox from another form
Posted on: 17 Mar 2013  
Hmm, it looks like selecting another row doesn't change the current row, which is an odd state of affairs. However, changing the current cell to a different row should definitely change it.

So, try replacing these lines:

  dgv.Rows[i].Selected = true;
  dgv.Rows[i -1].Selected = false; // make sure only a single row is selected

with these:

  int colIndex = dgv.CurrentCell.ColumnIndex;
  dgv.CurrentCell = dgv.Rows[i].Cells[colIndex];

Re: acces textbox from another form
Posted on: 17 Mar 2013  
it worked :D ..thanks a lot :)
Re: acces textbox from another form
Posted on: 20 Mar 2013  
hi :) you told me earlier how to acces a control from another form, and i have this code:

if (cauta.Visible == true)
            {
                cauta = Application.OpenForms["cauta"] as cautaClient;
                CheckBox cb1 = cauta.checkBox1;
                CheckBox cb2=cauta.checkBox2;
                CheckBox cb3 = cauta.checkBox3;
                CheckBox cb4 = cauta.checkBox4;
                CheckBox cb5 = cauta.checkBox5;

                cb1.Checked = false;
                cb2.Checked = false;
                cb3.Checked = false;
                cb4.Checked = false;
                cb5.Checked = false;
            }
        }
but i keep getting this error for the bolded line and i can't understand why 
"Object reference not set to an instance of an object."

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.