开发者

How to use my class within another class in VB.NET

Yes, this is a really bad question, probably one th开发者_运维技巧at 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


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.


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


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