Home > Back-end >  want to open same form from 2 button but every buttoncan openform once
want to open same form from 2 button but every buttoncan openform once

Time:10-09

I have form to show Customer and vendors if I open form from button1 the forms Show customer and if I open from button 2 form show vendor

the problem here I wanna the buttons open form 1 time for each I made this code but the first it work for the first opened form only and the another button open many time as much as I click the button

if (Application.OpenForms[frm.Name] != null)
                    {
                        if (Application.OpenForms[frm.Name].Text == e.Item.Caption)
                        {
                            frm = Application.OpenForms[frm.Name];
                            frm.BringToFront();
                        }
                        else
                        {
                            frm.Show();
                        }
                    }
                    else
                        frm.Show();

CodePudding user response:

Ok, here is the code for the form that contains the buttons. I just called it Form1.

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
}

private void btnCustomer_Click(object sender, EventArgs e)
{
    btnCustomer.Enabled = false;

    CustomerVendorForm form = new CustomerVendorForm(btnCustomer);
    form.Show();
}

private void btnVendor_Click(object sender, EventArgs e)
{
    btnVendor.Enabled = false;

    CustomerVendorForm form = new CustomerVendorForm(btnVendor);
    form.Show();
}

And here is the CustomerVendor form:

public partial class CustomerVendorForm : Form
{

Button Form1Button;

public CustomerVendorForm(Button button)
{
    InitializeComponent();

    Form1Button = button;
}

private void CustomerVendorForm_FormClosed(object sender, FormClosedEventArgs e)
{
    Form1Button.Enabled = true;
}

So we disable the button that they clicked. Open the form, passing the button. When the user closes the form, the button is re-enabled.

Does that make sense?

CodePudding user response:

well I modified my code to get open forms text and add them to string and checked if the string contain the caption of button it wont open it for second time
here is my code ,thanks for David.Warwick he give me the idea where i need to start

if (Application.OpenForms[frm.Name] != null)
                    {
                        var texts = "";
                        foreach (Form fr in Application.OpenForms)
                        {
                            texts  = fr.Text    "/";
                        }
                            if (texts.Contains(e.Item.Caption.ToString()))
                            {
                                frm = Application.OpenForms.Cast<Form>().Where(x =>  x.Text  == e.Item.Caption.ToString()).FirstOrDefault();;
                                frm.BringToFront();
                            }
                            else
                            {
                                frm.Show();
                            }
                        
                    }
                    else
                        frm.Show();
  •  Tags:  
  • c#
  • Related