vb.net 如何在VB中“提示”用户输入的子表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17226904/
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 "prompt" a subform for user input in VB?
提问by user1452954
Here is what i want to achieve in my project :
这是我想在我的项目中实现的目标:
When a user press a button, a form pop out with textbox, radio button and select menu on it, allowing the user to input their inform,
当用户按下按钮时,会弹出一个带有文本框、单选按钮和选择菜单的表单,允许用户输入他们的通知,
Click OK, the form data will be passed to some textbox in the main program form click cancel, the subform just go away
单击确定,表单数据将传递到主程序表单中的某个文本框单击取消,子表单就消失了
Is this possible in VB ?
这在 VB 中可能吗?
I tried prompt and inputbox, but there are limited
我试过提示和输入框,但有限制
I'm new to VB, so sorry if I didn't make the question clear
我是 VB 新手,很抱歉,如果我没有把问题说清楚
回答by tinstaafl
A simpler way is declare a variable of the form you'll use then call the showdialog. Now you can access all the control properties that are on the form. Because you're using an object of form2 closing the form doesn't dispose of the object so everything is available.
一种更简单的方法是声明一个您将使用的形式的变量,然后调用 showdialog。现在您可以访问窗体上的所有控件属性。因为您使用的是 form2 的对象,所以关闭表单不会处理该对象,所以一切都可用。
Dim NewForm2 As New Form2
Dim Result As DialogResult = NewForm2.ShowDialog
If Result = Windows.Forms.DialogResult.OK Then
MsgBox(Newform2.TextBox1.Text)
End If
In Form2 add this:
在 Form2 中添加:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
回答by PJSimon
I used this sample and cleaned up the code a bit: http://www.dreamincode.net/forums/topic/103846-custom-inputbox/
我使用了这个示例并对代码进行了一些清理:http: //www.dreamincode.net/forums/topic/103846-custom-inputbox/
I didn't know how to implement your radio button and select menu, but you can probably figure it out because the hard part is getting the text from the popup form to be passed back to the calling form and that is done for you here. Let me know if you do need help with that part. Maybe you could provide a screen shot of the UI?
我不知道如何实现您的单选按钮和选择菜单,但您可能可以弄清楚,因为困难的部分是将弹出窗体中的文本传递回调用窗体,而这里已为您完成。如果您确实需要这部分的帮助,请告诉我。也许您可以提供 UI 的屏幕截图?
So, from your calling form, click the button to show the prompt:
因此,从您的呼叫表单中,单击按钮以显示提示:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim result = frmPrompt.Show("The Title", "The Prompt", "Default", TextBox1.Text, False)
End Sub
And this is the code you paste into your custom prompt form:
这是您粘贴到自定义提示表单中的代码:
Imports System.Windows.Forms
Public Class frmPrompt
Protected m_BlankValid As Boolean = True
Protected m_ReturnText As String = ""
Public Overloads Function ShowDialog( _
ByVal TitleText As String, _
ByVal PromptText As String, _
ByVal DefaultText As String, _
ByRef EnteredText As String, _
ByVal BlankValid As Boolean) As System.Windows.Forms.DialogResult
m_BlankValid = BlankValid
Me.Lbl_Prompt.Text = PromptText
Me.Text = TitleText
Me.Txt_TextEntry.Text = DefaultText
Me.ShowDialog()
EnteredText = m_ReturnText
Return Me.DialogResult
End Function
Public Overloads Shared Function Show(ByVal TitleText As String, ByVal promptText As String, ByVal DefaultText As String, ByRef TextInputted As String, Optional ByVal IsEmptyValid As Boolean = True) As System.Windows.Forms.DialogResult
Dim tmp As New frmPrompt
Return tmp.ShowDialog(TitleText, promptText, DefaultText, TextInputted, IsEmptyValid)
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_TextEntry.TextChanged
If Me.Txt_TextEntry.Text = "" Then
Me.But_Ok.Enabled = m_BlankValid
Else
Me.But_Ok.Enabled = True
End If
End Sub
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Ok.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
m_ReturnText = Me.Txt_TextEntry.Text
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Cancel.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
m_ReturnText = ""
Me.Close()
End Sub
End Class
回答by SysDragon
Just create your own form and show it with .ShowDialog()to be modal, like InputBox.
只需创建您自己的表单并将其显示.ShowDialog()为模态,例如 InputBox。
Then, when the data is entered in the controls, before closing the form, pass the data to your main form so it is not losed.
然后,在控件中输入数据时,在关闭窗体之前,将数据传递到主窗体,以免丢失。

