|
Dear ManojRajan..
I read your articles, and found it very useful for me. However, I am still facing problem with my code.
I am trying to make a control arrays of button and name each control with "Table " & CStr(i).
Below is my code..
Me.BackColor = Color.FromArgb(128, 128, 255)
Dim i As Integer
For i = 1 To 10
Table(i) = New Button
Table(i).Top = 250
Table(i).Width = 100
Table(i).Height = 100
Table(i).Left = 1.1 * i * Table(i).Width
Me.Controls.Add(Table(i))
Table(i).Text = "Table No " & CStr(i)
Table(i).BackColor = Color.Coral
AddHandler Table(i).Click, AddressOf ClickHandler
Next
Private Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Frm_Sales.ShowDialog()
Frm_Sales.lbl_tableno = Microsoft.VisualBasic.Right(CType(sender, Button).Text, 2)
End Sub
When I type the code above, I got this error
"Value of type 'String' cannot be converted to 'System.Windows.Form.Label'
Please help me to fix it
Thank You..
|
|
|
|
|
thanks a lot for explaing the thing by example. certainly it will be great help in my project at some stage. Also special thanks to "deliot" for providing very nice small example.
|
|
|
|
|
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 REM Tuesday, November 16, 2010 10:00 PM
1 REM based largely on these articles:
2 REM (1) http:
3 REM (2) http:
4 REM (3) http:
5
6 Public Class Form1
7 REM AllButtons_Click is a sub I created, note that it doesn't have a "handles" statement at the end
8 Private Sub AllButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
9
10 REM a button object that we will "link" to the actual button
11 Dim TempButton As Button
12
13 REM Convert the "sender" object, which is a System.Object to a Button.
14 REM This makes sure that we can set and check Button properties.
15 TempButton = CType(sender, Button)
16 Label1.Text = "You Pushed The Button Named " & TempButton.Name
17
18 REM Make a visible change to the button pushed.
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 REM create an array of 5 buttons to test with...
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 REM set initial X and Y location for first button
32 Xpos = 23
33 Ypos = 23
34
35 For I = 1 To 5
36 REM create Point object to use with Location
37 MyPoint.X = Xpos
38 MyPoint.Y = Ypos
39 REM create a new button
40 MyButton(I) = New Button
41 REM assign settings to new button
42 MyButton(I).Name = "Button" & I
43 MyButton(I).Text = "Button" & I
44 MyButton(I).Location = MyPoint
45 MyButton(I).Visible = True
46 REM add new button to "Me" which is Form1
47 Me.Controls.Add(MyButton(I))
48 REM tell Visual Basic which SUB will "handle"
49 REM when the button is clicked. In this case, it
50 REM will be handled by the AllButtons_Click sub!!
51 AddHandler MyButton(I).Click, AddressOf AllButtons_Click
52 REM change Xpos for next button, otherwise, all buttons
53 REM will be ON TOP of each other!!
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
<br />
Private Sub ClickButton(ByVal sender As System.Object, _<br />
ByVal e As System.EventArgs) <br />
Dim btn As Button<br />
btn = CType(sender, Button)<br />
Select Case btn.Tag<br />
Case 1<br />
Textbox1.Text="Clicked"<br />
Textbox2.Text=""<br />
Case 2<br />
Textbox1.Text=""<br />
Textbox2.Text="Clicked"<br />
End Select<br />
End Sub <br />
|
|
|
|
|
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 >>;;
|
|
|
|
|
I think nearly EVERYONE who has ever made a statement or comment on this subject has missed one of the big pictures. Looping thru the control array to set it, move it, size it color it etc. I really miss that simplicity.
A control array in VB.6, which used the now defunct index property value, was not ONLY helpful with events was EXTREMELY helpful in setting up buttons by allowing us to loop thru them at run time It made it SUPER simple to move the buttons around, change their values and colors etc looping thru the established array instead of dealing with them individually. For example. I build kiosks for exhibits for polling/trivia and information. I like to change the color of the backgrounds, buttons, values of buttons etc on the fly based on the question/fact etc. Making a related array that holds all of these values Example: for 10 questions each with no more than 5 answers might be Questions(10,5) The answers are indexes 0-4 as are the buttons so all I need to do is set the index for the questions I want to display and run thru a loop to transfer them to the button that matched the index value.
And Yes you can name all of your Answer buttons to the same name like Ans1, Ans2 and loop thru the entire collection of controls and find them and deal with them; but if you admit it or not; was far easier to already have an array of a control established for you.
All of that being said it is something easily learned, managed and once the concept is learned may be a little trickier to implement is still available just in a different method of creation. Drag and drop was far easier than build/name/loop/discover but once that is all done....its done. It pretty much works the same just takes a lot more thought to make it work well, and PROBABLY more CPU cycles to accomplish it which always ticks me off. When it comes to CPU cycles I am really like Scrooge, I want to save as many as I can for the heave lifting, like MEDIA or DB activity.
Interesting discussion.....I do still miss VB.6 simplicity at times. Glad there are many simular languages that are easy to teach and learn. I think the overhead for some languages keep many from learning them.
|
|
|
|
|