如何在VB中“提示”用户输入的子表单?

时间:2013-06-21 02:37:49

标签: vb.net forms prompt subform

以下是我想在项目中实现的目标:

当用户按下按钮时,弹出一个带有文本框,单选按钮和选择菜单的表单,允许用户输入他们的信息,

单击“确定”,表单数据将传递到主程序表单中的某个文本框 点击取消,子表格就消失了

这可能在VB吗?

我尝试过提示和输入框,但是有限的

我是VB的新手,很抱歉,如果我没有明确提出问题

3 个答案:

答案 0 :(得分:3)

更简单的方法是声明您将使用的表单的变量,然后调用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

答案 1 :(得分:1)

我使用了这个示例并清理了一下代码: 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

答案 2 :(得分:0)

只需创建自己的表单并将其与.ShowDialog()一起显示为模态,如InputBox。

然后,当在控件中输入数据时,在关闭表单之前,将数据传递给主表单,以便不会丢失。