Results 1 to 11 of 11

Thread: Conversion of subs and functions to a common class or routine

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    402

    Conversion of subs and functions to a common class or routine

    I have finished developing a copy and Paste routine for a data grid view, this now works for data grids of any size, and after a lot of testing the Programme has been proven to work.

    However this is on a simple form, with all the routines being within the class of the from.
    When a copy or paste routine is selected on the form, it calls the relevant sub/procedure which currently exists within the form class

    As I intend to use this routine with lots of forms contains data grid views, I was wondering what is the best way to go about this ?

    I clearly want the code to only exist once, and not take the easy option of copying the same code into every form.

    Note the code requires access to the actual data grid view (both read and update and) and the clipboard.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Conversion of subs and functions to a common class or routine

    You just need to create a class that inherits DataGridView. This is an effective method of extending the standard DataGridView control.

    https://docs.microsoft.com/en-us/dot...orkdesktop-4.8

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Conversion of subs and functions to a common class or routine

    Obviously there’s slightly more than simply inheriting a control, but using the inheritance method you can encapsulate your extension code into your new custom control…

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    402

    Re: Conversion of subs and functions to a common class or routine

    Ok, I sort of get what you are suggesting, however this is all very new, but happy to learn.
    I can sort of understand that I need to create a new class ‘DatagridviewCutPaste’ which inherits Datagridview.

    However on my form I have dataGridView copied fromthe toolbox box, which i assume is linked to the datagridview class.

    How is this now linked to my new class ?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Conversion of subs and functions to a common class or routine

    Quote Originally Posted by Signalman View Post
    However on my form I have dataGridView copied fromthe toolbox box, which i assume is linked to the datagridview class.

    How is this now linked to my new class ?
    It's not "linked". When you drag a control or component from the Toolbox onto a form, code is generated to create an instance of that type. It's basically the same code you would write to create an instance of a type and set its properties. If you add a class to your project that inherits Component, either directly or indirectly, then that component will be added to the Toolbox automatically and you can use it like any other. You can add your custom DataGridView to a form like you would any other control. If you already have a regular DataGridView that you want to replace without losing any code, just open the designer code file and change the type of that control from DataGridView to your class.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Conversion of subs and functions to a common class or routine

    The designer code file is hidden in VB projects by default, to prevent people accidentally breaking the designer. To see it, select the project or an item within it in the Solution Explorer and then click the Show All Files button on the toolbar. The designer code file will then be visible when you expand the form item.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    402

    Re: Conversion of subs and functions to a common class or routine

    This is very confusing, I have tried a number of things, but they all generate errors, The latter being with the project , i have created a class called DataGridViewCopyPaste, which has the code in it. However it did not recognise the DataGridView or Binding Source within each sub, so I had to add it in as a parameter. Then when the main procedure in Form1 tried to call it, it did not exist, so I have to prefix it all with DataGridView1.xxx. It then generated an error saying that it could not find the sub. So I made them Public. It then wanted them to be shared. So added Public Shared to them. This sort of works, but at no point have i inherited the Data Grid View, so I suspect that i have gone down the totally wrong route with this.
    Also

    What am I doing Wrong. Do you have any examples or more of a clue as regards what i should do in what order to convert the following so i can use it in every datagridview
    Last edited by Signalman; Sep 29th, 2021 at 03:05 PM.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Conversion of subs and functions to a common class or routine

    Here’s an example of an extended DGV….

    http://www.scproject.biz/DataGridVie...g.php#advanced

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    402

    Re: Conversion of subs and functions to a common class or routine

    I have looked the examples as suggested by .paul, which are some really useful routines. And downloaded the examples. Thanks .paul for the suggestion. However there is no class inheritance in there. All the routines have the extra functionality in the same form (just like my issue )and not In a separate inherited class.

    Can someone provide some further guidance to my querys in post #7. I am really at a loss as how to go about this ?
    Last edited by Signalman; Sep 27th, 2021 at 08:50 AM.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Conversion of subs and functions to a common class or routine

    The inheritance is right there at the top of the class. It's true that it uses extra classes and forms, but you don't need to do that for your extensions. There'll be hurdles, but you haven't even got started on the easy stuff yet...

    Code:
    Public Class ExtendedDataGridView
        Inherits DataGridView

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Conversion of subs and functions to a common class or routine

    You're looking for a compiled extended DGV??? You have to walk before you can run...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width