Tutorial to set up the One-to-One relationship
1) Set up the one-to-one relationship
Create your two independent forms (Form1 and Form2 by default) by adding them to your project in Visual Studio.
Now, you need to create a dependency between those two forms. I am using forms Game (Form1) and Player (Form2).
In Player (Form2) form where you would usually declare your fields,
//fields
//create the dependency to Game Form
private Game myGameForm;
Apply the same logic to the Form1 class, or the Game class.
//fields
//create the dependency to Play Form
private Play myPlayForm;
2) Create the dependency that Form2 cannot exist without Form1.
We want the Play object to exist only if we have a Game.
//fields
//create the dependency to Play Form
private Play myPlayForm;
public Game()
{
myPlayForm = new Play(this);//create a Play form if the game exists
InitializeComponent();
}
Obviously, we cannot have a player if we do not have the game. To enforce this constraint, we make use of the constructor for the Form2, so that Form2 (or Game), will only be created if a Form1 object (Game) is not passed as an argument to the constructor.
//fields
//create the dependency to Game Form
private Game myGameForm;
public Play(Game gameform)
{
InitializeComponent();
myGameForm = gameform;//assign the object myGameForm to the argument gameform
}
So what did we exactly do? We said that if a Game exists, then the player also exists.
3) Show the second Form2 (Player) when the button on Form1 (Game) is clicked.
private void btnPlay_Click(object sender, EventArgs e)
{
myPlayForm.Show();
}
Run your code. On clicking the button from Form1 (Game), you will see the new form Player appear. You're on the right track. That's all we need to know to assign 1-to-1 dependency with constraints!
End of Tutorial
This was the first part of the question. The rest is very easy and will require the use of Random. I've included the full code of the application below.
The Game Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OneToOne
{
public partial class Game : Form
{
//fields
//create the dependency to Play Form
private Play myPlayForm;
//constructor
public Game()
{
myPlayForm = new Play(this);//create a Play form if the game exists
InitializeComponent();
}
//events
private void btnPlay_Click(object sender, EventArgs e)
{
myPlayForm.Show();
}
//methods
public void showValue(int maxval)
{
lblMax.Text = "Max Number is " + maxval.ToString()+ ".\n\nRestart game to play again";
}
}
}
The Play Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OneToOne
{
public partial class Play : Form
{
//fields
//create the dependency to Game Form
private Game myGameForm;
private int myNum = 0;
private int iCounter = 0;
//properties
public int MyNum
{
get { return myNum; }
set { myNum = value; }
}
public int ICounter
{
get { return iCounter; }
set { iCounter = value; }
}
//constructor
public Play(Game gameform)
{
InitializeComponent();
myGameForm = gameform; //assign the object myGameForm to the argument gameform
}
//events
private void btnClick_Click(object sender, EventArgs e)
{
showValue();
}
//methods
public void showValue()
{
Random myRan = new Random(); //create a myRan variable of type Random (Class)
int iRan = myRan.Next(100); //gets a random number from 0 - 100
iCounter++;
if (iCounter <= 3) //can click only 3 times
{
lblNum.Text = iRan.ToString(); //display random value on form
if (iRan >= myNum)
{
myNum = iRan;
}
}
else
{
MessageBox.Show("Cannot click more than 3 times. Turn has ended");
//prevent user from clicking more than 3 times
btnClick.Enabled = false;
//send the max value to Form1 or GameForm
myGameForm.showValue(myNum);
//hide this form
this.Hide();
//show the previous form
myGameForm.Show();
}
}
}
}
I hope that helps!
You might have already noticed that there is a mistake in the comment when creating an instance/object of the class Random.
ReplyDeleteThe comment should read:
//create a myRan object of type Random (Class)
Great Article
B.Tech Final Year Projects for CSE in JavaScript
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai