这是我想在我的项目中实现的目标:
当用户按下按钮时,会弹出一个带有文本框、单选按钮和选择菜单的表单,允许用户输入他们的信息,
点击确定,表单数据将传递到主程序表单中的某个文本框点击取消,子表单就消失了
这在VB中可能吗?
我试过提示和输入框,但有限制
我是VB的新手,如果我没有把问题说清楚,很抱歉
一种更简单的方法是声明一个您将使用的形式的变量,然后调用 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
在 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
我使用了这个示例并稍微清理了代码: http ://www.dreamincode.net/forums/topic/103846-custom-inputbox/
我不知道如何实现您的单选按钮和选择菜单,但您可能会弄清楚,因为困难的部分是从弹出表单中获取文本以传递回调用表单,并且在这里为您完成。让我知道你是否需要这部分的帮助。也许您可以提供 UI 的屏幕截图?
因此,从您的呼叫表单中,单击按钮以显示提示:
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
这是您粘贴到自定义提示表单中的代码:
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
只需创建自己的表单并将其显示.ShowDialog()
为模态,例如 InputBox。
然后,在控件中输入数据时,在关闭表单之前,将数据传递到主表单,以免丢失。