The pass-through value between the user control and the form: to achieve the success or failure of minesweeper in minefield, the smiley face icon makes a corresponding change.
1. Pass the parameter through the constructor function
// in the Minefield class Public PictureBox smile; Public minefield (PictureBox pb) { smile=PB; InitializeComponent ();} // join in the InitializeComponent in Form.designer This New Mine.minefield (PictureBox1);
This makes it possible to invoke the outside PictureBox directly in the minefield.
However, there is a problem: the form Designer generated code is best not to modify, each time as long as the design window changes, Form.designer code will be regenerated, you add the
This.minefield1 = new Mine.minefield (pictureBox1);
form[Design] will also report strange anomalies.
2, through the event trigger mechanism
//in the minefield Public Delegate voidMinesweepedeventsuccessfullyhandler (); Public EventMinesweepedeventsuccessfullyhandler minesweepedsuccessfully; Public Delegate voidMinesweepedfalledhandler (); Public EventMinesweepedfalledhandler minesweepedfalled; Public voidfalling () {if(Minesweepedfalled! =NULL) {minesweepedfalled (); } } Public voidsuccessful () {if(Minesweepedsuccessfully! =NULL) {minesweepedsuccessfully (); } } if(demining Success) successful (); if(Minesweeper failed) Falling ();//Triggering Events//in Form Private voidForm1_Load (Objectsender, EventArgs e) { This. Minefield1.init (Ten,Ten); This. minefield1.minesweepedfalled + =The name of the event-handling function; This. minefield1.minesweepedsuccessfully + =The name of the event-handling function; }
Through this example, I have a further understanding of the concept of events.
Learn notes about minesweeper Games (ii)