0

I have two classes. the first class is form1 which has a combobox and three options. I've been trying to find a way to get the current combobox selectedindex from my other class but all my attempts have failed. Here's some code from my last attempt.

public partial class Form1 : Form
{

form2 herochoose = new form2();

public void comboBox1_SelectedIndexChanged_2(object sender, EventArgs e)
{
    heroischosen();
}

public int heroischosen()
{
    if (comboBox1.SelectedIndex == 0)
    {
        herochoose.HeroChoosen = 0;
    }
    else if (comboBox1.SelectedIndex == 1)
    {
        herochoose.HeroChoosen = 1;
    }
    else if (comboBox1.SelectedIndex == 2)
    {
        herochoose.HeroChoosen = 2;
    }

    return herochoose.HeroChoosen;
  }

}

and from form 2

public partial class form2 : Form
{
   private int _heroChoice;

   public int HeroChoosen
   {
        get { return _heroChoice; }
        set { _heroChoice = value; }
   }

   protected override void OnPaint(PaintEventArgs e)
   {
       if (_heroChoice == 0)
       {
           Console.WriteLine("herochoice1");
       }
       else if (_heroChoice == 1)
       {
           Console.WriteLine("herochoice2");                   
       }
       else if (_heroChoice == 2)
       {
           Console.WriteLine("herochoice3");   
       }
     }
  }

when form1 is active it shows that each choice is properly being chosen in the combobox, but when I start form2 it never keeps the combobox choice. I'm sorry if I'm missing something really simple.

Thanks.

Doniyor Niazov
  • 1,270
  • 1
  • 10
  • 19
  • how you starts the form2, I dont find any `herochoose.Show()` in the given code – sujith karivelil Dec 07 '16 at 02:57
  • Have you tried to debug and put a breakpoint inside the method 'heroischosen' in Form1? – Oscar Siauw Dec 07 '16 at 02:58
  • As @un-lucky said. For this to work you would need to launch form 2 from form1. If that is not possible you will need some other way of passing data between forms. A [Singleton](https://en.wikipedia.org/wiki/Singleton_pattern) pattern is quite popular in games programming for maintaining a collection of state variables used throughout the game. – meganaut Dec 07 '16 at 03:00

1 Answers1

1

Simple fix for you, Get the value in Form2's constructor, So that you can use the same in a simple way:

public partial class form2 : Form
{
    private int _heroChoice;

    public int HeroChoosen
    {
        get { return _heroChoice; }
        set { _heroChoice = value; }
    }
    // constructor
    public form2(int selectedItem)
    {
       HeroChoosen = selectedItem;
       // Now you can use HeroChoosen 
    }
    // Rest of codes here
}

If so the calling method will like this:

public void comboBox1_SelectedIndexChanged_2(object sender, EventArgs e)
{
   form2 herochoose = new form2(heroischosen()); 
   // the return value from heroischosen will be passed to the form 2
   // and are handled their in form2's constructor
   herochoose .Show();
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88