1

i have 2 forms they both have label1, form1 and form2 label1 must show the same output, when i click a button on form1 the label1 in form1 will change so as the label1 in form2

Form1

        SqlDataReader reader = cmdnext.ExecuteReader();
        while (reader.Read())
        {
            label1.Text = reader[0].ToString();
            break;

        }
        sqlcon.Close();

        Lobbypage lp = new Lobbypage(label1.Text);

    }

Form2

     public Lobbypage(string labelText)
    {
        InitializeComponent();
        label1.Text = labelText;

    }

skip button

private void button1_Click(object sender, EventArgs e)
    {
        sqlcon.Open();
        SqlCommand cmdcurrent = sqlcon.CreateCommand();
        cmdcurrent.CommandType = CommandType.Text;
        cmdcurrent.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
        cmdcurrent.CommandText = "update tblQLCashier set status = 'missing' where queID = (select min(queID) from tblQLCashier where status = 'On process' and department = @Title)";
        cmdcurrent.ExecuteNonQuery();

        SqlCommand cmdnext = sqlcon.CreateCommand();
        cmdnext.CommandType = CommandType.Text;
        cmdnext.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
        cmdnext.CommandText = "update tblQLCashier set status = 'On process' , department = @Title where queID = (select min(queID) from tblQLCashier where status = 'Pending' and department ='')";
        cmdnext.ExecuteNonQuery();
        sqlcon.Close();

        sqlcon.Open();
        cmdnext.CommandText = "select queID from tblQLCashier where queID = (select min(queID) from tblQLCashier where department in ('', @Title) and status in ('Pending', 'On process'))";
        SqlDataReader reader = cmdnext.ExecuteReader();
        while (reader.Read())
        {
            label1.Text = reader[0].ToString();
            break;

        }
        sqlcon.Close();

        Lobbypage lp = new Lobbypage(label1);

    }

both label1 in form1 and form2 should simultaneously update and have the same output

enter image description here

vantes leo
  • 11
  • 3

3 Answers3

1

In Form2

public Lobbypage(Label label)
{
    InitializeComponent();
    label1 = label;
}

In Form1

Lobbypage lp = new Lobbypage(label1);

So what's going on ? When giving label1 in Lobbypage's constructor, it creates a new variable (label) which is a copy of label1. The "trick" is that label1 is in fact a reference thus the copy will also point to the same label object.

Spotted
  • 4,021
  • 17
  • 33
  • my prob here is that when i click the skip button the label here in the form1 will change number but in the other form its still the same its still 9 i need to close the form2 and open it again for it to update – vantes leo Apr 05 '19 at 05:40
  • @vantesleo Did you try my approach ? – Spotted Apr 05 '19 at 05:42
  • yes, sir still not working only the label in form1 incremented and form2 is still 9 – vantes leo Apr 05 '19 at 05:46
  • @vantesleo Can you post the event handler called when Skip button is pressed ? – Spotted Apr 05 '19 at 05:50
  • sorry im new with programming are you asking the skip button code? i already edited my post and add the skip code. ty – vantes leo Apr 05 '19 at 06:05
1

you can create one static string property and assign it to both labels.

Sumitk
  • 55
  • 10
0

Why dont u make a public label this is the code that works for me:

namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
    static public Label label2 = new Label();

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label2.Location = new Point(20, 20);
        Controls.Add(label2);
        label2.Text = "mama";
    }
}
}

the other Form

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{

    public Form1()
    {

        Form2 hi = new Form2();
        hi.Show();
        InitializeComponent();
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        Form2.label2.Text = "Mathman";

    }
}
}

Be sure to add the functions to the handler. that means not just copy paste it in. Dubble click on the form to add the loadfunction automatically after Click before Click