I'm OO programming in C# and bumped into a problem. I got this piece of code here (this is the button btn_delete_click)

            if (dialogResult == DialogResult.Yes)
            {
                if (objBestand.bestandsnaamString == file2)
                {
                    objBestand.VerwijderBestand();

                     // btn_ophalen_click?
                }
            }

Now I want to call the "function" of another button at "btn_ophalen_click?"

This is the button I want to call there

    private void btn_Ophalen_Click(object sender, EventArgs e)
    {
            string PathString;  //  maak string aan 
            PathString = textBox1.Text + @":\" + textBox2.Text; //vul_list string mwet waarde
            objBestanden = new clsBestanden();
            objBestanden.Zoekbestanden(PathString);  // Roep method Zoekbestanden aan 
            vul_list();  // vul lijst of form
    }

When I click the btn_delete_click, it deletes a file, and then has to "trigger" btn_ophalen_click I hope I gave enough info.

有帮助吗?

解决方案

It is bad practice to call event as method. The best way to do want you want is to move event code to separate method:

 //part of btn_delete_click event code
 if (dialogResult == DialogResult.Yes)
  {
      if (objBestand.bestandsnaamString == file2)
      {
          objBestand.VerwijderBestand();

          Ophalen();
      }
  }

private void btn_Ophalen_Click(object sender, EventArgs e)
{
    Ophalen();
}

private void Ophalen()
{
    string PathString;  //  maak string aan
    PathString = textBox1.Text + @":\" + textBox2.Text; //vul_list string mwet waarde
    objBestanden = new clsBestanden();
    objBestanden.Zoekbestanden(PathString);  // Roep method Zoekbestanden aan 
    vul_list();  // vul lijst of form
}

其他提示

you should always keep the action inside the event in a separate method.

private void btn_delete_Click(object sender, EventArgs e)
{
    Delete();
}

private void btn_Ophalen_Click(object sender, EventArgs e)
{
    Open();
}

private void Delete()
{
    if (dialogResult == DialogResult.Yes)
    {
        if (objBestand.bestandsnaamString == file2)
        {
            objBestand.VerwijderBestand();
            Open();
        }
    }
}

private void Open()
{
    string PathString;  //  maak string aan 
        PathString = textBox1.Text + @":\" + textBox2.Text; //vul_list string mwet waarde
        objBestanden = new clsBestanden();
        objBestanden.Zoekbestanden(PathString);  // Roep method Zoekbestanden aan 
        vul_list();  // vul lijst of form
}

I think button.PerformClick(); perform click is what you need. But better extract method Ophalen() with needed arguments and call it in both button click handlers

you can just do something like this.

public void toDo()
{
 string PathString;  //  maak string aan 
        PathString = textBox1.Text + @":\" + textBox2.Text; //vul_list string mwet waarde
        objBestanden = new clsBestanden();
        objBestanden.Zoekbestanden(PathString);  // Roep method Zoekbestanden aan 
        vul_list();  // vul lijst of form
}

if (dialogResult == DialogResult.Yes)
        {
            if (objBestand.bestandsnaamString == file2)
            {
                objBestand.VerwijderBestand();
                toDo()
            }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top