[英]How to place a user control form in a VB.NET form?
我希望能夠通過單擊一個按鈕來顯示用戶控制表單。 我無法使用.show()顯示該表單,就像使用常規表單一樣。
問題:如何將用戶控件表單放置在VB.net表單中?
我應該改一下這個問題。 如何在表單中顯示用戶控件表單。 當我建立表格時,我並不完全確定這項工作是否可行。
碼:
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
這里有兩個解決方案。 一種方法是在您的項目中創建一個新Form
,並使其托管UserControl
。 然后,在Button6_Click
方法中實例化Form
(該代碼類似於Button1_Click
方法中的ColorDialog
代碼)。 另一個解決方案是直接在Button6_Click
處理程序中實例化一個占位符表單,如下所示:
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
坦率地說,第一種方法是更清潔的方法,通過使用WinForms Designer作為宿主窗體,您將對如何呈現UserControl
擁有更多的控制權。
嘗試在致電之前將可見性設置為True
UserControl1.Visible = True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.