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();
    }
 }
}
Jacob
  • 41
  • 6

2 Answers2

1

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,765
  • 14
  • 13
  • When i try this though, in the main program, mode is still equal to false – Jacob 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
  • 105,241
  • 11
  • 83
  • 163