 |
|
 |
This article helped me a GREAT DEAL in creating a 3D Tic Tac Toe Game.
Here is some "demo" code I used to test these concepts before I implemented them in my own code. Basically, it dynamically loads 5 command buttons, and sets it so all 5 command buttons are "handled" by the same subroutine. At first, I was pretty upset that Control Arrays were removed, but I think using an "Array Of Controls" with CType and AddHandler is better actually:
0 1 2 3 4 5
6 Public Class Form1
7 8 Private Sub AllButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
9
10 11 Dim TempButton As Button
12
13 14 15 TempButton = CType(sender, Button)
16 Label1.Text = "You Pushed The Button Named " & TempButton.Name
17
18 19 TempButton.Text = "Pushed"
20
21 End Sub
22
23 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
24 25 Dim MyButton(5) As Button
26 Dim MyPoint As Point
27 Dim I As Integer
28 Dim Xpos As Integer
29 Dim Ypos As Integer
30
31 32 Xpos = 23
33 Ypos = 23
34
35 For I = 1 To 5
36 37 MyPoint.X = Xpos
38 MyPoint.Y = Ypos
39 40 MyButton(I) = New Button
41 42 MyButton(I).Name = "Button" & I
43 MyButton(I).Text = "Button" & I
44 MyButton(I).Location = MyPoint
45 MyButton(I).Visible = True
46 47 Me.Controls.Add(MyButton(I))
48 49 50 51 AddHandler MyButton(I).Click, AddressOf AllButtons_Click
52 53 54 Xpos = Xpos + 100
55 Next I
56
57 End Sub
58
59 End Class
|
|
|
|
 |
|
 |
The below code segment from your example really helped me:
Dim btn As Button
btn = CType(sender, Button)
I used it to cause a button click on the selected button in my array of buttons I created from code.
Dim btn As Button ' Define a button to be clicked on.
btn = CType(Controls.Item(miFirstPageButtonID + miNewPagePosition), Button)
btn.PerformClick() ' Cause a button click on the desired button.
It works great.
Thank you for your efforts.
Bill
|
|
|
|
 |
|
 |
Hi ManojRajan,
Thanks for your wonderful article. I am facing a problem tackling with Control arrays, I have a piece of code in VB6.0 which has arnd 200 textboxes as a contol array. I want to convert that code into VB.Net, what can I do for this?
Witing for ur reply
Manish Ahuja
|
|
|
|
 |
|
 |
I would like to ask you a favor.
I need to do this but with a usercontrol, can you please give some advice to me?
I tried, but when i use the MyControl = cType(sender,Entity)..
it fails
Entity is the class name of my usercontrol
regards.
Hugo
|
|
|
|
 |
|
|
 |
|
 |
You have titled your article - "Control Arrays",
but you haven't finished up the idea on how to create an array of controls,
and most of all, you missed a major capability issue - in vb6 it was easy to know
which control have launched the event simply by checking the 'Index' property,
by in .Net there is no such property to use, so how is it done?
true, it's not that complicated, but i still think that taking that final step in demonstrating
how to actually implement the two techniques you've covered in the article by creating a 'Button' array
(can even be a simple array
dim ButtonsArray as button(3)
)
and demonstrating how to set instances and so on and so on,
as this kind of article is mostly intended for beginners,
it might be useful for the readers.
Anyway,
thanx for taking the time and sharing
Fade (Amit BS)
|
|
|
|
 |
|
 |
Dear Mr.Amit,
It seems that you have not read or understood the article properly. Because if you go through the first paragraph, it describes about how control arrays are managed in VB6 and how you can refer to the index property. Also beneath that you can see the text explained that you need not do anything to create a control array in .net but just append the event handler. Then in the example give it is shown how you can know which button is clicked.I think you were in a hurry to critcising, rather than aprreciating this effort. Any way i hope you will be having a better article than this. Good luck.
regards
ManojRajan
|
|
|
|
 |
|
 |
Hi,
First, i wish to apologize, i wasn't criticising your effort, i respect you effort.
nontheless, i offered some ideas on how i think you article can be improved, you can
appriciate it or not, and then delete it from your article.
and still i don't think you code shows how the identity of the buttonis revealed, let's say for example you have 10 buttons above of 10 text boxes, in their text property they have only a caption says "Click" and the click event should write "Clicked" to the text box below the button,
how could the coder know which button was pressed? there is no index property to reveal that information...
I don't have a simple solution for that one besides creating an additional object holding the Index value for each button, if you have a simpler solution, let me know, i would be very glad to lean from it.
Respectfuly yours,
Amit
Fade (Amit BS)
|
|
|
|
 |
|
 |
Hi,
I agree that the code posted is not good enough if you have all the buttons with the same text. The other way to do this, You can either use the name property of the control( which willl be unique) or else assign each button a unique value in the tag property of the button (Which is actually meant to store these kinds of information), and then refer to that. This will help you solve your problem.
MsgBox(CType(sender, Button).Name)
MsgBox(CType(sender, Button).Tag)
ManojRajan
|
|
|
|
 |
|
 |
