user3376708
user3376708

Reputation:

How to place a user control form in a VB.NET form?

I want to be able to show a user control form at the click of a button. I have not been able to show the form with the .show() as I would be able to with a regular form.

Question: How to place a user control form in a VB.net form?

I should rephrase the question. how do I show a user control form in a form. I was not completely certain of this worked when I was building my form.

Code:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim MyDialog As New ColorDialog()
        ' Keeps the user from selecting a custom color.
        MyDialog.AllowFullOpen = True
        ' Allows the user to get help. (The default is false.)
        MyDialog.ShowHelp = True
        ' Sets the initial color select to the current text color,
        MyDialog.Color = TextBox1.ForeColor

        ' Update the text box color if the user clicks OK  
        If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            TextBox1.ForeColor = MyDialog.Color
        End If
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        Dim myCoolFile As String = "C:\Users\itpr13266\Desktop\test.txt" '// your file location.
        Dim tempStr As String = IO.File.ReadAllText(myCoolFile) '// read file into a String.
        Dim x As Integer = tempStr.Length - 1 '// get String length, Index based, starting at 0 not 1.
        Dim myArray(x) As String '// create your String Arrays.
        Dim i As Integer = 0 '// Integer used to increase Array #'s.

        For Each myChr As Char In tempStr '// loop thru each each character in the String.
            myArray(i) = myChr '// add values to each String Array.
            i += 1 '// increase count for next Array.
        Next

        For number As Integer = 1 To myArray.Length - 1 Step 1 'loops through the array by 1 and to 1 less the length
            Debug.Write(myArray(number)) 'write the value in the array to the debug window
        Next
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        UserControl1.Show()
    End Sub
End Class

Upvotes: 0

Views: 9404

Answers (2)

avanek
avanek

Reputation: 1659

There are two solutions here. One is to create a new Form in your project and have it host the UserControl. Then you instantiate the Form in your Button6_Click method (the code will be similar to the ColorDialog code in your Button1_Click method). The other solution is to instantiate a placeholder form directly in the Button6_Click handler, like so:

Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
    Using UserControl1 As FooUserControl = New FooUserControl()
        Using tmpForm As Form = New Form()
            tmpForm.Width = UserControl1.Width
            tmpForm.Height = UserControl1.Height
            tmpForm.Controls.Add(UserControl1)
            tmpForm.ShowDialog()
        End Using
    End Using
End Sub

Honestly, the first approach is cleaner, and you will have far more control over how your UserControl gets presented by leveraging the WinForms Designer for the host form.

Upvotes: 2

display name
display name

Reputation: 4185

Try setting visibility to True before calling

UserControl1.Visible = True

Upvotes: 1

Related Questions