Add Controls dynamically and display information in them on windows form

irfi

Member
Joined
Sep 24, 2009
Messages
16
Programming Experience
1-3
Hi everyone,
I am making a windows application and would appreciate if anyone out there can assist me in adding dynamic (PictureBox, Label and a listbox) for each CustomerID value in a table.

Just like an array in a group box.

If there are 5 customers details then when i load the form it will add (PictureBox, Label and a listbox) for each customer dynamically and display them with their information. Moreover, if i add 6th Customer information in the database then next time i load the form it should display his/her information as well.

I did this in VB6 sometimes back but need a start to do in .net

Thanx
Cheers
irfi

Private Sub LoadCustomerInformation()
Dim Qry As String
Dim RS As New Recordset
Dim i As Integer
Qry = "select * from Customer_Details
RS.Open Qry, db, adOpenDynamic, adLockOptimistic
Do While Not RS.EOF
If i = 0 Then
Image2(i).Picture = LoadPicture(RS!PicturePath)
LblID(i) = RS!IDNo
LblName(i) = RS!CustomerName
LblNationality(i) = RS!Nationality
Else
Load Image2(i)
Image2(i).Left = Image2(i - 1).Left + Image2(i - 1).Width + 250
Image2(i).Picture = LoadPicture(RS!PicturePath)
Image2(i).Visible = True


Load LblID(i)
LblID(i).Left = Image2(i - 1).Left + Image2(i - 1).Width + 250
LblID(i) = RS!IDNo
LblID(i).Visible = True


Load LblName(i)
LblName(i).Left = Image2(i - 1).Left + Image2(i - 1).Width + 250
LblName(i) = RS!CustomerName
LblName(i).Visible = True


Load LblNationality(i)
LblNationality(i).Left = Image2(i - 1).Left + Image2(i - 1).Width + 250
LblNationality(i) = RS!Nationality
LblNationality(i).Visible = True
End If
i = i + 1
RS.MoveNext
Loop
End Sub
 
I think a good approach to this is to make a UserControl that has a picBox, label, and listbox. It is good OOP, and it simplifies your code. Have a Sub that makes each UC based on the numder of customers you have, and I would just add them to a FlowLayoutPanel for another sake of simplicity.
 
Have you considered using a datagridview?

It will allow you to dynamically add or remove rows depending on how many customers you have. The control allows columns to use things like combos and images.
 
Thanx for your reply.
Someone recommended me to use DATAREPEATER which i download and had success to some extent. The only thing i am stuck now is how do i filter the records for each employee iside the datagridview.

Inside the datarepeater i want to display the Employee_ID then Employee_Name and fill the Datagridview with all her/his leave details and want to repeat this for each employee.

For example in my table i have these cloumns:-
_____________________________________________________________
EMPLOYEE_ID EMPLOYEE_Name EMPLOYEE_Leave
_____________________________________________________________
001 Rebecca SICK
002 Sara ANNUAL
003 Rebecca SHORT
004 Rebecca SICK
005 Sara SHORT

Inside the Datarepeater i have placed TWO LABELS and A DATAGRIDVIEW.

FIRST LABEL FOR EMPLOYEE_ID
SECOND LABEL FOR EMPLOYEE_NAME
DATAGRIDVIEW 'TO FILTER AND DISPLAY ALL LEAVES TAKEN BY THE EMPLOYEE

Private _emptable As DataTable = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.Cursor = Cursors.WaitCursor

Try
_emptable = Employees.EmployeesData().Tables(0)

Labe1.DataBindings.Add("Text", _emptable, "Employee_ID")
Labe2.DataBindings.Add("Text", _emptable, "Employee_Name")
DataGridView1.DataSource = _emptable ' MAYBE NEED FILTERING HERE

DataRepeater1.DataSource = _emptable

Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

Me.Cursor = Cursors.Default

End Sub

Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles DataRepeater1.DrawItem

If e.DataRepeaterItem.ItemIndex Mod 2 = 0 Then
e.DataRepeaterItem.BackColor = Color.FromArgb(213, 224, 211)
Else
e.DataRepeaterItem.BackColor = SystemColors.Control
End If

End Sub

Public Class Employees

Private Shared _con As OleDbConnection = Nothing
Private Shared _com As OleDbCommand = Nothing
Private Shared _dap As OleDbDataAdapter = Nothing
Private Shared _dset As DataSet = Nothing

Private Shared _sqlstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\EMPLOYEEDETAILS.mdb"

Public Shared Function EmployeesData() As DataSet

Try
_con = New OleDbConnection(_sqlstr)
_con.Open()

_com = New OleDbCommand()
With _com
.Connection = _con

.CommandType = CommandType.Text

.CommandText = "SELECT Employee_ID, Employee_Name, Employee_Leave FROM Employees"
End With

_dap = New OleDbDataAdapter(_com)
_dset = New DataSet("DSetEmployees")
_dap.Fill(_dset)

Catch ex As Exception
_dset = Nothing
Throw ex
End Try

Return _dset

End Function
End Class
END CLASS
 
Back
Top