C#标签文本属性不会更改事件

时间:2014-03-09 18:54:05

标签: c# winforms

我想将lbl_smallBlind.Textlbl_bigBlind.Text更改为从数据库中检索的值。有人可以告诉我为什么它不起作用吗?

---我认为代码可以访问标签的属性:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public string lbl_sb_text
    {
        get { return lbl_smallBlind.Text; }
        set { lbl_smallBlind.Text = value; }
    }

    public string lbl_bb_text
    {
        get { return lbl_bigBlind.Text; }
        set { lbl_bigBlind.Text = value; }
    }
}

----从DB中检索值的代码:

public void rozne()
{

    string constr = Properties.Settings.Default.DodgeBulletsDBConnectionString;
    Form1 menuglowne = new Form1();

    using (SqlCeConnection con = new SqlCeConnection(constr))
    {
        SqlCeCommand com = new SqlCeCommand(
            "SELECT Big_blind, Small_blind FROM Site WHERE Level=1", con);
        con.Open();
        SqlCeDataReader reader = com.ExecuteReader();

        bool hasRow = reader.Read();
        if (hasRow)
        {
            int bb = reader.GetInt32(reader.GetOrdinal("Big_blind"));
            int sb = reader.GetInt32(reader.GetOrdinal("Small_blind"));
            menuglowne.lbl_sb_text = Convert.ToString(sb);
            menuglowne.lbl_bb_text = Convert.ToString(bb);
        }
        else
        {
            MessageBox.Show("Coś się zjebało z importem DS.Site.");
        }
    }
}

lbl_sb_textlbl_bb_text值设置为我想要的值,只是表单上的值不会更改。为什么呢?

1 个答案:

答案 0 :(得分:0)

您无需再次创建Form1实例。因为它将初始化单独对象的所有组件

public void rozne()
{

    string constr = Properties.Settings.Default.DodgeBulletsDBConnectionString;
    //Form1 menuglowne = new Form1();

    using (SqlCeConnection con = new SqlCeConnection(constr))
    {
        SqlCeCommand com = new SqlCeCommand("SELECT Big_blind, Small_blind FROM Site WHERE Level=1", con);
        con.Open();
        SqlCeDataReader reader = com.ExecuteReader();

        bool hasRow = reader.Read();
        if (hasRow)
        {
            int bb = reader.GetInt32(reader.GetOrdinal("Big_blind"));
            int sb = reader.GetInt32(reader.GetOrdinal("Small_blind"));
            this.lbl_sb_text = Convert.ToString(sb);
            this.lbl_bb_text = Convert.ToString(bb);
        }
        else
        {
            MessageBox.Show("Coś się zjebało z importem DS.Site.");
        }
    }
}

以下是我的建议

//Form1 menuglowne = new Form1();
this.lbl_sb_text = Convert.ToString(sb);
this.lbl_bb_text = Convert.ToString(bb);