简体   繁体   中英

How to pass a value from one Form to a private TextBox in another Form?

I have Form1 with several private TextBoxes. I want to pass some values from my DataGridView in Form2 to those TextBoxes in Form1 (when I press Enter for example).
What I want to do is to pass the values of rows of current selected Row in DataGridView to be passed to TextBoxes in Form1 :

(I know how to get the values of selected row in datagridview my question is just the title...)

if (e.KeyCode == Keys.Enter)
{
    SqlCommand sqlcmd = new SqlCommand("SELECT ID FROM X WHERE ID=" +
                        dataGridView1.CurrentRow.Cells[0].Value + "", sqlcon);
    SqlDataReader sqldr = sqlcmd.ExecuteReader();
    while (sqldr.Read())
    {
        Form1.CodeTextBox = sqldr[codecolumn].Tostring
        Form1.NameTextBox = sqldr[Namecolumn].Tostring
        Form1.BlahTextBox = sqldr[Blahcolumn].Tostring                               
    }
}

which yells at me:

codeTextBox is private... not able to do so because of protection level...

I think I have to make a static class to do so, but I dont know how.
I would appriciate if someone would explain me that.

Form1 Class:

 public partial class Form1 : Form
 {
    public static string searchString;

    SqlConnection sqlcon = 
       new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True");

    public Form1()
    {
        InitializeComponent();
        SqlDataAdapter sqlda = new SqlDataAdapter("GetInvoice", sqlcon);
        DataTable dt = new DataTable();
        sqlda.Fill(dt);
        dataGridView1.DataSource = dt;
     }
      //Xbuton_click datagridview_blah ...
}

Declare private variable and Form1 Constructor with parameter like :

private string CodeTextBox; private string NameTextBox; private string BlahTextBox;

Public Form1(string CodeTextBox , string NameTextBox, string BlahTextBox)
{
  this.CodeTextBox= CodeTextBox; 
  this.NameTextBox= NameTextBox; 
  this.BlahTextBox = BlahTextBox; 
}

Assign Value to Textbox

txtCodeTextBox.Text = CodeTextBox;
txtNameTextBox.Text = NameTextBox;
txtBlahTextBox.Text = BlahTextBox;

Call it like

if (e.KeyCode == Keys.Enter)
{
  SqlCommand sqlcmd = new SqlCommand("SELECT ID FROM X WHERE ID=" + dataGridView1.CurrentRow.Cells[0].Value + "", sqlcon);
  SqlDataReader sqldr = sqlcmd.ExecuteReader();
  while (sqldr.Read())
  {
   string CodeTextBox = sqldr[codecolumn].Tostring;
   string NameTextBox = sqldr[Namecolumn].Tostring;
   string BlahTextBox = sqldr[Blahcolumn].Tostring;
   Form1 frm = new Form1(CodeTextBox, NameTextBox, BlahTextBox);
  }
}

try this

CodeTextBox text changed event

 public void CodeTextBox_TextChanged(object sender, EventArgs e)
        {
            CodeTextBox.Text = ((TextBox)sender).Text;
        }

SqlDataReader

 while (sqldr.Read())
                     {
        Form1 form1 = new Form1();
        TextBox t = new TextBox();

        t.Text=sqldr[codecolumn].Tostring;

        form1.CodeTextBox_TextChanged(t,null);

                        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM