Creating control from 'control array?' - vb.net

I have an array of picture boxes as so:
Dim pieces(500) As PictureBox
pieces(1) = New PictureBox
With pieces(1)
.CreateControl()
.Visible = True
.BackColor = Color.Red
.Width = 50
.Height = 50
.Left = 50
.Top = 50
End With
The program does not crash or anything, but the picturebox is no where to be seen on the form. How to I make this work correctly?
And is 'Control Array' the correct term for this? or something else?

It won't show up until you add those PictureBoxes to a form.
I suppose you already have a Windows Form, so all you have to do is:
Window.Controls.Add(PictureBox)
Supposing your form object is called "Window"
You need to add them one by one and they don't need to be on an array, that's why there's a Control collection inside the Windows Form
Control Array is a VB 6 term, is not used in .NET anymore. The programming model between .NET and VB 6 is very different, you should take the time to go through a good tutorial or good book.

You need to add it to the form or panel where you want it/them displayed.
CreateControl only creates children and forces the creation of the control's Handle, but it will not place it onto the a form or parent control (it wouldn't know what to add it too!).

Related

Change Excel forms Label Length to match the length of it's text using VBA

How can I get an Excel Form Control Label which is on a worksheet to change it's length to match the length of the sting of characters in it's caption. Importantly it must work irrispective of what screen the file is viewed on or what the zoom setting is on the sheet.
So far I've got:
Sub LabelLength()
Dim XYLabel As Shape
Dim XYDataSheet As Worksheet
Set XYDataSheet = ThisWorkbook.Sheets(1)
Set XYLabel = XYDataSheet.Shapes(1) 'This is the Forms Label
XYLabel.OLEFormat.Object.Caption = Trim(XYDataSheet.Cells(1, 1).Value)
XYLabel.Width = Len(Trim(XYDataSheet.Cells(1, 1).Value)) * 7.5
End Sub
Which works, but does leave quite a lot of space after some captions.
I don't think this can be done with a Form control, at least not directly. If you want to stick with a Form control, I think you're on the right track with scaling its .width property based on the text size. To do it better than that, you'd probably have to build a dictionary object or something and sum the width of all characters in the string based on the width you had listed in the dictionary object.
However, if you can switch your label to an ActiveX control, then you can solve this by using:
.WordWrap = False
.AutoSize = True
I think people in general prefer to use Form controls because they're a bit more simple. But I also think, in general, ActiveX controls have more flexibility and you can customize them to your needs more. You can read a bit more on Form controls vs. ActiveX controls here.

Combobox event listener

I'm trying to use the solution given to this issue : Programmatically create event listener in VBA in my particular case :
I programmatically create comboboxes. What I would like to do is to programmatically create a single textbox with a precise label or several texboxes with as many labels next to the generated comboboxes depending on their values.
Here is the code I use to programmatically create those comboboxes :
Set listBoxB1 = Frame1.Controls.Add("Forms.ComboBox.1")
With listBoxB1
.Name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
So I want that when the user choses the value LIST, a single textbox is displayed next to the combobox with a label and when he or she uses the value NUM, 6 textboxes are created next to the combobox and aligned with it horizontally.
I actually want that the display changes automatically when the user changes the value of the combobox, it would be helpful if you can give some indications about that.
Can you please tell me how I can adapt the solution in the link above to my case, because I'm very new to VBA and don't exactly know how to do that, I have tried to implement the code given but failed.
It really is somewhat involved. This question discusses it: Assign on-click VBA function to a dynamically created button on Excel Userform . The accepted answer contains a trick that I have used many, many times. Create all the controls you will ever need ahead of time (there is only a finite amount of real estate on a form so in practice this will be a reasonably small number) and make them visible (or hide them) dynamically as you need them by controlling the .visible property. It is even possible to have 1 control sit on top of another in the design view with only 1 visible at run time. The visible one at run time will receive the events.

Dynamically Resize forms and controls inspite of change in Display size (Small/Medium/Large) in VB.net

We have developed windows application which is including many forms and controls with default small display size and it is working fine.
but in client systems,displays are not consistent. so when the same code runs with medium/large screen size, the controls are overlapping
It would be great help if anyone gives solution for dynamic re-size/fit the form & controls for any display (small-100%, medium-125%, large-150%)
and also Is there any way to find the Display size (small-100%, medium-125%, large-150%) in vb.net
You can change display size in control-Panel.
You can use the Anchor property to automatically adjust the control size according to its form size. You can anchor any side of the control to that side of the form. For example, if I anchor all four sides of a picturebox to the form, the picturebox will mimic the shape of the form when the form is resized. If you anchor only the top and the bottom of the picturebox, for example, it will change height with the form, but the width and horizontal position will remain constant.
Finally found a solution by doing R&D
call the below function in the Form_Load like ScaleForm(me)
Public Sub ScaleForm(WindowsForm As System.Windows.Forms.Form)
Using g As System.Drawing.Graphics = WindowsForm.CreateGraphics
Dim sngScaleFactor As Single
Dim sngFontFactor As Single = 1
If g.DpiX > 96 Then
sngScaleFactor = g.DpiX / 96
'sngFontFactor = 96 / g.DpiY
End If
If WindowsForm.AutoScaleDimensions = WindowsForm.CurrentAutoScaleDimensions Then
'ucWindowsFormHost.ScaleControl(WindowsForm, sngFontFactor)
WindowsForm.Scale(sngScaleFactor)
End If
End Using
End Sub

How to add a control after another dynamically?

What i am looking for is how to add another control(say a textbox directly under another textbox) that is added through code rather than the designer. I can get it to work using the .height property of the control then adding another 10-20 to it
dim space as integer
space += textbox1.height + 10
however, is there a way to do this on the location rather than the height? In this case, if I want to add a textbox at the very end of the frame or groupbox without having to add additional in between. Since the location takes two parameters(x,y), is it possible to place controls based on another controls location?
Does this help? (not clear what you are after, but yes, you can set the Location or Size all at once):
Dim thisTB as new TextBox
thisTB.Location = new Point(xSpot, ySpot)
thisTB.Size = otherTB.Size
Me.Controls.Add(thisTB)

Issue with a vb.net panel control loop

I have a loop in vb.net where I am wanting to display 50 panels, all with the same 3 controls. Only the last control is populated with the 3 controls, why is this?
Dim PanelVerticalPoint As Integer = btDF.Height * 6
For counter = 1 To 50 Step +1
Dim ButtonPanel As Panel = New Panel
With ButtonPanel
ButtonPanel.Location = New Point(0, PanelVerticalPoint)
ButtonPanel.Size = New Size(btDF.Width, btDF.Height)
Me.Controls.Add(ButtonPanel)
ButtonPanel.Controls.Add(btCustomButtonMenu)
ButtonPanel.Controls.Add(btCustomTextBox)
ButtonPanel.Controls.Add(btCustomButton)
End With
PanelVerticalPoint = PanelVerticalPoint + btDF.Height
Next counter
You have to add a new instance of the buttons to each panel. You are adding the same button instance to the panels so each add is really moving the button.
It's not easy to clone a control. It looks like your case might be appropriate for a user control instead. Make the user control in the designer with the buttons and text box, then just create many instances of the user control instead of the panel.
Here's a very similar question with that kind of answer
Clone Winform control