Question Call Private Subs

Nasedo

New member
Joined
Oct 19, 2009
Messages
2
Location
Cheshire, United Kingdom
Programming Experience
3-5
Hay, I have made a network chatting program within Visual Basic 6 and i'm currenly re-writing it in Visual Basic .Net 2008. Im having a problem that i wonder if you could help me with.

When i click the "btnSend" the code below works everytime.

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

'ADDING DATA TO THE STRINGS
MsgStr = txtMessage.Text & vbCrLf & txtSupportRoom.Text

'OPENS THE SUPPORT ROOM FILE (IF THE FILE CANT BE FOUND IT WILL CREATE IT)
FileOpen(1, SupportRoom, OpenMode.Output)
'WRITES THE DATA WITHIN THE STRING TO THE SUPPORT ROOM FILE
Print(1, MsgStr)
'CLOSES THE SUPPORT ROOM FILE
FileClose(1)

'CLEARS THE MESSAGE TEXTBOX
txtMessage.Text = ""
txtMessage.Focus()
End Sub

However if i call the code using a keypress function it works only once and fails to do so again. (keypress code below)

Private Sub txtMessage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMessage.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
Call btnSend_Click(Nothing, Nothing)
End If
End Sub

Could anyone explain why this is? and maybe offer a solution.

Thanks

Nasedo.
 
This is probably is more of a preference comment, but I think it would be easier to read/write if you created another private sub.

VB.NET:
private sub DoStuff()
'ADDING DATA TO THE STRINGS
MsgStr = txtMessage.Text & vbCrLf & txtSupportRoom.Text

'OPENS THE SUPPORT ROOM FILE (IF THE FILE CANT BE FOUND IT WILL CREATE IT)
FileOpen(1, SupportRoom, OpenMode.Output)
'WRITES THE DATA WITHIN THE STRING TO THE SUPPORT ROOM FILE
Print(1, MsgStr)
'CLOSES THE SUPPORT ROOM FILE
FileClose(1)

'CLEARS THE MESSAGE TEXTBOX
txtMessage.Text = ""
txtMessage.Focus()
End Sub


Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
DoStuff()
End Sub


Private Sub txtMessage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMessage.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
DoStuff()
End If
End Sub
 
Back
Top