Adding Textboxes to another form at runtime

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Hi Everyone -

I have been creating a Windows Form Application (VB.NET) for a while now (hobby only) and thought of an idea that I would like to try to implement, however, I cannot find any info on the web regarding how I want to see if I can set this up.....

I have a database (3 tables) that have a relationship between them (I created the local database through the wizard)
I have added the text boxes and labels by drag and drop from the Data Source tab (which is from the dataset) - Name of my dataset is Database1DataSet.

What I would like to do is on one form (Form2) have check boxes and a label for each column in the database and have the user select which text box they would like to see on Form1 (as below image)

Form2.png

Then when the user goes back to Form1 - they will only see the text boxes (that are linked to the Databset that I dragged and dropped form the Data Source Tab (Like below image)

Form1.png

The layout should be across versus up and down, however, I am not worried about that since it's just for playing around right now.


If anyone has any guidance please let me know.

Thanks in advanced

daveofgv
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
I'd suggest that you're going about this the wrong way. Here's what I would suggest:

1. Design a user control for a Label/TextBox pair with the Label above the TextBox, just as you have for each field there.
2. Add a FlowLayoutPanel to your form.
3. Add an instance of your user control to the FlowLayoutPanel for each field.
4. At run time, simply hide the fields that you don;t want to display and show the others. You can do that by assigning the value of the Checked property of the corresponding CheckBox to the Visible property of each user control.

The FlowLayoutPanel will handle reflowing the displayed fields to fill in any gaps left by those fields that are hidden.
 

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Thank you for the reply..... So (it wont be the exact code) you are saying something similar to:

VB.NET:
If My.Settings.Checkbox1 = True Then
UserControlFirstName.Visible = True
End If

And do this for all the User Controls?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Thank you for the reply..... So (it wont be the exact code) you are saying something similar to:

VB.NET:
If My.Settings.Checkbox1 = True Then
UserControlFirstName.Visible = True
End If

And do this for all the User Controls?
Yes...ish. They are both Booleans and you want one to be the same value as the other so you simply assign one to the other:
Me.firstNameField.Visible = My.Settings.FirstNameVisible
 

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Thank you for the reply... I used the below code and it appears to work, however, if you think it there is "cleaner" code please let me know.
I first added a check box to another form. I created a settings in the application named ckFirstName. I binded the check box to the settings (boolean). I created a User Control named UCFirstName and added it to the flow layout panel on form1. I double clicked on the flow layout panel and changed the event to VisibleChanged - then added the below code within the event:

VB.NET:
   Private Sub FlowLayoutPanel1_VisibleChanged(sender As Object, e As System.EventArgs) Handles FlowLayoutPanel1.VisibleChanged
        If My.Settings.ckFirstName = True Then

            Me.UcFirstName1.Visible = My.Settings.ckFirstName
        Else
            UcFirstName1.Visible = False

        End If
    End Sub
Please let me know if this is the best way to do this since I will have to create a lot of user controls since there is a lot of columns in my database. ( a lot of If...Then Statements on the VisibleChanged event (maybe 50 - 100).

Also, I am having an problem linking the text boxes on each user control to the database.... any suggestions on this?

Thanks in advanced.

**** Edited Part ****

I see on your code... I do not have to use a IF...THEN statement. All I need is
VB.NET:
 Me.UcFirstName1.Visible = My.Settings.ckFirstName

:D:stung:

Thanks for that part...
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
You can bind your user control the same way you would any other control, e.g. a TextBox. You simply add properties to your user control to pass through the properties of the internal child controls that you want to access. For instance, you should have, at the very least, a property for the Text of the Label and a property for the Text of the TextBox:
Imports System.ComponentModel

Public Class UserControl1

    <Bindable(True)>
    Public Property LabelText() As String
        Get
            Return Me.Label1.Text
        End Get
        Set(value As String)
            Me.Label1.Text = value
        End Set
    End Property

    <Bindable(True)>
    Public Property TextBoxText() As String
        Get
            Return Me.TextBox1.Text
        End Get
        Set(value As String)
            Me.TextBox1.Text = value
        End Set
    End Property

End Class
 
Top Bottom