binding navigator to next item in vb.net - vb.net

I am still new to visual basic 2010.
I would like to change the option of binding navigator because it always move to the next row.
How could I make it to move to the next 9 rows?
For example is this:
I load form main and fill 9 rows of data by adding code:
Dim inc As Integer = 0
label1.Text = dataSet.Tables("DPT").Rows(inc + 0).Items(2)
label2.Text = dataSet.Tables("DPT").Rows(inc + 1).Items(2)
.....
label9.Text = dataSet.Tables("DPT").Rows(inc + 8).Items(2)
but when I debug and press next item, all the value of label1.Text ---> label9.Text are filled with rows number two.... Could you help me guys?

There is alot wrong that I see you have going on here... Check out my edits...
Dim inc As Integer = 0 'This isn't doing anything...
label1.Text = dataSet.Tables("DPT").Rows(inc + 0).Items(2) 'Your always referencing item 2
label2.Text = dataSet.Tables("DPT").Rows(inc + 1).Items(2)
label9.Text = dataSet.Tables("DPT").Rows(inc + 8).Items(2)
First change all of your labels text to this...
label1.Text = dataSet.Tables("DPT").Rows(1).Item(1) 'Change the number for your row and item as you go down... Or better yet use your Item name instead of the number...

Related

vb.net find textbox name that begins with specified string in a TabControl

I have a TabControl in a form with 4 tabpages. Each tabpage has multiple GroupBox. Each GroupBox has tableLayoutPanel. Each tableLayoutPanel has pragmatically generated array of textbox. if checkbox in a tableLayoutPanel is checked by the user then textboxes in the respective row will be generated. Suppose, the name of one of my textbox array is txtMax(0), txtMax(1).......upto txtMax(42). I need to know how many txtMax(?) (along with their index array) has been generated and become visible. I have tried the the following:
Dim coutGene as integer = 0
Dim coutParameter as integer = 0
Dim indx As Integer
Dim cntl1, cntl2, cntl3 As Control
For Each cnn As TabPage In tabParameters.TabPages
cntl1 = DirectCast(cnn, TabPage)
For Each c2 As Control In cntl1.Controls
If TypeOf (c2) Is GroupBox Then
cntl2 = DirectCast(c2, GroupBox)
For Each c3 As Control In cntl2.Controls
If TypeOf (c3) Is TableLayoutPanel Then
cntl3 = DirectCast(c3, TableLayoutPanel)
For Each c4 As Control In cntl3.Controls
If TypeOf (c4) Is TextBox Then
Dim txt As TextBox = DirectCast(c4, TextBox)
If txt.Name.StartsWith("txtMax") Then
If txt.Visible = True Then
indx = CInt(Between(txt.Name, "(", ")"))
countGene = CInt(countGene + Val(txtGene(indx).Text))
countParameter = countParameter + 1
txtMax(indx).Tag = ""
End If
End If
End If
Next
End If
Next
End If
Next
Next
Function Between(value As String, a As String, b As String) As String
' Get positions for both string arguments.
Dim posA As Integer = value.IndexOf(a)
Dim posB As Integer = value.LastIndexOf(b)
If posA = -1 Then
Return ""
End If
If posB = -1 Then
Return ""
End If
Dim adjustedPosA As Integer = posA + a.Length
If adjustedPosA >= posB Then
Return ""
End If
' Get the substring between the two positions.
Return value.Substring(adjustedPosA, posB - adjustedPosA)
End Function
But the every time code is not getting inside the loop for this condition If txt.Name.StartsWith("txtMax") Then
I am stuck up here. Any assistance will be highly appreciated. Regards. Tariq
You should simplify that code to this:
For Each tp As TabPage In tabParameters.TabPages
For Each gb In tp.Controls.OfType(Of GroupBox)()
For Each tlp In gb.Controls.OfType(Of TableLayoutPanel)()
For Each tb In tlp.Controls.OfType(Of TextBox)().Where(Function(c) c.Visible AndAlso c.Name.StartsWith("txtMax"))
'If you get here, tb is definitely a visible TextBox with a Name starting with "txtMax".
Next
Next
Next
Next
That code will find every visible TextBox with a Name that starts with ""txtMax" inside a TableLayoutPanel, inside a GroupBox, inside a TabPage, inside tabParameters, guaranteed. If that code doesn;t find any such controls, it's because there are no such controls, so that's what you need to investigate.
Thank you for all yours guidelines and suggestion. I tried to implement all,,, but the code could not find the control even though the control exists and visible. I tried this simple code
For k = 1 To 42
If txtMax(k).Visible = True Then
countGene = CInt(countGene + Val(txtGene(k).Text))
countParameter = countParameter + 1
txtMax(k).Tag = ""
End If
Next
But i find that, it search and count the control that exists only in the present tabpage of the TabControl. So i modified the code, though not efficient code, to solve my problem.
I have 4 tabpages in the TabControl. So i tried to select each tabpage by its index and searched 4 times.
For tp As Integer = 0 To 3
tabParameters.SelectedIndex = tp
For k = 1 To 42
If txtMax(k).Visible = True Then
countGene = CInt(countGene + Val(txtGene(k).Text))
countParameter = countParameter + 1
txtMax(k).Tag = ""
End If
Next
Next
Though i solved my problem, but still i seek your advise for efficient code.

