Results 1 to 6 of 6

Thread: [RESOLVED] Adding controls from Database on formLoad

  1. #1

    Thread Starter
    New Member < Sal >'s Avatar
    Join Date
    Oct 2009
    Location
    Sydney, Australia
    Posts
    6

    Resolved [RESOLVED] Adding controls from Database on formLoad

    Hi all,
    INTRO
    My first post here so ill just give a brief intro and appology for this informal post.
    I'm a junior programmer and keen to learn, from Australia, 18yrs old.


    Problem
    ok so to the problem i've got.
    I want my application to be able to be changed by the user.
    For example on my form I have the labels Blue, Red and Green but the user requires yellow and another colour which is unkown to me so they would have an ADD function which saves to the database so next time they login their colours are there as labels.

    How would you pick up the name of the colour from the database? and place that into your code to create a dynamic control with the same name as the colour which is unkown?

    Is there a way of changing a variable name?
    I went about it this way but am getting stuck:
    Code:
    Dim SQL As String
            SQL = "select * from Colours where type ='control' "
            Dim dt As DataTable = DAC.ExecuteQuery(SQL)
            Dim dr As DataRow
            Dim i As Integer = 0
            Dim controlName As System.Windows.Forms.Label()
            While i < dt.Rows.Count
                dr = dt.Rows(i)
                controlName = dr("Name")
    '' I want controlName to be changed
    
                i = i + 1
            End While
    So if there is any better or other way that works please and thank you.

    Cheers
    Last edited by < Sal >; Oct 13th, 2009 at 12:31 AM.

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Adding controls from Database on formLoad

    All you want :

    When user open their app , that label will go to backcolor of it in last access time, is it ?
    If so , you can use setting properties.
    Click to label, choose its properti and choose :

    Application property then choose the backcolor and name it a new name.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding controls from Database on formLoad

    Welcome fellow Sydneysider!

    The answer is that you cannot change 'controlName', nor do you really want to. Think about it. You are you, right? It doesn't matter whether I refer to you as your mother's son, your uncle's nephew, your dog's master or whatever; it's still you, right? the name of the reference doesn't make any difference. You could have a hundred different variables that all refer to the same Label object. Those variables would all be different, but there's still only one Label, right? Not only that, once you exit the method in which the variable was declared it ceases to exist anyway.

    So, what you can do is set the Name property of the control itself. You have to ensure that it's unique though, or else what's the point? That may also be pointless anyway though. Normally you wouldn't refer to dynamically created controls by name. Normally you'd put them in an array or collection and refer to them by index.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member < Sal >'s Avatar
    Join Date
    Oct 2009
    Location
    Sydney, Australia
    Posts
    6

    Re: Adding controls from Database on formLoad

    jmcilhinney Thanks for that .

    I'll give it another go at work tomorrow, never thought to use an array cause I had my mind set on changing the variable name to the name of the colour brought up from the DB.

    I'll post back and let you all know how I go.


    Cheers.

  5. #5

    Thread Starter
    New Member < Sal >'s Avatar
    Join Date
    Oct 2009
    Location
    Sydney, Australia
    Posts
    6

    Re: Adding controls from Database on formLoad

    Stuck Again,
    Ok the code below does not cause any errors, but is not bringing up the labels on the form:
    Code:
    Dim sSQL As String
            sSQL = "select * from tblLauncher where type ='control' "
            Dim dt As DataTable = DAC.ExecuteQueryAT(sSQL)
            Dim dr As DataRow
            Dim i As Integer = 0
           ' Get all the colours the user wishes to have on app ------
            Dim controls As New ArrayList
            While i < dt.Rows.Count
                dr = dt.Rows(i)
                controls.Add(dr("Name"))
                i = i + 1
            End While
    
            i = 0
            ' Creating the label ----
            Dim VariableLBL As Label() = New Label(controls.Count - 1) {}
            Dim x As Integer = 210
            Dim y As Integer = 2
    
            While i < controls.Count
                VariableLBL(i) = New Label
                VariableLBL(i).Name = "lbl" & controls.Item(i).ToString
                VariableLBL(i).Text = controls.Item(i).ToString
                VariableLBL(i).Location = New System.Drawing.Point(x, y)
                x = x + 30
    ' Adding the label to the form
                Me.Controls.Add(VariableLBL(i))
                i = i + 1
            End While
    Any suggestions why they are not appearing??
    NOTE: it is not the "visible = true" property

  6. #6

    Thread Starter
    New Member < Sal >'s Avatar
    Join Date
    Oct 2009
    Location
    Sydney, Australia
    Posts
    6

    Resolved Re: Adding controls from Database on formLoad

    OK I have Resolved this.

    The controls were being added to the form, I had just mixed up my X & Y Coordinates.
    Code now looks like this for a working Dynamic Array with Dynamic Controls:

    Code:
    ' <<<<  DYNAMIC CONTROL's From a DYNAMIC ARRAY  >>>>
            '' <<< LOAD CONTROL NAMES FROM DB >>> 
            Dim sSQL As String
            sSQL = "select * from Table where type ='control'"
            Dim dt As DataTable = DAC.ExecuteQueryAT(sSQL)
            Dim dr As DataRow
            Dim controlsz As New ArrayList
            Dim i As Integer = 0
            While i < dt.Rows.Count
                dr = dt.Rows(i)
                controlsz.Add(dr("Name"))
                i = i + 1
            End While
    
            '' <<<< CREATE CONTROL ARRAY >>>>
            i = 0
            Dim VariableLBL As Label() = New Label(controlsz.Count - 1) {}
            Dim x As Integer = 2
            Dim y As Integer = 120
            While i < controlsz.Count
                VariableLBL(i) = New System.Windows.Forms.Label()
                VariableLBL(i).Name = "lbl" & controlsz.Item(i).ToString
                VariableLBL(i).Text = controlsz.Item(i).ToString
                VariableLBL(i).Location = New System.Drawing.Point(x, y)
                y = y + 30
                i = i + 1
            End While
    
            '' <<<< ADD CONTROLS FROM ARRAY TO FORM >>>>
            For Each ctl As Control In VariableLBL
                Me.Controls.Add(ctl)
            Next
    If anyone wishes to modify this code due to uneeded code or faster loops feel free to.

    Thank you.
    Coming Soon!
    Coming to a VBForum near you!!
    27/11/2009
    DD/MM/YYYY

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width