Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Controls at Runtime 1

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Sorry for the questions but I am new to Vb.Net and having some issues. I have a windows form which has a tablepanellayout control which I add at runtime. Depending on the source data I am also populating up to 60 rows of that table with controls - 16 controls on each line to be exact. That adds up to a lot of runtime controls to add - hence it takes about 4 minutes for the form to fully load - which is obviously not ideal.

I am just wondering what options I have. I know that I will have a maximum of 60 rows. One option would be to add 60 rows of controls at design time and then delete rows at runtime if I don't require them. However, the thought of adding 960 controls to a form manually and formatting them doesn't really appear to me either.

Do I have any other options to speed up loading of my form?

Mighty
 
What controls are you generating?
If it is only data then why not using a datatable generated at runtime and a datagridview bound to that? or a listview? datagridview can display images too.

Zameer Abdulla
 
Controls displayed are a combination of labels, textboxes, comboboxes and checkboxes. Each row of the table has 10 labels, 4 comboboxes, 1 textbox and 1 checkbox.

Labels may not be the most efficient option. I am interfacing with a micro-balance and when the user clicks on a label it reads the weight from the balance and writes the result to the text of the label and stores it in a database.

When the form loads, if there is already data stored for some of the rows, it populates the relevant fields with the data - and colours them depending on whether they have passed or failed in relation to specifications.

Mighty
 
Try something like this
for testing purpose in a new project You have a form with 2 datagridviews and a button

DataGridView1 has no columns assigned in the design view

While DatGridView2 has 3 columns added
1) Textbox Column
2) Button Column
3) Boolean column

And try this code
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As New DataTable
        With dt
            .Columns.Add("LableColumn", GetType(System.String))
            .Columns.Add("TextColumn", GetType(System.String))
            .Columns.Add("ButtonColumn", GetType(System.String))
            .Columns.Add("BooleanColumn", GetType(System.Boolean))
            .Rows.Add(New String() {"Lable1", "Text1", "Button1", True})
            .Rows.Add(New String() {"Lable2", "Text2", "Button2", False})
            .Rows.Add(New String() {"Lable3", "Text3", "Button3", True})
        End With

        Me.DataGridView1.DataSource = dt

        '======================================================

        With Me.DataGridView2
            .AutoGenerateColumns = False

            .Columns(0).Width = 100
            .Columns(0).DataPropertyName = "TextColumn"

            .Columns(1).Width = 50
            .Columns(1).DataPropertyName = "ButtonColumn"

            .Columns(2).Width = 50
            .Columns(2).DataPropertyName = "BooleanColumn"

            .DataSource = dt
        End With
    End Sub

Zameer Abdulla
 
Not ideal but could possibly be made to work. How can I add handlers to fields within the datagrid?

Mighty
 
You might want to check out the Data Repeater tool as well. It lets you define a set of controls and then bind that set to your data. When you run your program you get one set of controls per line of data. The msdn site has decent documentation on it and a video tutorial from Beth Massi in the Forms Over Data section.


 
I have started using the datarepeater tool and it looks good. I added a question in relation to it to another post rather than to this one by mistake.

I have another question in relation to accessing fields within the datarepeater (which I like by the way). I have a click event handler on a label in my datarepeater which reads a weight from an externally updated scales and then updates the text of the label that was clicked on and the one beside it.So how do I find out what row of the datarepeater contains the label that was clicked on and how to I reference another field on that row?

Mighty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top