Hi all,

I am creating an Windows application.

I have 2 forms.

In First form I have two buttons. The Second Buttons Enabled property is set to False.

If the First button is clicked then Form 2 should be shown which I have done with the Coding.

In the second Form I have one button If this button is clicked then it should show first form and the second button of that form should be enabled (Enabled property to be set to TRUE).

Can anyone help me out in doing this task?

Please help me out.

Thanks in advance!!

Dani AI

Generated

Three simple, safer patterns to make Form2 enable the second button on Form1 are: (A) show Form2 modally and return a DialogResult, (B) give Form2 a direct reference/Owner to Form1 and call a public method, or (C) use an event on Form2 that Form1 subscribes to (preferred for loose coupling). 's Application.OpenForms loop is workable, but scanning the global form collection is brittle if multiple instances exist or forms are hidden; a direct reference or event is clearer and less error prone.

Event-driven example (keeps Form2 decoupled from Form1):

' Form2.vb
Public Event RequestEnableSecondButton As EventHandler

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RaiseEvent RequestEnableSecondButton(Me, EventArgs.Empty)
    Me.Close()
End Sub
' Form1.vb
Dim f2 As New Form2()
AddHandler f2.RequestEnableSecondButton, AddressOf OnEnableSecondButton
f2.Show()

Private Sub OnEnableSecondButton(sender As Object, e As EventArgs)
    Button2.Enabled = True
End Sub

Modal-dialog alternative (simple when Form2 is a dialog):

' In Form2 button click:
Me.DialogResult = DialogResult.OK

' In Form1:
Using f2 As New Form2()
    If f2.ShowDialog() = DialogResult.OK Then Button2.Enabled = True
End Using

About the loop question from : the C# loop should use the less-than check (i < Application.OpenForms.Count) so the index stays in range. Using != is incorrect and can cause logic errors. 's single-thread comment is relevant: all UI updates must occur on the UI thread (use Invoke if updating from background threads). Prefer exposing a small public method on Form1 to change state rather than making controls public.

Recommended Answers

All 3 Replies

Form1:

using System;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frm1 : Form
  {
    public frm1()
    {
      InitializeComponent();
    }

    public void EnableButton2()
    {
      button2.Enabled = true;
    }

    private void frm1_Load(object sender, EventArgs e)
    {
      button1.Enabled = true;
      button2.Enabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      new frm2().Show();
      //this.Hide();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      //Dont know what you want to do
    }
  }
}

Form2:

using System;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frm2 : Form
  {
    public frm2()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      frm1 f = GetMainForm();
      if (f == null)
        return;
      f.EnableButton2();
      //this.Hide();
      f.BringToFront();
      f.Focus();
    }

    private static frm1 GetMainForm()
    {
      for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
      {
        if (Application.OpenForms[i1] is frm1)
          return ((frm1)Application.OpenForms[i1]);
      }
      return null;
    }

  }
}

S2009,

Try to get the solution for same question with single thread. Do not open too many thread?

Scott has explained very well.

In the for loop shouldn't you use the != instead of the < than operator?

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.