0

I seem to be able to pass the parameter to the form from the program, but then how do i access the variable in the buttons routines. I have put the two sub routines Main() and Start up which would run the form1. and then I have put the namespace of the form.

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        StartUp();            
    }
    static void StartUp()
    {
        bool mode = false;
        Application.Run(new Form1());
        //bool playermode = GetPlayerMode(); 
    }

namespace PencilProject
 {
public partial class Form1 : Form 
{
    private static bool modebool;
    public Form1()
    {
        InitializeComponent();
    }
    public void button1_Click(object sender, EventArgs e)
    {
        modebool = true;            
        Close();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        modebool = false;
        Close();
    }
 }
}
  • 1
    Where do you want to pass what? – PepitoSh Jul 04 '18 at 08:13
  • I would like to pass the parameter of the bool, mode, to the buttons to determine what mode to be used –  Jul 04 '18 at 08:15
  • You'll need a constructor `Form1(bool mode) {modebool=mode;}` – Hintham Jul 04 '18 at 08:15
  • Have a look at https://stackoverflow.com/questions/1179532/how-do-i-pass-command-line-arguments-to-a-winforms-application – cordifed Jul 04 '18 at 08:15
  • `Application.Run(new Form1(/*PASS DATA HERE*/));` and then inside the constructor have some parameters such as `public Form1(string name, int age)` – Danny Goodall Jul 04 '18 at 08:16
  • when i tried that though, modebool was not passed back to the program –  Jul 04 '18 at 08:16
  • Please don't use `private static bool modebool;` inside a regular class. It's a bad idea. Change it to `public bool modebool;` and see if that helps solve your problem. – Enigmativity Jul 04 '18 at 09:20

2 Answers2

0

You can use DialogResult to have a "return value", some sort:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        StartUp();            
    }
    static void StartUp()
    {
        Form1 frm = new Form1()
        Application.Run(frm);

        bool mode = frm.DialogResult == DialogResult.Yes;

        //bool playermode = GetPlayerMode(); 
    }

namespace PencilProject
 {
public partial class Form1 : Form 
{
    public Form1()
    {
        InitializeComponent();
    }
    public void button1_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.Yes;      
        Close();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.No;
        Close();
    }
 }
}

You even can assign the DialogResult value to the buttons in the designer.

PepitoSh
  • 1,774
  • 14
  • 13
  • When i try this though, in the main program, mode is still equal to false –  Jul 04 '18 at 08:21
  • I see. You wish to have the selected value back in StartUp. See update answer... shortly. – PepitoSh Jul 04 '18 at 08:35
  • @JacobBrewer welcome to StackOverflow. If an answer has helped you, you might consider to mark it as accepted. If you don't know how it works [this post](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) can help. – Mong Zhu Jul 04 '18 at 10:47
0

Try this:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var f = new Form1();
        f.Mode = false;
        Application.Run(f);
        bool playerMode = f.Mode;
    }
}

And:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public bool Mode = false;

    private void button1_Click(object sender, EventArgs e)
    {
        this.Mode = true;
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Mode = false;
        this.Close();
    }
}

This works fine for me getting bool playerMode set based on the button I click.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172