如何在 VB.NET 中的另一个类中使用我的类

发布于 2024-11-19 05:58:43 字数 1889 浏览 2 评论 0原文

是的,这是一个非常糟糕的问题,可能任何一个像样的 VB.NET 程序员都应该知道这个问题。嗯,我是 VB.NET 新手,所以我需要一点帮助。

我已经创建了一个 Windows 窗体类和另一个窗体类,其代码是这样的:

Imports System.Windows.Forms

Public Class Form2

    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.PromptLabel.Text = PromptText
        Me.Text = TitleText
        Me.Txt_TextEntry.Text = DefaultText
        Me.ShowDialog()
        EnteredText = m_ReturnText
        Return Me.DialogResult
    End Function

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.Txt_TextEntry.Text = "" Then
            Me.OK_Btn.Enabled = m_BlankValid
        Else
            Me.OK_Btn.Enabled = True
        End If
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        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)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        m_ReturnText = ""
        Me.Close()
    End Sub
End Class

显然,我的设计中有一些图形。

要调用我的类,我想使用以下代码:

Dim TextReturned As String = ""
Dim a As New InputBox
If a.ShowDialog("The Title", "The Prompt", "Default", TextReturned, False) = Windows.Forms.DialogResult.Cancel Then
    ' Cancel Pressed
    Beep()
Else
    '
End If

我的错误是类型“InputBox”未定义。我怎样才能让它被定义呢?

谢谢,

奥丁努尔夫

Yes, this is a really bad question, probably one that any half decent VB.NET programmer should know. Well, I am new to VB.NET, so I need a little help.

I have created a windows forms class along side another one, and its code is this:

Imports System.Windows.Forms

Public Class Form2

    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.PromptLabel.Text = PromptText
        Me.Text = TitleText
        Me.Txt_TextEntry.Text = DefaultText
        Me.ShowDialog()
        EnteredText = m_ReturnText
        Return Me.DialogResult
    End Function

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.Txt_TextEntry.Text = "" Then
            Me.OK_Btn.Enabled = m_BlankValid
        Else
            Me.OK_Btn.Enabled = True
        End If
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        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)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        m_ReturnText = ""
        Me.Close()
    End Sub
End Class

Obviously, I have some graphics on my Design.

To call my class, I would like to use the following code:

Dim TextReturned As String = ""
Dim a As New InputBox
If a.ShowDialog("The Title", "The Prompt", "Default", TextReturned, False) = Windows.Forms.DialogResult.Cancel Then
    ' Cancel Pressed
    Beep()
Else
    '
End If

My error is that Type 'InputBox' is not defined. How can I make it such that it is defined?

Thanks,

Odinulf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪冰清 2024-11-26 05:58:43

假设上面的代码是从实际代码复制/粘贴的,您的类名为 Form2 而不是 InputBox。将自定义表单上的类声明更改为 Public Class InputBox

如果上面的代码片段只是有一个拼写错误,那可能会改变一些事情。

Assuming your code above is copy/pasted from your actual code, your class is named Form2 not InputBox. Change the class declaration on your custom form to Public Class InputBox

If your the snippets above just have a typo, that may change things, though.

江城子 2024-11-26 05:58:43

您的代码为:

Dim a As New InputBox

但该类定义为:

Public Class Form2

这意味着您的代码应为:

Dim as New Form2()< /code>

注意:或者你可以将你的类重命名为:
公共类Form2

公共类InputBox

Your code reads:

Dim a As New InputBox

but the class is defined as:

Public Class Form2

Meaning that your code should read:

Dim as New Form2()

note: alternatively you could rename your class as:
Public Class Form2

to

Public Class InputBox

苍白女子 2024-11-26 05:58:43

好吧,根据您发布的代码,您的“InputBox”被称为Form2(类名)。将其更改为InputBox,它应该可以工作。

Well, according to the code you posted, your "InputBox" is called Form2 (class name). Change that to InputBox and it should work.

时光瘦了 2024-11-26 05:58:43

InputBox 不是类型或类,而是方法。例如...

InputBox("The Title", "Theprompt") '以及您需要的任何其他参数

InputBox is not a type or class, it's a method. For example...

InputBox("The Title", "The prompt") 'and any other parameters you need

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文