none
c# - winform - how to avoid repetitive task

    Întrebare

  • 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.

    14 aprilie 2012 15:21

Răspunsuri

  • 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

    • Marcat ca răspuns de Bonaca 14 aprilie 2012 15:52
    14 aprilie 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


    • Marcat ca răspuns de Bonaca 14 aprilie 2012 15:51
    14 aprilie 2012 15:46

Toate mesajele

  • 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

    • Marcat ca răspuns de Bonaca 14 aprilie 2012 15:52
    14 aprilie 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


    • Marcat ca răspuns de Bonaca 14 aprilie 2012 15:51
    14 aprilie 2012 15:46
  • Thank You ! Both solutions - Works.

    Especially thanks for fastReplay.

    14 aprilie 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?

    14 aprilie 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

    14 aprilie 2012 16:49
    Moderator
  • 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.

    14 aprilie 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.

    14 aprilie 2012 17:36