如何在 VB.NET 窗体中放置用户控件窗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22564068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to place a user control form in a VB.NET form?
提问by display name
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.
我希望能够在单击按钮时显示用户控件表单。我无法像使用常规表单那样使用 .show() 显示表单。
Question: How to place a user control form in a VB.net form?
问题:如何在VB.net 窗体中放置用户控件窗体?
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
回答by avanek
There are two solutions here. One is to create a new Formin your project and have it host the UserControl. Then you instantiate the Formin your Button6_Clickmethod (the code will be similar to the ColorDialogcode in your Button1_Clickmethod). The other solution is to instantiate a placeholder form directly in the Button6_Clickhandler, like so:
这里有两种解决方案。一种是Form在您的项目中创建一个新的并让它托管UserControl. 然后Form在您的Button6_Click方法中实例化(代码将类似于ColorDialog您的Button1_Click方法中的代码)。另一种解决方案是直接在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
Honestly, the first approach is cleaner, and you will have far more control over how your UserControlgets presented by leveraging the WinForms Designer for the host form.
老实说,第一种方法更简洁,UserControl通过利用宿主窗体的 WinForms 设计器,您可以更好地控制如何呈现。
回答by display name
Try setting visibility to True before calling
在调用之前尝试将可见性设置为 True
UserControl1.Visible = True

