Denaes Posted August 11, 2003 Posted August 11, 2003 Ok, this is one thing I loved about vb6, the control array. It made some programming, especially with multiple repetitious controls, much much easier. An example would be MineSweeper. its like 16-100 buttons/labels, which are variable. with a control array you can easily add more controls and easily run through a for...next loop to place them all. I've seen people say that a Control Array is very un-OOP. Does VB.Net have anything to replace them, or to handle these situations? I know for things like clicking different buttons you can just add multiple ButtonNames.Events(click, mouseover, etc) to a prodecure and use the "Sender" to refer to which one was activated. Other than that, I'm at a loss to achieve half the functionality of a control array. Like in the minesweeper idea, how would you populate the program with 16-100 buttons/labels during runtime, AND make them all responsive to your procedure for clicking? I know I'm not even 1/4th the VB.Net programer that I was in VB6 (which isn't saying a whole lot), but control arrays made it so simple, only having to deal with the index of an existing control array. 1 or 1,000 members, it didn't matter. You could easily add more and keep track of them all. So far in VB.Net, its a LOT of copy and pasting to add controls to a procedure. Short of making 100 labels (tedius and unproffessional) and hiding those not used, I'm not sure how to go about this. There should be a site or FAQ or something devoted to this. Control Arrays were a pretty strong and useful technique, and at least in my college class/book, it was the preferred proper technique to have 1 procedure and a control array with a case statement rather than 12 seperate procedures. Any help would be greatly appreciated, even related concepts that I didn't specifically touch upon Quote
*Experts* mutant Posted August 11, 2003 *Experts* Posted August 11, 2003 You can still make a control array, just not in the designer: Dim textboxes(9) As TextBox For x as integer = 0 to 9 textboxes(x) = New TextBox textboxes(x).Location = New Point(x*10,x*10) Me.Controls.Add(textboxes(x)) 'using Me because Im assuming you 'place this code in the form now Next This example will add 10 textboxes to your form. Quote
Administrators PlausiblyDamp Posted August 11, 2003 Administrators Posted August 11, 2003 Rough and ready but should give you the idea of how to add controls dynamically just paste into the code behind a blank form Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As Button Dim i As Integer For i = 1 To 10 b = New Button b.Location = New Point(10, i * 25) b.Text = i Me.Controls.Add(b) AddHandler b.Click, AddressOf ButtonClick Next End Sub Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Dim b As Button b = DirectCast(sender, Button) MessageBox.Show(b.Text) End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Denaes Posted August 11, 2003 Author Posted August 11, 2003 You can still make a control array, just not in the designer: ... This example will add 10 textboxes to your form. damn, I searched around and didn't find this. if this works perfectly, then its better than vb6. If I recall vb6, you couldn't (or it was difficult) to create a control array in code, you kinda had to cheat and make two buttons (in the case of buttons) to create the array, and delete one, leaving the first with an index of 0. On the recieving end of the procedure for their .Click, how do you figure out the index of the clicked button? Sender.index? Quote
Leaders dynamic_sysop Posted August 11, 2003 Leaders Posted August 11, 2003 you can assign a value to the Tag property , eg: textboxes(x).Tag = x then use ... If textboxes(x).Tag = " value here eg: 0 " '/// do stuff End If Quote
Denaes Posted August 11, 2003 Author Posted August 11, 2003 Rough and ready but should give you the idea of how to add controls dynamically Wow, this is a real 'create your own control' type of breakdown. I like the other one better, but this looks informative and interesting. For i = 1 To 10 b = New Button b.Location = New Point(10, i * 25) b.Text = i Me.Controls.Add(b) AddHandler b.Click, AddressOf ButtonClick Next It looks like you're assigning the new button to the variable of b (for button). b.location is where it goes (x,y) b.text would display i in the text property (good for numbering buttons/whatever). me.controls.add(b) adds the button to Me (the form). addHandler b.Click I know this adds the .click event, but why do you need to use it? I thought the .click (as well as the others) were all methods in the button class. AddressOf ButtonClick - I have no clue what this does. Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Dim b As Button b = DirectCast(sender, Button) MessageBox.Show(b.Text) End Sub Oh, wait a sec. It looks like the AddressOf ButtonClick I saw above was telling it which procedure name to trigger on the .click. That's useful. Ok, whats DirectCast do? I've been seeing a few things like this, playing with objects. cType was another, which I think forces an object into the class of another (make an object a textbox for a second to use a textbox property). I also didn't pick up where you actually named the button. Does it not need a name since you used the AddressOf? I'm only trying to pick this apart for better understanding. My two .net books are fairly broad so don't go into a whole lot of detail in this sort of thing, and its not a major topic of the book, so what there is, is probobly stuffed in something elses section. Quote
Leaders dynamic_sysop Posted August 11, 2003 Leaders Posted August 11, 2003 Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Dim b As Button b = DirectCast(sender, Button) MessageBox.Show(b.Text) End Sub rather than using DirectCast , you can also type sender.Text. eg: Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) MessageBox.Show(sender.Text) End Sub here's an msdn article on DirectCast... DirectCast hope it helps. Quote
Administrators PlausiblyDamp Posted August 11, 2003 Administrators Posted August 11, 2003 Skipped naming the buttons because I'm lazy (seriously). Also Mutant's idea of having your own collection is a good one as it saves you having to iterate through ALL the controls on a form to find the dynamically generated controls. The addhandler command is very powerful in this respect as it will allow you to declare event handlers at compile time and actually only wire up what is needed at run-time. See modifications below Private Buttons(9) As ButtonTextBox 'Put in the form declarations section Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As Button Dim i As Integer For i = 0 To 9 b = New Button b.Location = New Point(10, i * 25) b.Text = i b.Name = "btn" & i.ToString() 'New line here Me.Controls.Add(b) TextBoxes(i)=b 'New line here AddHandler b.Click, AddressOf ButtonClick Next End Sub Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Dim b As Button b = DirectCast(sender, Button) MessageBox.Show(b.Text) End Sub I always code with Option Strict On so just using sender.Text will generate an error. The DirectCast is basically converting the generic sender object into a Button object - gives compile time checking of methods, events etc. The link dynamic_sysop gave explains it better than I could... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* mutant Posted August 11, 2003 *Experts* Posted August 11, 2003 Like PlausiblyDump said, dont use sender.Text as .Text is not a property of Object object and it will generate error with Option Strict On which should be on all times to prevent things like that. Quote
Leaders dynamic_sysop Posted August 11, 2003 Leaders Posted August 11, 2003 of course , i never thought about Option Strict. infact if you were using C# you wouldnt even be able to type " sender.Text " i also thought it easier to put a link to msdn rather than trying to type an explanation as to how DirectCast works. Quote
*Gurus* divil Posted August 12, 2003 *Gurus* Posted August 12, 2003 I would really hope that you would NOT use a control array do make something like minesweeper. Creating 100 (or many more) controls means creating 100 windows and quite frankly that's a huge waste. I've made a minesweeper clone before - all you have to do is draw the minefield yourself and you do away with having to have lots of buttons. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Denaes Posted August 12, 2003 Author Posted August 12, 2003 (edited) I would really hope that you would NOT use a control array do make something like minesweeper. Creating 100 (or many more) controls means creating 100 windows and quite frankly that's a huge waste. I've made a minesweeper clone before - all you have to do is draw the minefield yourself and you do away with having to have lots of buttons. For vb6, in class, this was a requirement that you used a control array. The point is learning how to use the control array, I'm really not going to go into buisiness creating and selling lean robust Minesweeper Clones :P Now I'm learning .net, and I'd like to learn how to use it in .net. Minesweeper was just an example really. I know in vb6 I had no problem using bitblt drawing api's to load pictures and create a grid on the form, which would work in this case requiring only 4 tiles (up, down, up flag, down mine... maybe down mine X). If you know of any sites that deal with drawing API's in .net or .net drawing functions, That'll probobly be one of my next few goals to learn. Edited August 12, 2003 by Denaes Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.