简体   繁体   中英

C# label text property not changing on event

I want to change lbl_smallBlind.Text and lbl_bigBlind.Text to values retrieved from database. Can somebody tell me why it's not working?

---The code that, I think, accessess labels' properties:

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; }
    }
}

---- The code that retrieves values from 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.");
        }
    }
}

The lbl_sb_text and lbl_bb_text values are set to what I want, just the values on the form do not change. Why?

You need not create Form1 instance again. Since it will initialize all the components for seperate object

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.");
        }
    }
}

Here are my suggestions

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

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