Creating dynamic controls for one row in a database - vb.net

I need to be able to create dynamic radio buttons and label for each row in a dataTable. The code i have written so far produces radio buttons which are linked so i select multiple buttons at once. Rather, the radio buttons de-select upon pressing another.
Do
For count = 0 To NoOfRecords - 1
For i = 1 To NoOfRecords - 1
Dim XCoord, YCoord As Integer
XCoord = 269
YCoord = 172
Dim XCoord1, YCoord1 As Integer
Dim Labeli As Label
Dim RadioButtonsi As RadioButton
XCoord1 = 269 : YCoord1 = 172
For count1 = 0 To NoOfRecords - 1
Labeli = New Label
Labeli.Location = New System.Drawing.Point(XCoord, YCoord)
YCoord = YCoord + 120
Labeli.Text = count1 & ". " & dataTable.Rows(count1).Item(0)
Me.Controls.Add(Labeli)
RadioButtonsi = New RadioButton
RadioButtonsi.Location = New System.Drawing.Point(XCoord1, (YCoord1 + 20))
YCoord1 = YCoord1 + 120
RadioButtonsi.Text = dataTable.Rows(count1).Item(1)
Me.Controls.Add(RadioButtonsi)
Next
Next
Next
Loop Until count = NoOfRecords
So just to summarize, I need to be able to create controls for each row in the dataTable which aren't linked.

Related

Arranging Buttons inside a Panel vb.net

I am trying to put 25 Buttons inside a Panel in VB.Net 2015 Classic Form Like the Picture but it is not working . could you please help... bellow my code
Dim i As Integer
For i = 1 To 25
newButton = New Windows.Forms.Button
newButton.AutoSize = True
newButton.Name = "btnButton" & i
newButton.Text = "Button " & i
newButton.Top = i * 5
newButton.Left = i * 25
newButton.Size = New Size(95, 70)
AddHandler newButton.Click, AddressOf ButtonClicked
Panel1.Controls.Add(newButton)
Next
Your code is creating the buttons, and the problem is that they are not arranged properly so what you need to do is arrange them in rows and columns. here i help you to do this;
Here this snippet will tell you how to arrange them in 5 columns and n rows:
Dim x As Integer = 5 ' x co-ordinate of the point
Dim y As Integer = 5 ' y co-ordinate of the point
For i = 1 To 25
If i Mod 5 = 0 Then ' For starting next row after column
y += 100 ' 100 is not mandatory change as per size of button
x = 0
Else
x += 100 ' 100 is not mandatory change as per size of button
End If
Dim p As Point = New Point(x, y)
Dim newButton = New Windows.Forms.Button
newButton.Location = p
//do the rest of formatting here
Panel1.Controls.Add(newButton)
Next

Overwrite a string from the middle

I am trying to make a function to create textboxes. I need to know if there is a way to replace the end of a string with the new number.
While x <= tbnumberofitems
Dim x As Integer = 0 ' looop count '
Dim y As Integer = 1 ' name count'
Dim label1name As String = "label"
Dim textbox1name As String = "textbox"
While x <= tbnumberofitems
y = y + 1
If x = 0 Then y = 1
Convert.ToString(y)
Dim label1 As New Label
label1.Name = label1name & y
'Code to create label box
Dim textbox1 As New TextBox
textbox1.Name = textbox1name & y
'code to create text box
x = x + 1
End While
This is what I currently have. What I need now is a way to make it where when the loop runs the next time, it changes the name to textbox2 and label2 on 3rd loop label 3 and textbox 3, etc. If that is not clear enough what I am trying to do is make it where the numberofitems, make it 5, creates 5 labels and 5 textboxes through the program.
Basicly Like this ..
For x as Integer = 1 to tbnumberofitems
'Code to create label
Dim lbl As New Label
lbl.Name = "label" & format(x)
lbl.Location = New Point(10, x*20)
Me.Controls.Add(lbl)
'Code to create textbox
Dim tb As New TextBox
tb.Name = "TextBox" & format(x)
tb.Location = New Point(100, x*20)
Me.Controls.Add(tb)
Next

Print multiple images in vb.net