Yes, you can use the name property if it's unique, and even if you want to create it in run time i believe that if you use some good naming conventions it will work, but it's not a good policy when creating many controls during run-time.
and by the way,
the tag property is used to determine the order which buttons recieve the focus when you click the 'TAB' key to switch to the next control, and it won't be wise to change it in run-time(i'm not even sure that you can) because they are all inter-connected to eachother - when you change one, they(the controls on the form) all re-adapt- might get messy
What i did is Inherited the Button class and added an 'Index' property, that way you can use it easily in arrays.
And please notice that your article is marked for VC6,VC7... all except for VB.Net [upper-right corner]
Fade (Amit BS)
|
|
|
|
 |
|
 |
Fade (Amit BS) wrote:
Yes, you can use the name property if it's unique, and even if you want to create it in run time i believe that if you use some good naming conventions it will work, but it's not a good policy when creating many controls during run-time.
You must set the name property of the control at run-time when you are creating controls dynamically unless you are wanting to use the default name (why would any one want to do that)
Fade (Amit BS) wrote:
the tag property is used to determine the order which buttons recieve the focus when you click the 'TAB' key to switch to the next control, and it won't be wise to change it in run-time(i'm not even sure that you can) because they are all inter-connected to eachother - when you change one, they(the controls on the form) all re-adapt- might get messy
I don't think you understand what the tag property is used for. It is used for holding any other information that may be required by your program. The tab order property is used for controlling the tab order. Changing it only affects the order in which the controls are tabbed to and only gets messy if you set them to the wrong order.
I am wondering if you understand 'AddHandler'. This is how control arrays are done in .NET. when you add a handler to an existing subroutine it is the same as if you had a control array in vb6 or added the control to the handles in the subroutine declaration. Yes, while it is true that there is no index for you to know which button was clicked, this can be determined by the name property as shown in the 'ClickButton' method. Or, if you don't want to use the name property, you can set an index (by using the tag property)as you create the controls and use it to determine the button that was clicked and then write the correct text into the correct textbox.
Lets assume that we have 2 textboxes on a form and we add 2 buttons during run-time. We set the tag property to 1 for the first button and to 2 for the second button (I will leave the code for this to you to write). As we create the new buttons we also add a handler for each button's click event to point us to the following sub. Now when we click on a button all we need to do is check the tag property and use the select case to determine which button was clicked and put the text into the correct textbox. No subclassing of the buttons required
Private Sub ClickButton(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim btn As Button
btn = CType(sender, Button)
Select Case btn.Tag
Case 1
Textbox1.Text="Clicked"
Textbox2.Text=""
Case 2
Textbox1.Text=""
Textbox2.Text="Clicked"
End Select
End Sub
|
|
|
|
 |
|
 |
I think you are more intrested in arguments than finding a solution, Please don;t waste others time for this. In the article no where i mentioned about changing the tab index and you have given a comment which i belive is more than the length of the article. And you replied the same thing that i mentioned in my previous response, ie, using the tag property you can very well know which button is clicked. And please dear friend you don't reserve the right to blame others without understanding the article and responses properly. My suggestion is that you can spare this time to create some good articles and post it here, as from your words, i belive you are very intelligent and good in coding . Expecting valuable inputs from you for this site. Happy days intelligent guy.
|
|
|
|
 |
|
 |
Looks like since you dont have a solution for his problems you are saying that he is intrested in arguments!
I agree that you put a lot of efforts and spent your time in making this article. But this person has also put in some effort in correcting your mistakes, and he gains nothing by winning an 'argument' . So, I think you were too proud of yourself, to accept your article was incomplete. And looks like it was some sort of jealousy which makes you sarcasticly say that he was 'very intelligent and good in coding'. And about the first reply you sent to him, you said that he did not appreciate your efforts! Well.... I don't need to tell you anything about it, you can yourself read the last line of the first message of his. It was strange to see him reply so patiently to such a message and was stranger to see you reply to such a patient person so rudely, clearly showing that you have no answers and are using the teasing as an escape way!
Now speaking about the article form my point of view:
I was browsing the net inorder to find a substitute for the VB6 in .NET, since a badly needed something like that. I totally agree that your article was the most informative one amoung all the articles I could find. But as Fade mentioned, the title says 'Control Arrays in VB.NET' and the article is incomplete! If you din't know, control arrays in VB6 have many more uses than what you have mentioned! And .NET is in on way easier than VB6 in this respect! The reason I told your article was the most informative is because it was the only thing that gave me a base and I thank you for that. You seem to be from tamil nadu, and I hope you have heard of a tamil saying that states something like that 'if you are doing something, do it completly and propely'. I know humans can't be prefect and you wrote whatever you felt Control arrays were all about, but when someone tried to correct you, you should have accepted it instead of showing that in so many years of programing, you don't have any sense of decency!
I wish you good luck in your future... for learning your manners, so that you can improve in life!
|
|
|
|
 |
|
 |
I don't think FADE understands the .NET principle. Perhaps he should go on a course and have it all explained.
|
|
|
|
 |
|
 |
fade makes a good point by not knowing thou... im new... and ir ead this article and im like wtf?!... control arrays were super simple inVB and this article doesn't explain everything in the code he makes..
it might be pointless posting this now as this article is 2 years old thou >>;;
|
|
|
|
 |