-
Sep 25th, 2021, 05:34 PM
#1
Thread Starter
Hyperactive Member
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.
-
Sep 25th, 2021, 05:42 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2021, 05:50 PM
#3
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2021, 06:25 AM
#4
Thread Starter
Hyperactive Member
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 ?
-
Sep 26th, 2021, 06:50 AM
#5
Re: Conversion of subs and functions to a common class or routine
Originally Posted by Signalman
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.
Last edited by jmcilhinney; Sep 26th, 2021 at 07:00 AM.
-
Sep 26th, 2021, 07:06 AM
#6
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.
-
Sep 26th, 2021, 03:04 PM
#7
Thread Starter
Hyperactive Member
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.
-
Sep 26th, 2021, 06:36 PM
#8
Re: Conversion of subs and functions to a common class or routine
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 27th, 2021, 07:24 AM
#9
Thread Starter
Hyperactive Member
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.
-
Sep 27th, 2021, 12:11 PM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 27th, 2021, 12:13 PM
#11
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|