Add Fields in vb.net - vb.net

Can I add multiple fields whenever I press a button (fields such TextBox) in VB.Net?
I tried this code but it didn't help me
Dim cLeft As Integer = 1
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
txt.Top = cLeft * 25
txt.Left = 150
txt.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
Return txt
End Function

That control will not be visible if it's position is under an other control.
I have some suggestions.
Use a flow layout panel control. This will automatically position your controls.
Just add new controls to the flow layout panel control collection.
Test the positions and sizes of other controls on your form and identify a location that isn't inhabited by another control. Then add your control to that location.
Hide controls until you need them to be visible.
Place controls into a panel und use this as o sort of group to hide or show all relevant controls simultaneously.
but
Based on one of the followup questions which you answered, it sounds like you might be looking for a spreadsheet control. In this case, why not simply use a datagridview control?

Related

Creating controls but not adding it to the form, how does it affect performance?

This question is not regarding coding help but more of an understanding of Visual Basics. If I were to programmatically create new controls and assign properties to them like so...
Dim MyControl As List(Of Control)
MyControl = New List(Of Control)
For i As Integer = 0 To 1000
Dim iControl As New Control
iControl.Text = "Control # " & i
iControl.Location = New Point(0,0)
MyControl.Add(iControl)
i += 1
Next
Since I'm not adding them to the form and they have no graphics, would having 1000 controls matter for performance sake? I am using these "hidden" controls sort of like an invisible point for another control to attach itself to, the other control can only attach itself to other controls.

Displaying images from a picturebox list

I am trying to display a line of pictures in my program. But I am having a problem, where it is only showing the first image in the imagelist and only showing one image-box.
Private Cards As New List(Of PictureBox)
Private Sub SetupCards()
For i As Integer = 0 To imglist1.Images.Count - 1
Dim PicCard As PictureBox = New PictureBox()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50
Me.Controls.Add(PicCard)
PicCard.Image = imglist1.Images(i)
Cards.Add(PicCard)
Next i
End Sub
You're placing the picture boxes on top of each other, which is why you only see the last card. You've got to set a different Left property for every picture box you add.
The solution is rather simple. Just add the picture box's width to Left, multiplied by the current index i.
PicCard.Left = 50 + PicCard.Width * i
Don't need to keep the imaging controls in your own list if you add them to a parent container control.
Use ListView or third-party controls, or use custom drawing code if you need to use ListBox (which wraps respective Windows control). See
C# Can I display images in a list box?

Accessing Textbox programmatically created

I need to read a datagrid numbers of items, and programmatically add tabs to one tabControl. No problem on reading the datagrid, no problem in creating the model in the tabcontrol. So, I read the number of items, create the tabs accordingly, with all textboxes already with the correct values and so on.
At this point, the user will update some information on the tabs created, and need to click a Update button. At this point, I need to read all tabs, one by one, accessing all textboxes created, and send this to my database.
The only thing I got no result till now is “How to access these programmatically created Textboxes?
This is how I create the textboxes inside the TabControl
Dim TXT As New TextBox
TXT = New TextBox
TXT.Location = New System.Drawing.Point(213, 25)
TXT.Width = 303
TXT.TextAlign = HorizontalAlignment.Center
TXT.Name = "TXT_02_" & tab_counter
TXT.Text = MAT_DTCP(1) 'ABERTURA
TXT.BackColor = ColorTranslator.FromOle(RGB(128, 255, 255))
FORM_01.TBC_DTCP.SelectedTab.Controls.Add(TXT)
You could use LINQ:
Dim allTextBoxes = From tab In FORM_01.TBC_DTCP.TabPages.Cast(Of TabPage)()
From txt In tab.Controls.OfType(Of TextBox)()
Where txt.Name.StartsWith("TXT_02_")
Select txt
For Each txt As TextBox In allTextBoxes
' ... '
Next

String to Object as Expression