VB Winforms use for loop to reference control of specific number

Ok so say i have 9 buttons named btn1 to btn9 i need to reference these to set there texts accordingly i tried square bracket notation like this
For i = 0 To 9 Step +1
For Each btn In TableLayoutPanel7.Controls.OfType(Of Button)()
btn[i].text = i
Next
Next
this doesnt work is there any way to use i in the control name to reference it so it will just name them 1 - 9 respectively? any help anyone could provide on where to start would be greatly appreciated
Or, as I suggested:
For i = 1 To 9
TableLayoutPanel7.Controls("btn" & i).Text = i.ToString()
Next
This will set the text:
Dim counter As Integer = 1
For Each btn In Me.Controls.OfType(Of Button)()
btn.Text = counter
counter += 1
Next
However, if you need to set specific values, then you need to test the name of each button:
For Each btn In Me.Controls.OfType(Of Button)()
Select Case btn.Name
Case "Button1"
btn.Text = 1
Case "Button2"
btn.Text = 2
'etc
End Select
Next

How to create multiple labels

I'm new to "coding/programming". I'm trying to make a functional program - I call it "a point to pay". It's like those supermarket programs where they register the thing you are going to buy. So i need to create some labels to register products.
The code I have:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''When i click the button
If CantidadVer1.Text = 0 Then ''this verifies how many Labels i have created
CantidadVer1.Text = +1 ''this updates the verification
Dim lbl1 As New Label ''this creates the labels
lbl1.Size = New System.Drawing.Size(159, 23)
lbl1.Text = (Product.Text) ''product.text is a TextBox
lbl1.Location = New System.Drawing.Point(12, 80 + 20) '' i add 20 more everytime i create a label
Me.Controls.Add(lbl1)
ElseIf CantidadVer1.Text = 2 Then ''at this point it creates the label but "crashes" (It dosent work anymore)
CantidadVer1.Text = +1
Dim lbl2 As New Label
lbl2.Size = New System.Drawing.Size(159, 23)
lbl2.Text = (Product.Text)
lbl2.Location = New System.Drawing.Point(12, 80 + 40)
Me.Controls.Add(lbl2)
ElseIf CantidadVer1.Text = 2 Then
CantidadVer1.Text = +1
Dim lbl3 As New Label
lbl3.Size = New System.Drawing.Size(159, 23)
lbl3.Text = (Product.Text)
lbl3.Location = New System.Drawing.Point(12, 80 + 60)
Me.Controls.Add(lbl3)
ElseIf CantidadVer1.Text = 3 Then
CantidadVer1.Text = +1
Dim lbl4 As New Label
lbl4.Size = New System.Drawing.Size(159, 23)
lbl4.Text = (Product.Text)
lbl4.Location = New System.Drawing.Point(12, 80 + 80)
Me.Controls.Add(lbl4)
ElseIf CantidadVer1.Text = 4 Then
CantidadVer1.Text = +1
Dim lbl4 As New Label
lbl4.Size = New System.Drawing.Size(159, 23)
lbl4.Text = (Product.Text)
lbl4.Location = New System.Drawing.Point(12, 80 + 100)
Me.Controls.Add(lbl4)
End If
End Sub
So I execute it and then it creates 2 labels and then crashes.
It is supposed to create 5 labels .
Is there an easier way to create multiple labels without making the program crash?
In your code I only see one label created every time. I don't think the code crashes. Remember in an IF block, if the condition is met in the first part, it skips all subsequent ElseIf conditions.
With a little research, you'll see that the best option would be to use a DataGridView instead of a bunch of labels. You could simply add a new row for each item. Lets say you have a datagridview named DGV_Product with 3 textbox columns for product, quantity and price:
Dim price as Double = 1.99
Dim product as String = "Apple"
Dim qty as integer = 3
DGV_Product.Rows.Add(New String() {product, Cstr(qty), CStr(price)})
That adds a row containing "Apple, 3, 1.99"
But if you insist on using labels, This would work better:
1) Set The following variable at a class level.
Dim lbl_pos as integer
2) In your form Load event handler set the value to 20 less than where you want your first label to appear:
lbl_pos = 80 ' gathered from your code
3) Then in your click event handler of the button you increment the new label's position by 20 before adding the new label.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lbl_pos = lbl_pos + 20 'increment the position
Dim lbl as New Label
With lbl
.Text = "Your Text"
.Location = New Point(10,lbl_pos) '(left position, top position)
' And so Forth
End With
me.controls.add(lbl)
End Sub
With this approach you can add as many labels as you like without a bunch of If-ElseIf blocks.
I hope you rethink your approach and go with the DataGridView instead as it's much more practical and you can add as many rows as you like without hassle.
You have an error in your code .. The line
ElseIf CantidadVer1.Text = 2 Then ''at this point it creates the label but "crashes" (It dosent work anymore
should be
ElseIf CantidadVer1.Text = 1 Then ''at this point it creates the label but "crashes" (It dosent work anymore)
If you dont set the value for CantidadVer1 in the begining. Its value = "" not 0. so it will be the bug if you dont put 0 to the CantidadVer1.text
the first if maybe like this
If CantidadVer1.Text = "" OR CantidadVer1.Text = 0 Then
Your PTP is poorly designed. All the answers here are trying to solve your problem by looking based on what you've done.
But your model is kinda based on your what's in your view. For example, have you planned how you will retrieve all the added products ? The history is inside your view : good luck with that.
That's why I suggest to review your design. For example :
Use a List in your code behind to save your data. Your button_click should only do that (with some validations).
Use a repeater in your view. Bind that repeater to your List : and voila. Your repeater will take care of creating all the label corresponding to all the added products.

Adding labels and other controls to a groupbox dynamically in vb.net not working?

So I've been having problems adding labels from a record structure into a groupbox that stack below each other vertically. I could use a Flow Layout Panel but I have multiple labels I need to add from data in a record structure which I want to be able to scroll later with a vertical scrollbar as the autoscroll will only work with one panel only, and not all of them.
For some reason the program puts only one label in, and the others don't seem to be visible even though they have been created (I used a messagebox to check). Could someone please help me to put them in, as I am fairly new to programming and need help.
For context, the program loads 'materials' from an xml file and stores it in a record structure, then this bit of the program creates labels and radio buttons dynamically and puts it in a groupbox, so that it can be arranged manually on a pretty grid that will all become scrollable by using a scrollbar later.. Because the data I am working with is all related, and can have strings of multiple sizes (names and suppliers of materials for example) I didn't want to attempt to add whitespace to make the grid work but with a single label (not that the others would show beneath the first for some reason)
CODE:
'sets up Labels for editing materials
prgFunctions.loadMat()
Dim counter As Integer
Dim newMatIDLabel(numMatFile) As Label
Dim newMatRdb(numMatFile) As RadioButton
Dim lastPos As Integer
'testing to see if materials load GET RID OF LATER
' For counter = 1 To numMatFile
'ListBox1.Items.Add(materials(counter).matName)
' Next
'create the labels with information
For counter = 1 To numMatFile
'ID
newMatIDLabel(counter) = New Label
newMatIDLabel(counter).Name = "lblMatIDNum" & counter
newMatIDLabel(counter).Text = materials(counter).matID
newMatIDLabel(counter).Parent = gbxMaterials
' MsgBox(newMatIDLabel(counter).Name & " " & newMatIDLabel(counter).Text)
Next
'create the checkboxes NOW RADIO BUTTONS
For counter = 1 To numMatFile
newMatRdb(counter) = New RadioButton
newMatRdb(counter).Name = "chkMatSelectNum" & counter
newMatRdb(counter).Text = ""
newMatRdb(counter).Parent = gbxMaterials
Next
'matID locations
lastPos = 10
For counter = 1 To numMatFile
'SOMEHOW MOVE IT INTO THE GROUPBOX INSTEAD, as issues arise everywhere
newMatIDLabel(counter).Location = New Point(7, lastPos + 10)
lastPos = lastPos + 10
Next
Why not just:
lastPos = 10
For counter = 1 To numMatFile
Dim label As New Label
label.Name = "chkMatSelectNum" & counter
label.Text = materials(counter).matID
label.Location = New Point(7, lastPos)
label.Visible = True
gbxMaterials.Controls.Add(label)
lastPos += label.Height
Next
Based on F0r3v3r-A-N00b's answer:
The label has a height of 23, so it seems it is hiding the others.
This works for me:
Dim lastPos As Integer = 20
For counter As Integer = 1 To numMatFile
Dim label As New Label
label.Name = "chkMatSelectNum" & counter
label.Text = materials(counter).matId
label.AutoSize = True
label.Visible = True
label.Location = New Point(7, lastPos)
gbxMaterials.Controls.Add(label)
lastPos += 17
Next

Display output on the form in VB 2010

I'm designing a windows form. I have output to be displayed on the form it self.
Tried using print, but it is not working.
How do I do that?
I'M NOT PRINTING THE FORM.
ADDED:
I need to display 3 numbers with text string next to each number.
I want to do this in a way that it shows in the form or label in the form without overwriting the previous results.
example:
3 (wrong) 1 (right) 8 (wrong)
2 (wrong) 1 (right) 5 (right)
9 (right) 1 (right) 5 (right)
ADDED:
Thanks for the help everyone. one more question and i think i'm good.
I was thinking of doing something like this inside a loop, problem is I can't add a string and an int together to make a new var:
Xnum1 = Xnum1 + 50
Xnum2 = Xnum1 + ".0F"
Ynum1 = Ynum1 + 50
Ynum2 = Ynum1 + ".0F"
In VB6 you could use the Print statement to draw to the surface of the form. In VB.NET, however, you should be using the Form.CreateGraphics method to create a new Graphics object that can be used to draw to the form's surface. For instance:
Private Sub PrintText(text As String, x As Single, y As Single)
Dim g As Graphics = Me.CreateGraphics()
g.DrawString(text, New Font("Arial", 16), New SolidBrush(Color.Black), New PointF(x, y))
End Sub
That would be the closest equivalent to using the VB6 Print statement like that.
However, I would strongly recommend using a control to display the data. It looks like for the data you need to display, a simple multi-line text box or label would be sufficient. For instance:
Private Sub AppendResult(index As Integer, right As Boolean)
If right Then
TextBox1.Text = TextBox1.Text & " " & index.ToString() & " (right)"
Else
TextBox1.Text = TextBox1.Text & " " & index.ToString() & " (wrong)"
End If
End Sub
If you want to get more fancy, you could look into using a data grid, a list box, a list view, or even a table layout control instead.
I believe that the most efficient way is to use a tableLayoutPanel with 6 columns. Add in each cell a label showing in the first cell the number, in the second the indicator for that number (right/wrong). Do the same for second and third number.(second number = third and fourth cell, third number =fifth and sixth cell)
For the next set of numbers you can add a new row with with labels in each cell.
I'll add some code to make my answer more professional.
First you add the tableLayoutPanel in your form. You size it as you like (make its width, long enough to handle the data)
You delete the lastRow and then you add columns (you want to have 6 columns). You edit the size of the columns to be Percentage =16.67%
Public Class Form1
Private rowIndex
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i = 0 To 4 Step 2
Dim val As Integer = 3
AddLabels(val, i, 0)
Next
For i = 1 To 5 Step 2
Dim val As String = "right"
AddLabels(val, i, 0)
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
rowIndex = rowIndex + 1
Me.TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, 30))
Me.TableLayoutPanel1.Height = Me.TableLayoutPanel1.Height + 30
For i = 0 To 4 Step 2
Dim val As Integer = 3 'here you have to put your number
AddLabels(val, i, rowIndex)
Next
For i = 1 To 5 Step 2
Dim val As String = "right" 'here you have to put your indicator
AddLabels(val, i, rowIndex)
Next
End Sub
Private Sub AddLabels(ByVal lblValue As String, ByVal column As Integer, ByVal row As Integer)
Dim lblHeader As New Label
lblHeader.AutoSize = True
lblHeader.Margin = New Padding(0)
lblHeader.BackColor = Color.Transparent
lblHeader.TextAlign = ContentAlignment.MiddleLeft
lblHeader.Dock = DockStyle.None
lblHeader.Text = lblValue
'Put the lblHeader in the right cell
Dim lblHeaderPos As New TableLayoutPanelCellPosition(column, row)
TableLayoutPanel1.SetCellPosition(lblHeader, lblHeaderPos)
TableLayoutPanel1.Controls.Add(lblHeader)
End Sub
Let me know if you facing any problems.
Also if you don't know how many rows you will add, put the tableLyoutPanel inside a panel. Make the panel's property AutoScroll=True and then you can add infinite number of new rows.