-1

i am using visual studio 2008 c# winform. . i've make sudoku game which is working well . . i want to make best player screen for it and score depend on how much time the player take to complete game . . i am using another form to take player name when he meets the condition for best player and give the name to label on main form but its not working.here is my code:

    private void button1_Click(object sender, EventArgs e)
    {

        Form1 main = new Form1();
        main.lbBEN.Text = textBox1.Text;
        this.Close();
    }

and this on another form:

 if (emint<bmint)
 {
     best b = new best();
     b.ShowDialog();


 }

please guide me. . .THANK you

joe_coolish
  • 7,201
  • 13
  • 64
  • 111
Aenu
  • 1

2 Answers2

2

Add a public property to the second form and just below the ShowDialog(), sets the form1 label.Text to that property containing the name of the user.

public partial class Form2 : Form
{
    string _highestScoreUser = string.Empty;
    public Form2()
    {

    }

    public string HighestScoreUser
    {
        get{ return _highestScoreUser; } 
        set{ _highestScoreUser = value; }
    }
}

In Form1 code after ShowDialog is called like

{
    Form2 form = new Form2();
    form.ShowDialog();
    form1.label.Text = form.HighestScoreUser;    
}

Hope this help

joe_coolish
  • 7,201
  • 13
  • 64
  • 111
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
0

You've created a brand new Form1 object unrelated to the Form1 that is already on the screen. You need to somehow pass a reference to the real Form1 the the secondary form.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • can you please help me with some code – Aenu May 04 '11 at 05:00
  • @Aenu: When you create the best player form you are probably in the main form. Say `new BestPlayerForm(this)` where this is `Form1` and your `BestPlayerForm` constructor can save it for the click handler. – Rick Sladkey May 04 '11 at 05:04