Hi,
How to control button1.tex from form1, controlled in form2 or get the value from button1.text in form1 to form2?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Sep 25, 2011, 1:15 PM
Sam HobbsPosted Sep 25, 2011, 1:56 PM
Sam HobbsPosted Sep 25, 2011, 12:22 PM
For getting the control in the form, you can create a public method or property in form1 that accesses the button. Then you have a safe and reliable way to use the button from another form.
If you use the code that Vulpes suggested, then the likely problem is that you are using the incorrect names. Are you sure there is a "lblCaptainchoose" in the CustomBox form? Note that if you were to use "as" to do the cast, then you can check to ensure that it worked. Alternatively, you can use try/catch to catch problems.
Black DiamondPosted Sep 25, 2011, 12:03 PM
I encounter an error in your code...plss help me.
CustomBox CustomBox1 = (CustomBox)Application.OpenForms["CustomBox"];
Label lblCaptainchoose = (Label)CustomBox1.Controls["lblCaptainchoose"];<<--------------------error--------------Object reference not set to an instance of an object.
string text = lblCaptainchoose.Text;
try
{
conn.Close();
string insert = "INSERT INTO CaptainVotes (Names) VALUES (@Param)";
SqlCommand cmd = new SqlCommand(insert, conn);
cmd.Parameters.Add("@Param", SqlDbType.VarChar, 100);
cmd.Parameters["@Param"].Value = lblCaptainchoose.Text;
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
Black DiamondPosted Sep 25, 2011, 10:34 AM
VulpesPosted Sep 25, 2011, 10:18 AM