Change properites/read information of progrmmatically created items

Posted on 2007-10-10
Last Modified: 2010-04-23
There is probably a simple answer to this but...

I programmatically created text boxes with a loop from a database.  Now I need to be able to click a button(that I also created progrmmatically) and it will turn those text boxes read only value from true to false, I also need it to pop up information about the current text in those text fields.  Below is the link that discusses how I created the text boxes and buttons.

I need to be able to do this at a later time then they are created.

Question by:higginsonline
    LVL 96

    Expert Comment

    by:Bob Learned
    If you are dynamically creating TextBox controls, and then you later want to reference those controls from a button click event handler, then you need to store the reference in some kind of list structure.

    LVL 62

    Accepted Solution

    Hi higginsonline;

    This will do what you need.

        Private Sub DisableTextBoxes_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles DisableTextBoxes.Click

            For Each obj As Object In htTextBox.Values
                Dim tb As TextBox = CType(obj, TextBox)
                tb.ReadOnly = True

        End Sub


    Author Comment


    The only problem with that is I create 3 different text boxes per row
      tbName   tbPrice  tbGLAcct
    I want tbName tbPrice and tbGLAcct read only to = false
    Not every instance of tbName.

    So what would I change the obj to?
    LVL 62

    Expert Comment

    by:Fernando Soto
    If I understand you correctly the button that you created on each row should make the three text boxes on that same row read only when it is clicked?

    If that is the case then you need a way to identify the three text boxes on the same row as the button. How do you set the Name property of the TextBox's on each row?

    Author Comment

    Private htTextBoxName As New Hashtable
        Private htTextBoxPrice As New Hashtable
        Private htTextBoxGLacct As New Hashtable
        Private htCheckBoxOver As New Hashtable

            OledbRead = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While OledbRead.Read()
                Dim idx As Integer = OledbRead.Item("itemID")
                Dim tbName As New TextBox
                tbName.Name = "plName" & idx.ToString()
                tbName.Location = New Point(X, Y)
                tbName.Text = OledbRead.Item("ItemName")
                tbName.Font = verdana
                tbName.BorderStyle = BorderStyle.FixedSingle
                tbName.Width = 120
                tbName.ReadOnly = True

                Dim tbPrice As New TextBox
                tbPrice.Name = "plPrice" & idx.ToString()
                tbPrice.Location = New Point(Xprice, Y)
                tbPrice.Text = OledbRead.Item("BasePrice")
                tbPrice.Font = verdana
                tbPrice.BorderStyle = BorderStyle.FixedSingle
                tbPrice.Width = 75
                tbPrice.ReadOnly = True
                tbPrice.TextAlign = HorizontalAlignment.Right

                Dim tbGLacct As New TextBox
                tbGLacct.Name = "plGLacct" & idx.ToString()
                tbGLacct.Location = New Point(Xglacct, Y)
                tbGLacct.Text = OledbRead.Item("GLacct")
                tbGLacct.Font = verdana
                tbGLacct.BorderStyle = BorderStyle.FixedSingle
                tbGLacct.Width = 75
                tbGLacct.ReadOnly = True
                tbGLacct.TextAlign = HorizontalAlignment.Right

                Dim cbOver As New CheckBox
                cbOver.Name = "plOverRide" & idx.ToString()
                cbOver.Location = New Point(Xover, Y)
                cbOver.Width = 15
                cbOver.Font = verdana
                If OledbRead.Item("Override") = True Then cbOver.Checked = True
                cbOver.Enabled = False

                Dim ebtn As New Button()
                ebtn.Text = "Edit"
                ebtn.Name = "Row" & row.ToString()
                ebtn.Location = New Point(Xbtn, Y)
                ' I am using the Tag property to identify the row clicked on
                ebtn.Tag = idx.ToString
                ebtn.Font = verdana
                ebtn.Width = 75
                ' Because you want to be able to click on the button and have an event fired
                ' you need to have written a subroutine to handle that in advance. This wires
                ' the Click event on the button to that subroutine
                AddHandler ebtn.Click, AddressOf Row_Click

                Dim dbtn As New Button()
                dbtn.Text = "Delete"
                dbtn.Name = "dRow" & row.ToString()
                dbtn.Location = New Point(Xbtn1, Y)
                ' I am using the Tag property to identify the row clicked on
                dbtn.Tag = idx.ToString
                dbtn.Font = verdana
                dbtn.Width = 75
                ' Because you want to be able to click on the button and have an event fired
                ' you need to have written a subroutine to handle that in advance. This wires
                ' the Click event on the button to that subroutine
                AddHandler dbtn.Click, AddressOf Row_Click2

                Y += 30
                htCheckBoxOver.Add(cbOver.Name, cbOver)
                htTextBoxGLacct.Add(tbGLacct.Name, tbGLacct)
                htTextBoxPrice.Add(tbPrice.Name, tbPrice)
                htTextBoxName.Add(tbName.Name, tbName)
            End While
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi higginsonline;

    This code when placed in the click event of the button that you click will make the three TextBox's in that row ReadOnly

            Dim btn As Button = CType(sender, Button)
            Dim row As String = btn.Tag.ToString()

            CType(htTextBoxName("plName" & row), TextBox).ReadOnly = True
            CType(htTextBoxPrice("plPrice" & row), TextBox).ReadOnly = True
            CType(htTextBoxGLacct("plGLacct" & row), TextBox).ReadOnly = True


    Author Comment

    Once again you are the man, thank you
    LVL 62

    Expert Comment

    by:Fernando Soto
    Not a problem, glad I was of some help. ;=)

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now