In VB.NET, I need to print multiple Images of bar codes by arranging them in tabular format. For now what I am doing is, Creating the bar codes and adding them in new picture box. These Picture boxes are added on a panel which I am creating on form at run time and print that panel (with picture boxes in 4x9 table).
But, when I need to print more that 36 bar codes, this idea doesn't work.
So, Please suggest me some improvements in my code or any other way to do this job.
I am sorry, here is the code for generating images and adding them to the panel..
''' Method for create bar code images with a loop and adding them to the panel by picture box...
Private Function GetBarcodeText(RowId As Guid)
Dim BarcodeValue As StringBuilder = New StringBuilder(96)
Dim temp As New StringBuilder
Dim data As String
Dim param = New SqlParameter()
Dim imageNo As Integer = 0
Dim colorValue As String = ""
Dim scaleValue As String = ""
'' Adding the panel on the form which is dynamically created
Me.Controls.Add(outputPanel)
'' Setting the Initial size for the panel
outputPanel.Size = New Point(794, 112)
outputPanel.Name = "outputPanel"
outputPanel.BackColor = Drawing.Color.White
param.ParameterName = "#RowId"
param.Value = RowId
param.SqlDbType = SqlDbType.UniqueIdentifier
' Get the particular row of data from database
dt = objStockProvider.GetBarcodeDetails(param)
' GET colour code
Dim color As String = dt.Rows(0)("COLOUR").ToString()
Dim countColors As Integer = 0
' Get the color code numbers
param.ParameterName = "#Dscale"
param.Value = dgvViewTickets.CurrentRow.Cells("SCALE").Value.ToString()
countColors = objStockProvider.CountColorCodes(param)
For i = 1 To countColors
For j As Integer = 1 + ((12 / countColors) * (i - 1)) To (12 / countColors) * i
If dt.Rows(0)("S" + j.ToString()) <> 0 Then
Dim totalTicketsForASize As Integer
totalTicketsForASize = dt.Rows(0)("S" + j.ToString())
For k As Integer = 1 To totalTicketsForASize
' Set Bar code value which has to create
BarcodeValue = "123456789012"
' Create Barcode Image for given value
Dim image = GetBarcodeImage(BarcodeValue, colorValue, scaleValue)
If image IsNot Nothing Then
'' Create picture box to contain generated Image.
Dim pcbImage As New PictureBox
pcbImage.Width = W
pcbImage.Height = H
pcbImage.Image = image
pcbImage.Location = New Point(X, Y)
imageNo += 1
If imageNo Mod 4 = 0 Then
X = 15
Y += H
outputPanel.Height += H
Else
X += W
Y = Y
End If
pcbImage.Visible = True
'' Adding picture box to panel
outputPanel.Controls.Add(pcbImage)
End If
Next
End If
Next
color = color.Substring(color.IndexOf(",") + 1, color.Length - color.IndexOf(",") - 1)
Next
PrintGeneratedTickets()
End Function
Now, I am printing the panel by following method:
Private Sub PrintGeneratedTickets()
bmp = New Bitmap(outputPanel.DisplayRectangle.Width, outputPanel.DisplayRectangle.Height)
Dim G As Graphics = Graphics.FromImage(bmp)
G.DrawRectangle(Pens.White, New Rectangle(0, 0, Me.outputPanel.DisplayRectangle.Width, Me.outputPanel.DisplayRectangle.Height))
Dim Hdc As IntPtr = G.GetHdc()
SendMessage(outputPanel.Handle, WM_PRINT, Hdc, DrawingOptions.PRF_OWNED Or DrawingOptions.PRF_CHILDREN Or DrawingOptions.PRF_CLIENT Or DrawingOptions.PRF_NONCLIENT)
G.ReleaseHdc(Hdc)
pndocument.DocumentName = bmp.ToString()
Dim previewmode As New PrintPreviewDialog
previewmode.Document = pndocument
previewmode.WindowState = FormWindowState.Maximized
previewmode.PrintPreviewControl.Zoom = 1
pndocument.DefaultPageSettings.Margins.Top = 10
pndocument.DefaultPageSettings.Margins.Bottom = 30
pndocument.DefaultPageSettings.Margins.Left = 16
pndocument.DefaultPageSettings.Margins.Right = 16
pndocument.DefaultPageSettings.Landscape = False
' Set other properties.
previewmode.PrintPreviewControl.Columns = 4
previewmode.PrintPreviewControl.Rows = 9
previewmode.ShowDialog()
Dim file As String = DateTime.Now.ToString()
file = Path.GetFullPath("D:\Bar Codes\" + file.Replace("/", "-").Replace(":", ".") + ".bmp")
bmp.Save(file)
G.Dispose()
outputPanel.Controls.Clear()
End Sub
This code is working fine but what I need to do, is to fix the number of images (4x9) per page. But when I am trying to create more than it, that all are printing on a single page with compressed size.
Also when trying to re-run the code, It shows nothing in preview..
Some body please suggest the correction in code so that I can reprint the tickets and use paging for more than 36 images.
Well, Printing the Images on a panel was not a good idea.. I replaced the panel and created an array of images and used the print document directly and print after arranging images on it.
Thanks.

multiple DatagridView Rendering Vb.net

I'm able to add multiple datagridview 1 after another on panel.
by adding code of SysDragon from the same forum
Dim AllDataGrids As List(Of My_custom_DGV)
Dim lastCtrl As Control
AllDataGrids = New List(Of My_custom_DGV)
For j As Int32 = 0 To 3
Dim aDataGridView As New My_custom_DGV()
aDataGridView.for_date = ""
aDataGridView._count = week_count
aDataGridView.Dock = DockStyle.Top
aDataGridView.Dock = DockStyle.Right
aDataGridView.Dock = DockStyle.Left
aDataGridView.Dock = DockStyle.Bottom
AllDataGrids.Add(aDataGridView)
Next j
For i As Integer = 1 To 3
Dim dgv As My_custom_DGV = AllDataGrids(i)
' Dim dgv As DataGridView = AllDataGrids(i)
If dataGrid_Panel.Controls.Count.Equals(0) Then
dgv.Top = DataGridView1.Height + 20
Else
lastCtrl = dataGrid_Panel.Controls(dataGrid_Panel.Controls.Count - 1)
dgv.Top = lastCtrl.Top + lastCtrl.Height + 5
End If
dataGrid_Panel.Controls.Add(dgv)
Next
the problem is that when i scroll in between them then it is looking very bad(i.e. as i scroll,it is repeating the the image on panel again and again). does it render the view again when i Scroll,and may be calling the paint method of datagridview repeatedly. if yes then what is the solution?
Thanks.

Dynamically adding Panel and RadioButtons in Visual Basic

Guys I'm trying to dynamically create panels which are filled with seven radio buttons each.
I get the panels but they are only filled with 1 radio button each. What am I doing wrong here? QuestionQuantity is an Integer and is the variable that determines how many panels I will be creating. The code is in the form load function located.
Thanks,
Dim Pan As Panel
Dim RButton As RadioButton
For x As Integer = 1 To QuestionsQuantity Step 1
Pan = New Panel
Pan.Name = "Panel" & Convert.ToString(x)
Pan.Left = 300
Pan.Top = 100 + 52 * (x - 1)
Pan.Height = 48
Pan.Width = 280
Pan.BackColor = Color.Coral
Controls.Add(Pan)
For y As Integer = 1 To 7 Step 1
RButton = New RadioButton
RButton.Name = "RadioButton" & Convert.ToString(x) & Convert.ToString(y)
RButton.Left = 1 + 30 * (y - 1)
RButton.Top = 10
RButton.Text = Convert.ToString(y)
RButton.CheckAlign = System.Drawing.ContentAlignment.BottomCenter
RButton.TextAlign = System.Drawing.ContentAlignment.TopCenter
RButton.UseVisualStyleBackColor = True
Controls.Add(RButton)
Pan.Controls.Add(RButton)
Next
Next
I messed with it and took out the Panel section and just used the RadioButtons in order to see if i get seven of these. I can get seven if i ofset them in the y direction (.top) but it does not work for some reason in the x (.left) direction
For y As Integer = 1 To 7 Step 1
RButton = New RadioButton
RButton.Name = "RadioButton1" & Convert.ToString(y)
RButton.Left = 20 + (y * 30)
RButton.Top = 10
RButton.Text = Convert.ToString(y)
RButton.CheckAlign = System.Drawing.ContentAlignment.BottomCenter
RButton.TextAlign = System.Drawing.ContentAlignment.TopCenter
RButton.UseVisualStyleBackColor = True
Controls.Add(RButton)
Next
Please help me!
I finally got it. The problem was declaring the size of the RadioButton. It will be too big if not declared even if i make the spacing bigger.
RButton.Size = New System.Drawing.Size(17, 30)
That solved the problem.
You should be using a UserControl that contains your seven radio buttons.
For x As Integer = 1 To QuestionsQuantity
Dim pan As New QuestionUserControl
Pan.Name = "Panel" & Convert.ToString(x)
Pan.Left = 300
Pan.Top = 100 + 52 * (x - 1)
Pan.Height = 48
Pan.Width = 280
Pan.BackColor = Color.Coral
Me.Controls.Add(Pan)
Next
If sticking with your current code, remove this (you should be only adding it to the panel):
For y As Integer = 1 To 7
'// Controls.Add(RButton)
Pan.Controls.Add(RButton)
Next
As far as seeing the control, I'm guessing you aren't going "right" enough:
Dim leftMark As Integer = 20
For y As Integer = 1 To 7
'// code
RButton.Left = leftMark
'//code
leftMark += rButton.Width + 4
Next
with a Pan.Height = 48 there's not going to be much controls inside.
You add the button both to the panel an to Controls ?
Put the radiobuttons in a gridbox. You can dynamically add rows of radiobuttons as you wish. If this will work for you, I'll send a sample code when I get home.