其他类不可见按钮控件

时间:2013-07-01 06:07:38

标签: c#

我有这个代码从SQL查询中获取值并将值放在文本框中。我想把它放到另一个类,并从主类访问它。但我的问题是,该类不会识别按钮(txtbox_ticketnum),因为它来自主类。帮助!

 using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {

            using (SqlCommand com_retrieve = new SqlCommand("usp_SelectTop1Ticket", con))
            {
                com_retrieve.CommandType = CommandType.StoredProcedure;
                con.Open();
                try
                {
                    txtbox_ticketnum.Text = com_retrieve.ExecuteScalar().ToString();
                    MessageBox.Show("Ticket Has been saved. Your Ticket Number: " + com_retrieve.ExecuteScalar().ToString(), "Ticket Filed");
                }
                catch (SqlException)
                {
                    MessageBox.Show("The database has encountered an error");
                }
                catch (Exception)
                {
                    MessageBox.Show("The server has encountered an error");
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您的按钮(txtbox_ticketnum)似乎是私有 - 这是默认的Visual Studio行为。您可以更改它(将txtbox_ticketnum的“Modifiers”属性设置为“public”),或者 添加自己的属性(更好的解决方案)

public partial class MyForm {
  ...
  public String TicketNumText {
    get {
      return txtbox_ticketnum.Text; 
    }
    set {
      txtbox_ticketnum.Text = value; 
    }
  }

  ...

  MyForm form = new MyForm();

  ...

  con.Open();

  try
    {
      form.TicketNumText = com_retrieve.ExecuteScalar().ToString();
       ...