none
c# - winform - how to avoid repetitive task

    Pergunta

  • In vba I had the "call" procedure, i.e. - execute some stored procedures, to avoid repeating the same line of code menu times.

    Whati is the way to do it in c# - winform?

    I should populate my datagridview from multiple buttonsClickEvents. And i want to call the same lines of code - stored somewhere.

    sábado, 14 de abril de 2012 15:21

Respostas

  • You can create a function and achieve this

    function void SomeMethod()
    {
    // Code goes here
    }
    
    //Now you can call it as
    SomeMethod();


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    Dot Net Goodies

    • Marcado como Resposta Bonaca sábado, 14 de abril de 2012 15:52
    sábado, 14 de abril de 2012 15:39
  • Hi Bonaca,

    you can create Procedure like FillDataGridView() as below

    private void FillDataGridView() { // DataGridView Fill Code goes here; }

    private void btnModify_Click(object sender, EventArgs e)
    {
        FillDataGridView();
    }

    Regards,

    Gautam


    • Marcado como Resposta Bonaca sábado, 14 de abril de 2012 15:51
    sábado, 14 de abril de 2012 15:46

Todas as Respostas

  • You can create a function and achieve this

    function void SomeMethod()
    {
    // Code goes here
    }
    
    //Now you can call it as
    SomeMethod();


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    Dot Net Goodies

    • Marcado como Resposta Bonaca sábado, 14 de abril de 2012 15:52
    sábado, 14 de abril de 2012 15:39
  • Hi Bonaca,

    you can create Procedure like FillDataGridView() as below

    private void FillDataGridView() { // DataGridView Fill Code goes here; }

    private void btnModify_Click(object sender, EventArgs e)
    {
        FillDataGridView();
    }

    Regards,

    Gautam


    • Marcado como Resposta Bonaca sábado, 14 de abril de 2012 15:51
    sábado, 14 de abril de 2012 15:46
  • Thank You ! Both solutions - Works.

    Especially thanks for fastReplay.

    sábado, 14 de abril de 2012 15:51
  • I now tried to put all that code on separate file - CodeFile1.cs.

    And I get the error:

    "Expected class, delegate, enum, interface, or struct"

    I supose sam linking of that file with my form is needed.

    What is the way, pls?

    sábado, 14 de abril de 2012 16:05
  • I suspect that you didn't actually create a class in that new file. You can't have a method without it being in a class. Post the code you tried to use ...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    sábado, 14 de abril de 2012 16:49
    Moderador
  • At a guess you just need to copy the class declaration from the Form1 file i.e. make it a partial class in the new file. You will of course need to add a closing brace at end of file.

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    sábado, 14 de abril de 2012 17:04
  • Yes, a class declaration was missing. ThankYou.

    I just moved myself from vba to c#.

    My far goal is to make an application for alarm security agency.

    Some buildings (sometimes over 1000 objects) have alarm sensors and if they are activated, alarm signal travels (by phone or by IP) to the call centar, where operaters have some application to deal with that alarm signals. That application - that's it - of my interrest. Possibly - i would have a client for such application.

    I ask myself - maybe some template exists? Or tutorial, book...whatever i could use. Google search gives very poor help.

    sábado, 14 de abril de 2012 17:36