Results 1 to 15 of 15

Thread: Class Module - convert from VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Class Module - convert from VB6

    New to both VB6 and VB.NET but trying to learn both...
    i created a class module (which is working) in VB6...
    can't seem to figure out how to get same functionality in VB.NET....any help would be great...this is what i have in VB6....all applicable controls are named cmd00 to cmd09...this is fairly simple and just have buttons for #'s 0-9...that when clicked just concatenate to the end of a text box (txtCurScore)...any help ".NETing" this would be appreciated...

    In Standard Module
    VB Code:
    1. Dim Buttons01() As New cls01
    2. Sub OpenMainForm()                                
    3.   Dim ctl As Control
    4.   Dim cnt01 As Integer
    5.   For Each ctl In Form1.Controls
    6.     If TypeOf ctl Is CommandButton Then
    7.       If Left(ctl.Name, 4) = "cmd0" Then
    8.         cnt01 = cnt01 + 1
    9.         ReDim Preserve Buttons01(1 To cnt01)
    10.         Set Buttons01(cnt01).Buttons01Class = ctl
    11.       End If
    12.     End If
    13.   Next ctl
    14.   vForm.Show
    15.   vForm.Refresh
    16. End Sub

    In Standard Module
    VB Code:
    1. Sub Group01()
    2.   Form1.txtCurScore.Text = Form1.txtCurScore.Text & Val(Right(Form1.ActiveControl.Name, 1))
    3. End Sub


    In Class Module
    VB Code:
    1. Public WithEvents Buttons01Class As CommandButton
    2.  
    3. Private Sub Buttons01Class_click()
    4.   Group01
    5. End Sub
    Last edited by TheNoocH; Oct 16th, 2006 at 02:37 PM.

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Class Module - convert from VB6

    so.. you basically just need to convert all this code to vb .net?

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Re: Class Module - convert from VB6

    Fromethius...essentially....i tried copy and paste into .NET and ran into a lot of little issues...figured i'd see if i can get a little help...plus curious if approach is even right for .NET...
    Last edited by TheNoocH; Oct 16th, 2006 at 02:51 PM.

  4. #4
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Class Module - convert from VB6

    well.. here's what might help. paste it into .net and take a picture of the error box and stuff and we'll try to convert it

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Re: Class Module - convert from VB6

    Here is an attachment of the project...note: this is only a tiny part extracted from a larger project...but just focusing on the Class Module issue...essentially all i want to be able to do with this is press the buttons on the form and have the number show in the text box concatenated...so if i hit the 5 button then the 3 button then the 8 button....538 would show up in the text box....this is the simplest eg i had...but hopefully if i can see how it ports to .NET (note: using 2005 express) then i can hopefully figure out the other classes i have to port....thanks in advance for any help....

    currently issues/errors with these two lines...

    VB Code:
    1. Dim Buttons01() As New cls01
    doesn't like the NEW

    VB Code:
    1. ReDim Preserve Buttons01(1 To cnt01)

    wants the lower bound of array to start with 0

    i tried removing the New and changing the 1 to a 0...got rid of the errors ...but when ran the form and pressed buttons nothing went into the text box....so obviously i'm missing something...i'm assuming VB.NET may handle this process differently?
    Attached Files Attached Files
    Last edited by TheNoocH; Oct 16th, 2006 at 03:49 PM.

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

    Re: Class Module - convert from VB6

    It's not a good idea to paste VB6 code into VB.NET and expect it to work. VS has an item on the Tools menu named "Upgrade VB6 Code", and it doesn't just make coffee and do the filing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Re: Class Module - convert from VB6

    jmc,
    i initially tried to upgrade from VB6 like you stated i realize it doesn't work great...that's why i posted this...but maybe i didn't phrase my initial question properly...

    so i guess my followup question is how best do i handle the situation i'm after in VB.NET...
    i have a group of buttons that i want to all perform the same task...
    in VB6 i could handle it with the code (class module) i posted earlier...
    i'm just looking for best way to do the same thing in .NET....

    any help would be great...

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Class Module - convert from VB6

    Yes, but it doesn't do ALL the work, either.

    I think you will find several noticeable changes, but much of what you are dealing with are areas that I haven't ever dealt with, like control arrays.

    This line:
    If TypeOf ctl Is CommandButton Then

    has almost certainly changed to something along the lines of ctl.Type


    This line:
    cnt01 = cnt01 + 1
    should now be
    cnt01+=1

    Then, aren't you dealing with a control array. I've never actually used them, either in VB6 or VB.NET, but I don't think they are still there in VB.NET. That would get around the point that you are using a 1 based array, while arrays in VB.NET are 0 based. If you don't have a control array, it doesn't matter what the base is.

    Ok, one last difference that I actually know something about:

    Form1.txtCurScore.Text = Form1.txtCurScore.Text & Val(Right(Form1.ActiveControl.Name, 1))

    Will change, because Val is legacy, as is Right. So this becomes:

    Form1.txtCurScore.Text &=Form1.ActiveControl.Name.substring(8)

    This assumes that the name of the button is Buttons0x.
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Class Module - convert from VB6

    There are I don't know how many posts on this forum relating to how to handle situations for which you would use a ControlArray in VB6. If you want 10 buttons then add 10 buttons to your form. If you want to access them by index then create an array in code, assign your buttons to the elements and then you cn refer to them by index. If you want all 10 buttons to do something similar then create one method to handle the Click event of all 10. Within the method you can cast the 'sender' argument as Button and you've got a reference to the button that raised the event. This sort of stuff has all been posted many times before. If you have 10 buttons on a form and you each to append a digit to the text in a TextBox then assign the appropriate digit to the Tag property of each button and do something like this:
    VB Code:
    1. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, _
    2.                                                                                              Button2.Click, _
    3.                                                                                              Button1.Click
    4.     Dim btn As Button = DirectCast(sender, Button)
    5.  
    6.     Me.TextBox1.AppendText(CStr(btn.Tag))
    7. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Re: Class Module - convert from VB6

    jmc,
    thanks for your example...it helps alot...i tried searches but didn't realize "control array" was the term i needed to look for...
    thanks again...

    quick followup questions to make sure i get this...
    1) after the handles i would need to add all 10 command buttons...is there a way to loop through them all eg...if they all started with cmd0?
    2) button_click....i can call this whatever i want...eg test_click?
    3) dim btn as button....btn is variable and can be anything...Button is the form control
    4) directcast is the method to handle the control array....and sender is the object ...and button is the type of control?
    5) the rest of the code just handles what you want to do with btn...
    6) this code goes behind the form? instead of the me.textbox line could i call a sub instead?

    let me know if i'm misunderstanding anything...like i stated before i'm trying to learn/understand this stuff better...

    thanks for all your help...i greatly appreciate it...

    PS....jmc, great links in your signature...
    Last edited by TheNoocH; Oct 17th, 2006 at 07:40 AM.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Class Module - convert from VB6

    1) Pass.
    2) Yes, the name doesn't matter, the Handles part is what tells the compiler what the sub is used for.
    3) Yes, btn is the variable name, Button is the type.
    4) Direct cast tells the compiler that the value in this variable is a memory address of an object of this other type. Sender is of type System.Object, which is pretty generic. If you were to just use that type, you wouldn't have much functionality. Therefore, you are informing the compiler as to what type the object REALLY is. This works because any reference type variable holds only a pointer (the address of the first byte) to the object, and any object pointer can be considered a pointer to its base type. Since every object is derived from System.Object, then every object can be considered as a pointer to a System.Object. By using DirectCast, you are telling the compiler that the object is really one specific type of System.Object.
    5) Yes.
    6) Yes, and in your case you might well want to, since you don't necessarily want to use the tag property. However, the tag property could be a better way to get the numeral you want rather than using all that substring stuff on the name.
    My usual boring signature: Nothing

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Class Module - convert from VB6

    Question number 1 suggests that you're missing the point. The idea is that you don't refer to objects by their names. In fact, most objects don't even have names. Controls have a Name property that will match the default identifier used to refer to them in code when they're added to the form in the designer. There's nothing ot stop you assigning different controls to different variables or changing their Name properties though. Have a look at this example:
    VB Code:
    1. Dim tb1 As New TextBox
    2.  
    3. tb1.Name = "TheBestTextBoxInTheWorld"
    4.  
    5. Dim tb2 As TextBox = tb1
    You now have one TextBox object with a Name property "TheBestTextBoxInTheWorld" referred to by two variables named 'tb1' and 'tb2'.

    If you want to refer to objects by a numerical index then put them in an array and then refer to them as an element of that array by index. All this talk of indexes is unnecessary anyway. What is the loop for? It doesn't serve a purpose. Add each variable to the Handles clause of the method. That's all you need to do. The IDE will even do it for you. Select all the Buttons in the desinger, go to the Properties window, clcik the Events button, double-click the Click event. Tada! One event handler with all the Buttons' Click events listed in the Handles clause.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Re: Class Module - convert from VB6

    Shaggy, thanks for your response...and yes i keep forgetting about TAG...

    JMc, thanks for the clarification...and thanks for the tip on having the IDE add all the selected buttons to the handles clause...

    i'm sure i'll get all this stuff slowly but surely...you guys help alot...

    PS...for #6 this is what i tried and it seems to work...can you let me know if I handle this the proper way? thanks

    in the form code:
    VB Code:
    1. Private Sub X01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd01.Click, cmd02.Click, cmd03.Click, cmd04.Click, cmd05.Click, cmd06.Click, cmd07.Click, cmd08.Click, cmd09.Click, cmd00.Click
    2.         Call x01(sender)
    3.     End Sub

    in a module:
    VB Code:
    1. Sub x01(ByVal sender)
    2.         Dim btn As Button = DirectCast(sender, Button)
    3.         Form1.txtCurScore.AppendText(CStr(btn.Text))      
    4.     End Sub

    thanks again for your help...
    Last edited by TheNoocH; Oct 18th, 2006 at 12:39 PM.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Class Module - convert from VB6

    VB Code:
    1. Private buttons As Button()
    2.  
    3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     'Put all the buttons in an array where the index corresponds to the number they represent.
    5.     Me.buttons = New Button() {Me.Button0, _
    6.                                Me.Button1, _
    7.                                Me.Button2, _
    8.                                Me.Button3, _
    9.                                Me.Button4, _
    10.                                Me.Button5, _
    11.                                Me.Button6, _
    12.                                Me.Button7, _
    13.                                Me.Button8, _
    14.                                Me.Button9}
    15. End Sub
    16.  
    17. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click, _
    18.                                                                                              Button8.Click, _
    19.                                                                                              Button7.Click, _
    20.                                                                                              Button6.Click, _
    21.                                                                                              Button5.Click, _
    22.                                                                                              Button4.Click, _
    23.                                                                                              Button3.Click, _
    24.                                                                                              Button2.Click, _
    25.                                                                                              Button1.Click, _
    26.                                                                                              Button0.Click
    27.     'Get the array index of the button that was clicked and append it to the existing text.
    28.     Me.TextBox1.AppendText(Array.IndexOf(Me.buttons, sender).ToString())
    29. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    61

    Re: Class Module - convert from VB6

    jmc,
    thanks for the example...i think i understand what you are doing....but is there a way to pass it so that the append is happening in a module instead of the form code? my main issue is what i am trying to do is have 3 seperate forms as a front end with a form chooser to select which one ....so i only want to make changes in one place (ie module rather than each individual form code)....hopefully i'm making sense...

    and is there anything inherently wrong with my last attempt?

    thanks

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