|
-
Aug 23rd, 2007, 10:11 AM
#1
Thread Starter
Fanatic Member
[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..."
-
Aug 23rd, 2007, 10:22 AM
#2
Thread Starter
Fanatic Member
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..."
-
Aug 23rd, 2007, 10:28 AM
#3
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
-
Aug 23rd, 2007, 10:36 AM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|