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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 25, 2011, 9:15 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 25, 2011, 6:18 AM EDT
Black Diamond191 reputationPosted Sep 25, 2011, 6:34 AM EDT
Black Diamond191 reputationPosted Sep 25, 2011, 8:03 AM EDT
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();
}
Sam HobbsRank #107 · 20.5K reputationPosted Sep 25, 2011, 8:22 AM EDT
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.
Sam HobbsRank #107 · 20.5K reputationPosted Sep 25, 2011, 9:56 AM EDT