Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 5th, 2008, 06:56 PM
Authorized User
 
Join Date: Jul 2007
Location: Denver, CO, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating new buttons at runtime and making them v

Hi,

I have a little problem I was hoping someone could help me with. I have a user control that I have created, with a split container with two panels splitting vertically (two panels, one on top, one on bottom).

On the top panel, it is occupied by a DataGridView that is fully docked. On the bottom panel, I have a PictureBox control that is also fully docked too.

At runtime, depending on what items I have populated the DataGridView, I want to loop over them, and then on-the-fly, create a single Button control for each row in the DataGridView.

I have a Sub that creates an array of Button controls on the fly that has the following Dim statement:

Dim cmdLocArray(dgvTree.Rows.Count) As Button

As I loop through each row of the grid, I instantiate a new Button object for each element of the array. Then set its properties (size, location, text, etc.) that I want. At the end, I made sure that I set the button to visible.

Here is the code that I have below:

Public Sub PopulatePlacementView()

Dim intX As Integer
Dim cmdLocArray(dgvTree.Rows.Count) As Button
Dim imgLocIcon As Image
Dim strText As String
Dim sngX, sngY As Single

'Load the image first
imgLocIcon = Image.FromFile(AppInfo.InstallPath + "\Icons & Images\location.gif")

For intX = 0 To dgvTree.Rows.Count - 1

cmdLocArray(intX) = New Button

cmdLocArray(intX).Name = "cmdLoc" + CStr(intX)
cmdLocArray(intX).Parent = SplitContainer1.Panel1

cmdLocArray(intX).Size = New Size(150, 50)
sngX = picPlacement.Location.X + (100 * intX)
sngY = picPlacement.Location.Y + (100 * intX)
cmdLocArray(intX).Location = New Point(sngX, sngY)

cmdLocArray(intX).Image = imgLocIcon
cmdLocArray(intX).ImageAlign = ContentAlignment.TopLeft

cmdLocArray(intX).TextAlign = ContentAlignment.TopLeft
cmdLocArray(intX).TextImageRelation = TextImageRelation.ImageBeforeText

strText = "Location: " + dgvTree.Rows(intX).Cells("colLocation").Value
strText = strText + vbCrLf + "Date / Time: " + dgvTree.Rows(intX).Cells("colTimeStamp").Value
strText = strText + vbCrLf + "Reading: " + Trim(dgvTree.Rows(intX).Cells("colData").Value)
strText = strText + vbCrLf + "Status: " + Trim(dgvTree.Rows(intX).Cells("colStatus").Value)
strText = strText + vbCrLf + "Alarm Status: " + Trim(dgvTree.Rows(intX).Cells("colAlarms").Value)
cmdLocArray(intX).Text = strText
cmdLocArray(intX).FlatStyle = FlatStyle.Flat

cmdLocArray(intX).BringToFront()
cmdLocArray(intX).Visible = True

Me.Controls.Add(cmdLocArray(intX))

Next


End Sub

The odd thing here is that it does not generate any errors, but the buttons do not appear on the form. I thought that they might be hidden below the PictureBox control on the bottom panel, but when I call the .BringToFront() on each button within the For loop, nothing happened either.

I'm not sure how to go about doing this. It used to be a lot easier back in VB6 to create a control array, and also be able to trap their events (MouseMove, MouseOver, etc.) too. Is there a much easier way for me to do this? I do not want to endup having to create say 20 buttons at design time, and then having to loop through and unhide them when I need to. It just seems a bit odd that VB2008 does not allow me to do that easily.

I would really appreciate any help you can provide.

Thank you very much for your help.


Khoi Nguyen
__________________
Khoi Nguyen
Reply With Quote
  #2 (permalink)  
Old June 6th, 2008, 10:26 AM
Authorized User
 
Join Date: Jul 2007
Location: Denver, CO, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It turns out that all I needed to do was to invoke BringToFront() of each button after I added them to the Controls collection of the main UserControl container.

The reason why I decided to create a control array is to mimic what I had done in the past in VB6 where control arrays were possible. Not sure if it will work for VB2008 here.

The reason that I was aligning them that way was just for an initial location. Ultimately, I want to be able to put generic code within the MouseClick and MouseMove events of all buttons so that they can be dragged around within the main form.

To my knowledge, in VB2008, since there is no control array, each control has to have a unique name, and there are event handlers that reference their unique names. In the past, I would just create one button control on the form, and hide it. Also, I made the first button have an index of 0. Doing so, I can access the MouseClick and MouseMove events, and add code to loop through all button items in the control array to allow users to move them around within the form. Does any one have thoughts on how I can achieve similar functionality in VB2008?

Thank you very much for your help.

Khoi Nguyen
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating CheckBox at runtime. sachingowdar Java GUI 2 April 22nd, 2007 09:55 PM
creating checkboxes at runtime abhishekdubey_997 ASP.NET 1.0 and 1.1 Basics 14 November 24th, 2006 01:56 AM
Creating ctrls at runtime, not available@postback dayanand ASP.NET 1.0 and 1.1 Basics 8 August 10th, 2006 10:31 PM
Creating a DLL at runtime jai2k Beginning VB 6 1 January 5th, 2005 01:23 PM
creating access runtime versions Loralee Access VBA 7 September 2nd, 2004 10:26 PM



All times are GMT -4. The time now is 06:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.