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:
Dim Buttons01() As New cls01
Sub OpenMainForm()
Dim ctl As Control
Dim cnt01 As Integer
For Each ctl In Form1.Controls
If TypeOf ctl Is CommandButton Then
If Left(ctl.Name, 4) = "cmd0" Then
cnt01 = cnt01 + 1
ReDim Preserve Buttons01(1 To cnt01)
Set Buttons01(cnt01).Buttons01Class = ctl
End If
End If
Next ctl
vForm.Show
vForm.Refresh
End Sub
In Standard Module
VB Code:
Sub Group01()
Form1.txtCurScore.Text = Form1.txtCurScore.Text & Val(Right(Form1.ActiveControl.Name, 1))
End Sub
In Class Module
VB Code:
Public WithEvents Buttons01Class As CommandButton
Private Sub Buttons01Class_click()
Group01
End Sub
Re: Class Module - convert from VB6
so.. you basically just need to convert all this code to vb .net?
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...
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
1 Attachment(s)
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:
Dim Buttons01() As New cls01
doesn't like the NEW
VB Code:
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?
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. ;)
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...
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.
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:
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, _
Button2.Click, _
Button1.Click
Dim btn As Button = DirectCast(sender, Button)
Me.TextBox1.AppendText(CStr(btn.Tag))
End Sub
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...
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.
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:
Dim tb1 As New TextBox
tb1.Name = "TheBestTextBoxInTheWorld"
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.
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:
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
Call x01(sender)
End Sub
in a module:
VB Code:
Sub x01(ByVal sender)
Dim btn As Button = DirectCast(sender, Button)
Form1.txtCurScore.AppendText(CStr(btn.Text))
End Sub
thanks again for your help...
Re: Class Module - convert from VB6
VB Code:
Private buttons As Button()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put all the buttons in an array where the index corresponds to the number they represent.
Me.buttons = New Button() {Me.Button0, _
Me.Button1, _
Me.Button2, _
Me.Button3, _
Me.Button4, _
Me.Button5, _
Me.Button6, _
Me.Button7, _
Me.Button8, _
Me.Button9}
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click, _
Button8.Click, _
Button7.Click, _
Button6.Click, _
Button5.Click, _
Button4.Click, _
Button3.Click, _
Button2.Click, _
Button1.Click, _
Button0.Click
'Get the array index of the button that was clicked and append it to the existing text.
Me.TextBox1.AppendText(Array.IndexOf(Me.buttons, sender).ToString())
End Sub
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