Results 1 to 4 of 4

Thread: [RESOLVED] [02/03] Is there anything inherently wrong with this?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Resolved [RESOLVED] [02/03] Is there anything inherently wrong with this?

    Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    If e.KeyData = Keys.Enter Then
    Me.TextBox1.Text = DirectCast(sender, TextBox).Text
    DirectCast(DirectCast(sender, TextBox).Parent, Form).Close()
    End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f As New Form2

    AddHandler f.TextBox1.KeyDown, AddressOf TextBox_KeyDown

    f.Show()
    End Sub
    End Class
    So, I have two forms, each with a textbox. Form1 has a button which launches Form2. Form1 adds an event handler to the textbox in Form2 so that when you press Enter, it grabs the text and closes Form2. Is this OK to do? I mean, in my little test, it works. But, is this really bad practice?

    Also, I cannot use ShowDialog().
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [02/03] Is there anything inherently wrong with this?

    Kleinma,
    I believe I got some code that you posted once that allowed one to change the background of one form from another form (from red to blue, I think). If I remember correctly, it did something similar to what I am doing here. Was that you? I thought I saved the project, but I can't seem to find it.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [02/03] Is there anything inherently wrong with this?

    I don't really remember to be honest, however I likely would not have done it by adding an event handler to form2 from form1....

    what you could do is declare the Form2 variable in Form1 at the form level (instead of proceedure level) which will allow you to use the WithEvents keyword, and receive events from Form2 in Form1 automatically..

    In form1 you could have code:

    Code:
        Private WithEvents _Form2 As Form2 = Nothing
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If _Form2 Is Nothing Then
                _Form2 = New Form2
            End If
            _Form2.Show()
        End Sub
    
        Private Sub _Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Form2.Closed
            MessageBox.Show(_Form2.TextBox1.Text)
            'CLEAN UP THE FORM
            _Form2.Dispose()
            _Form2 = Nothing
        End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [02/03] Is there anything inherently wrong with this?

    Oh, yeah. That would work as well. That way seems safer, too. Thanks.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width