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...
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.
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?
Last edited by TheNoocH; Oct 16th, 2006 at 03:49 PM.
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.
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....
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:
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, _
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.
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.
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.
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?