prattom
prattom

Reputation: 1743

Adding to comboBox of previous form

I have a comboBox in Form1 with login button and comboBox1 is disabled. When a user clicks on login button a new form Form3 opens for users to input username and password. If username and password is correct then comboBox1 is enabled and items are added to comboBox. I am implementing these things in following way. In Form1 when user click on login button

private void button1_Click(object sender, EventArgs e)
{
    var tep = new Form3();
    tep.ShowDialog();
}

In Form3 I have

 public void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "")
        {
            MessageBox.Show(" Enter UserName and Password .");
            return;
        }
        else
        {
            const string f = @"users.txt";
            const string p = @"passwd.txt";
            string[] lines = System.IO.File.ReadAllLines(f);
            string[] lines1 = System.IO.File.ReadAllLines(p);
            if (Array.IndexOf(lines, textBox1.Text) != -1 && Array.IndexOf(lines1, textBox2.Text) != -1)
            {

                MessageBox.Show("correct");
                var df = new Form1();
                df.comboBox1.Enabled = true;
                df.comboBox1.Items.Add("line1");
                df.comboBox1.Items.Add("line2");
                this.Close();

            }
            else
            {
                MessageBox.Show("Not Correct");
            }
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

Thus if login is successful then comboBox1 is enabled and items are added to it as shown in above code but problem is that comboBox1 remains disabled and no items are added to it. Where am I making mistake?

Upvotes: 0

Views: 76

Answers (2)

gzaxx
gzaxx

Reputation: 17600

That happens because you are creating new Form1 and original one is still disabled.

Best way of doing that would be to use DialogResult like this:

private void button1_Click(object sender, EventArgs e)
{
    var tep = new Form3();
    if (tep.ShowDialog() == DialogResult.OK)
    {
        comboBox1.Enabled = true;
        comboBox1.Items.Add("line1");
        comboBox1.Items.Add("line2");
    }
}

and in Form3

public void button1_Click(object sender, EventArgs e)
{
  if (textBox1.Text == "" || textBox2.Text == "")
  {
      MessageBox.Show(" Enter UserName and Password .");
      return;
  }
  else
  {
      const string f = @"users.txt";
      const string p = @"passwd.txt";
      string[] lines = System.IO.File.ReadAllLines(f);
      string[] lines1 = System.IO.File.ReadAllLines(p);
      if (Array.IndexOf(lines, textBox1.Text) != -1 && Array.IndexOf(lines1, textBox2.Text) != -1)
      {
          MessageBox.Show("correct");
          this.DialogResult = System.Windows.Forms.DialogResult.OK;
          this.Close();
      }
      else
      {
          MessageBox.Show("Not Correct");
      }
  }
}
private void button2_Click(object sender, EventArgs e)
{
  this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  this.Close();
}

Upvotes: 3

Sumeshk
Sumeshk

Reputation: 1988

No need to create new object of Form1 in Form3.

Pass Form1 object to the Form3() constructor as Form3(this) and instead of creating new Form1() object in public void button1_Click(object sender, EventArgs e) use the Form1 object that is passed through the constructor

in Form3

use

        Public Form1 form1;
        public Form3( Form1 form1Object)
        {
            InitializeComponent();
            form1=form1Object;
        }

and use that object in public void button1_Click(object sender, EventArgs e) as

  form1.comboBox1.Enabled = true;
  form1.comboBox1.Items.Add("line1");
  form1.comboBox1.Items.Add("line2");
  this.DialogResult = System.Windows.Forms.DialogResult.OK;
  this.Close();

And in Form1

modify as

private void button1_Click(object sender, EventArgs e)
{
    var tep = new Form3(this);
    tep.ShowDialog();
    this.Refresh();
}

Upvotes: 0

Related Questions