Event call at another event

Asked

Viewed 54 times

1

I have in my code the event dgvCellValueChanged that will be triggered when some value of the label is changed, it calculates the value of column 0 * column 1( Value 1 * Value 2) and assigns the total column that result.

Ex:

private void dgvCellValueChanged(objectivo Sender, DatagridViewCellEventArgs e)
{
    Dgv.Rows[e.RowIndex].Cells["Total"]. Valeu = Double.Parse( 
    Int36.Parse(Dgv.Rows[e.RowIndex].Cells["Valor 1"]. Value) * 
    Int36.Parse(Dgv.Rows[e.RowIndex].Cells["Valor 2"]. Value));
}

What I want to know is if anyone here can tell me if there is a way to call this method within the method

private void dgvMouseMove(objectivo Sender, MouseEventArgs e) { }
  • I noticed that you are trying to accept both answers. It is only possible to accept 1 answer per question.

2 answers

3


Since the signature methods are different, a good idea is to create an intermediate method that is called by the two events.

private void Metodo(int rowIndex)
{
    Dgv.Rows[rowIndex].Cells["Total"]. Valeu = Double.Parse( 
    Int36.Parse(Dgv.Rows[rowIndex].Cells["Valor 1"]. Value) * 
    Int36.Parse(Dgv.Rows[rowIndex].Cells["Valor 2"]. Value));
}

Then just change the methods of the events to make the call.

private void dgvCellValueChanged(objectivo Sender, DatagridViewCellEventArgs e) 
    => Metodo(e.RowIndex);

provate void dgvMouseMove(objectivo Sender, MouseEventArgs e) 
{    
    int rowIndex = Dgv.HitTest(e.X, e.Y).RowIndex; // Obtém o índice da linha clicada
    if(rowIndex < 0) return;
    Metodo(rowIndex);
}
  • I tried to call this way to, but gives error of parameters. Informs q these parameters are incorrect.

  • So you didn’t put the correct signature on your question. You can check this out?

  • It is vdd, the signature is wrong, the correct is: private void dgvMouseve(Sender goal, Mouseeventargs and) { }

  • @Sannunes I updated the answer.

  • I’ll try it this way!! Thanks for the help!! ^^

  • @You’re welcome. Note that when you mark the On the left side of the answer, you choose the answer as a solution to your problem, and in a way, that’s a "finished" warning. Accept only when a solution works and remember that you can only choose one answer per question.

Show 1 more comment

1

Personally I think the most correct/"beautiful" way is to create a method that should be evoked in both events:

private void dgvCellValueChanged(objectivo Sender, DatagridViewCellEventArgs e)
{
    AtualizaValores(e.RowIndex);
}

private void dgvMouseMove(objectivo Sender, MouseEventArgs e) 
{
    int intIndex = Dgv.HitTest(e.X, e.Y).RowIndex;

    if(intIndex >= 0)
        AtualizaValores(intIndex);
}

void AtualizaValores(int intLinha)
{
    var row = Dgv.Rows[intLinha];

    row.Cells["Total"].Value = double.Parse(int.Parse(row.Cells["Valor 1"].Value) * int.Parse(row.Cells["Valor 2"].Value));
}
  • 1

    Why this is the way more correct?

  • I forgot to put one "Personally I think...", because that’s it, personally I think that would be the most correct/"beautiful way".

  • 1

    The method AtualizaValores is wrong. The variable e is not defined there. Even if you receive by parameters, the EventArgs are not the same.

  • Still wrong.

  • Calm @LINQ, was in edition :). Thanks for the "touch".

  • I had no way of knowing, especially since an edition had already been made =)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.