Hack Forums
[C#] Making a button help - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Making a button help (/showthread.php?tid=5192905)



Making a button help - Kale - Mar 5, 2016

So im currently working on a game project to create a basic tic tac toe game.

I've created my tic tac toe and done all the codings for it its working perfectly fine.

I have now added a seperate windows form (Form2) and i need help on how to link a button to the game? So when the user clicks "Start" on the button i want it to open the game.

Can someone help me with this basic line of coding ive been trying to figure it out ever since and i thought that people on this section of the forum can assist me ?


* - Oliver Sykes - Mar 5, 2016

Uh...Do you just want to create a new instance of Form1? If so, it should be easy to google. Otherwise, you may need to elaborate.


* - GuRuHaX - Mar 5, 2016

(Mar 5, 2016 07:44 AM)Oliver Sykes Wrote:  Uh...Do you just want to create a new instance of Form1? If so, it should be easy to google. Otherwise, you may need to elaborate.

If it is what Oliver Sykes says ,

Lets say your form where your game actually is taken place is called formGame and your other form where you want to press start and open up formGame from (if it is that that you actually want to achieve)
is called formStart then,

On he buttonClick handler (button_Click) of formStart the code should be :

PHP Code

<?php 
formGame nameHowYouWant 
= new formGame ();
// nameHowYouWant .Show();
nameHowYouWant.ShowDialog(); 

The nameHowYouWant .Show(); shows the form in a non modal form. This means that you can still click on the parent form (formStart)

The nameHowYouWant.ShowDialog(); shows the form modally, meaning you cannot go or click the parent form untill you close formGame.

🤝


* - MikaDev - Mar 7, 2016

If you mean you need to press a button in Form2 to start the game in Form1, you can pass your Form1 onto the 2nd form like this:

Form2:

Code
public partial class Form2 : Form
{
   public Form1 Form { get; }
   public Form2(Form1 f)
   {
      Form = f;
   }

   private void button1_Click(object sender, EventArgs e)
   {
      //call method that starts game in form1, example:
      Form.StartGame();
   }
}

And in your first for you can show Form2 like this:

Code
new Form2(this).Show();

Sorry if the code is aligned weird, I'm writing this on phone


* - Kale - Mar 7, 2016

Its fine guys, i have got it fixed from a help of a friend. Thanks everyone for your support.