I have multiple TextBox controls Monday that will hold a value. I want to be able to add up all of the Monday textboxes and store that value as monTotal. I am getting an error message that says string cannot be converted to integer.
For i As Integer = 1 To rowCount Step 1
Dim var As Object
var = "txtMonday" & i & ".Text"
monTotal = monTotal + CInt(var)
Next
The way you are attempting to obtain a reference to the text boxes is not idiomatic of VisualBasic .NET.
var = "txtMonday" & i & ".Text" ' this is not a way to obtain a reference to the text box's text
While it would be possible to accomplish something like that using reflection, you'd be much better off refactoring your code to use an array of text boxes.
Since you are probably using Windows Forms you could perhaps implement logic to find the text box control you are interested in on the form using something like this:
' assuming container is the control that contains the text boxes
For Each ctrl In container.Controls
If (ctrl.GetType() Is GetType(TextBox)) Then
If ctrl.Name.StartsWith("txtMonday") Then
Dim txt As TextBox = CType(ctrl, TextBox)
monTotal = monTotal + CInt(txt.Text)
End If
End If
Next
The example above assumes that all the txtMonday.. text boxes are placed inside a control named container. That could be the form itself, or some other panel or table.
If all the textboxes live on the form and there are none being used for other text work you could use this. You could place all the textboxes that contain values your looking for in a separate container and get them like below, but use that control collection.
Dim amount As Double = 0
For Each tb As Textbox In Me.Controls.OfType(Of Textbox)()
amount += Convert.ToDouble(tb.Text)
Next
Dim monTotal as double=0
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox AndAlso ctrl.Name.StartsWith("txtMonday") Then
monTotal = monTotal + val(ctrl.Text)
End If
Next

Add tooltip control dynamically

I have a child form that is completely created in code.
I would like to add tool tips to the textbox controls on the form. I know how to set up the tool tips on the fly but can't find a way to add the tooltip control to the form on the fly. All the hits I find on google refer to dragging the control from the designers toolbox.
I would need to do something like:
' Add tool tip control
Dim toolTip1 As New ToolTip()
toolTip1.ShowAlways = True
frm.Controls.Add(toolTip1)
This does not work
I tried adding a sub to handle the from.load event(with a handler that poitn to the sub) at design time but can not get passed the error "Tooltip1 is not declared" when adding the tooltip to the iundividual dynamic controls.
If I call this dynamic form from a parent form and add the tooltip control to the parent form I can use it for the child form. But how would I do this if I was creating the form from a routine that does not live on a parent form?
thanks
Dim frm As New Form
' Add tool tip control
''Dim toolTip1 As New ToolTip()
''toolTip1.ShowAlways = True
'Draw the Form object
'close the dynamic frm if existing already
If frm IsNot Nothing Then
frm.Close()
End If
frm = New Form()
frm.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
frm.Name = "frm_test"
'dimension is irrelevant at the moment
frm.ClientSize = New System.Drawing.Size(10, 10)
'the parent will be the current form
'frm.MdiParent = this;
'splash screen mode form, why not...
frm.ControlBox = True
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
frm.BackColor = System.Drawing.Color.LightGray
For Each item As MYFILE.Class in the Collection
Dim aTextBox As New TextBox()
aTextBox.Font = New System.Drawing.Font(sFont, Single.Parse(sSizeFont), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CByte(0))
aTextBox.BackColor = System.Drawing.Color.Yellow
aTextBox.Location = New System.Drawing.Point(iNewColumnPosition + 5 + intMaxWidthLabel, intVertPos)
aTextBox.Size = New System.Drawing.Size(intWidthTextBox + 10, intGapHeight)
'store the biggest width, so that the textboxes can be vertically aligned
If intWidthTextBox > intMaxWidthText Then
intMaxWidthText = intWidthTextBox
End If
'giving a name to all your object will be the only way
'to retrieve them and use them
'for the purpose of this sample, the name can be the
'same for all textboxes.
aTextBox.Name = item.ParameterName
'giving the maximun size in caracters for the textbox.
aTextBox.MaxLength = Integer.Parse(item.ParameterLength)
toolTip1.SetToolTip(aTextBox, "TIP" & intIndex.ToString)
'tab have to be ordered
aTextBox.TabIndex = intIndex
intIndex += 1
'Vertical position is to be manage according the
'tallest object in the form, in this case the
'textbox it self
intVertPos += intGapHeight
'adding the textbox to the form
frm.SuspendLayout()
aTextBox.SuspendLayout()
frm.Controls.Add(aTextBox)
Next
I left a lot of code out but this should give you an idea as to what I am doing
In vb it is
Dim tooltip As New ToolTip(components)
tooltip.SetToolTip(textBox1, "This is a textbox tooltip")
Sorry this is not in VB.NET but I am pretty sure you can easily convert this. The first line is to set ToolTip control to your form components. The second is how you set a tooltip to a control and give it the related text.
ToolTip tooltip = new ToolTip(components);
tooltip.SetToolTip(textBox1, "This is a textbox tooltip");