Hello,

I was wondering if you can supply me with information regarding the following. I have two forms - form A and form B. In form A I must choose two teams using two comboBoxes for each. When I click "Continue" which is a button - I want to be able to use whatever team is chosen in Form B... I can give you the following code that shows you what Ive tried but it does not work... ANY assistence regarding this matter will be much appreciated. :)

This is form A which I want to use in form B

        internal ComboBox cBox1 = new ComboBox();

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //WHEN CHOOSING A TEAM THEIR FLAG WILL APPEAR IN THE PANEL

            if (comboBox1.SelectedIndex != comboBox2.SelectedIndex)
            {
                Graphics gR = panel1.CreateGraphics();
                comboBox1.SelectedItem = cBox1;

                if (cBox1.ToString() == "New Zealand")
                {
                    //DRAW NEW ZEALAND RUGBY LOGO
                    Image imag = Image.FromFile("NZ.jpg");
                    gR.DrawImage(imag, new Point(0, 0));
                    label6.Text = "No. 1";
                    //listOfTeams.Add(NZ);
                }
                else if (comboBox1.SelectedIndex == 1)
                {
                    //DRAW SOUTH AFRICA RUGBY LOGO
                    Image imag = Image.FromFile("SA.jpg");
                    gR.DrawImage(imag, new Point(0, 0));
                    label6.Text = "No. 2";

                    Team SA = new Team();
                    //listOfTeams.Add(SA);
                }
                else if (comboBox1.SelectedIndex == 2)
                {
                    //DRAW AUSTRALIA RUGBY LOGO
                    Image imag = Image.FromFile("AUS.jpg");
                    gR.DrawImage(imag, new Point(0, 0));
                    label6.Text = "No. 4";

                    Team AUS = new Team();
                    //listOfTeams.Add(AUS);
                }
                else if (comboBox1.SelectedIndex == 3)
                {
                    //DRAW ARGENTINIAN RUGBY LOGO
                    Image imag = Image.FromFile("ARG.png");
                    gR.DrawImage(imag, new Point(0, 0));
                    label6.Text = "No. 10";

                    Team ARG = new Team();
                    //listOfTeams.Add(ARG);
                }
            }
            else
            {
                //WHEN THE SAME TEAMS ARE SELECTED - PROMT USER TO SELECT/CHOOSE A DIFFERENT TEAM
                MessageBox.Show("Select a different team", "Team Selection",MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
                comboBox1.ResetText();
            }
        }

And this is form B where I want to use form A's comboBox selections

        internal PlayerList SCList = new PlayerList();

        int total = 0;
        int tryScored = 5;
        int penOrDropkick = 3;
        int conversionTry = 2;
        public int seconds;
        public int minutes;
        public int hours;

        public scoreCard()
        {
            InitializeComponent();
        }

        //LOADING THE FORM
        private void scoreCard_Load(object sender, EventArgs e)
        {
            FormA f = new FormB();

            Graphics gR = panel1.CreateGraphics(); 

            //IF THE COMBOBOX IN FORM A WAS INDEED "New Zealand" THEN IT WILL DRAW THE FLAG IN THE PANEL

            if(f.cBox1.ToString() == "New Zealand")
            {
                Image imag = Image.FromFile("AUS.jpg");
                gR.DrawImage(imag, new Point(0, 0));
            }
        }
    }

Thank you very much.

Dani AI

Generated

Short summary of the real problems and a practical fix

The symptoms are (a) reading controls from a brand‑new form instance instead of the form that actually has the selections, (b) creating a separate ComboBox and assigning it back into the control (that doesn’t capture the selected value), and (c) drawing directly with CreateGraphics (paint will disappear on resize/refresh). correctly flags the bad instantiation; is on the right track with passing data into FormB. The simplest, robust pattern is: validate selections in FormA, take the selected values (or Team objects), then instantiate FormB and pass those values via a constructor or public properties.

Minimal example (validate in FormA, pass strings into FormB, show flags in PictureBox controls)

// inside FormA (Continue button)
if (comboBox1.SelectedIndex < 0 || comboBox2.SelectedIndex < 0) { MessageBox.Show("Pick both teams"); return; }
var t1 = comboBox1.SelectedItem.ToString();
var t2 = comboBox2.SelectedItem.ToString();
if (t1 == t2) { MessageBox.Show("Select different teams"); return; }
var formB = new FormB(t1, t2);
formB.Show(); // or ShowDialog(); Hide() FormA if desired
// in FormB
private readonly string team1, team2;
public FormB(string t1, string t2) { InitializeComponent(); team1 = t1; team2 = t2; }
private void FormB_Load(object s, EventArgs e) {
    pictureBoxLeft.Image = LoadFlagImage(team1);
    pictureBoxRight.Image = LoadFlagImage(team2);
}

Practical tips and cautions

  • Prefer binding ComboBox to a Team object (Name, Id, FlagPath) and pass the Team instance instead of raw strings.
  • Avoid CreateGraphics for permanent UI; use PictureBox.BackgroundImage or override OnPaint and call Invalidate() when the image changes.
  • Loading files: Image.FromFile locks the file. To avoid locks, read into a stream and return a new Bitmap copy.
  • If FormA is the main form, closing it will end the app—use Hide() or change app flow accordingly.
  • Add null checks and exception handling around file IO and image loading.

These changes keep forms decoupled, fix the instantiation bug noted, and implement ’s constructor idea in a safe, maintainable way.

Recommended Answers

All 2 Replies

What is the code doing that is wrong?

One thing I've noticed, f is of type FormA but you're initializing it as a new FormB.

I would attempt approaching the problem by overloading the constructor on formB and have it take as parameters the values you need from FormA.

in essence:

public class formB

string name = "";

public formB (string n)

name = n;

then you would just instantiate a formB FROM formA with that overloaded definition...

// INSIDE formA

name = combobox1.getitemtext();

formB myformB = new formB (name); // This is the magic.

Hope that helps! cheers. (sorry had to take out some curlys...the code snippet verification drived me mad when writing this :P)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.