Hey, I'm new here.. just having a problem with a program I'm creating to talk to devices connected over the network. The program has to be able to send strings out of a specified ip address and port number...and it needs buttons which are customised by the user to set what they want them to do.
So basically, I used some code from the Microsoft visual basic site and tweaked it about to make it do what I wanted. It was working perfectly fine when the variables message, port and server were being defined in the button onclick subroutine.. but now I need the variables undefined so that the users input can set the values and also I need them to be public so I can use them in other forms....this is where it stopped working.
My current code:
The main form with the button that is to be customised:
And the button customisation screen:Code:Public Class frmMain Public message As String 'Defines message as a string Public server As String 'Defines server as a string Public port As Int32 Private Sub cmdButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton1.Click Try 'Dim message As String = "Message" 'Defines message as a string 'Dim server As String = "192.168.95.231" 'Defines server as a string 'Dim port As Int32 = "10001" Dim client As New Net.Sockets.TcpClient(server, port) 'Defining variable client as a tcp client Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message(0)) 'Changes message into ASCII to store as a byte array, data. Dim stream As Net.Sockets.NetworkStream = client.GetStream() 'Defines stream as a Network stream MsgBox(message) stream.Write(data, 0, data.Length) 'Sending the data into the stream 'Console.WriteLine("Sent: {0}", message) 'Displays message that was sent ' Receive the TcpServer.response. ' Buffer to store the response bytes. data = New [Byte](256) {} ' String to store the response ASCII representation. 'Dim responseData As [String] = [String].Empty ' Read the first batch of the TcpServer response bytes. 'Dim bytes As Int32 = stream.Read(data, 0, data.Length) 'responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) 'Console.WriteLine("Received: {0}", responseData) 'If responseData = "%1POWR OK" & vbCr Then 'lblStatus.Text = "Successful" 'ElseIf responseData = "%1POWR=ERR1" & vbCr Then 'lblStatus.Text = "Out of parameter" 'End If ' Close everything. stream.Close() client.Close() Catch f As ArgumentNullException Console.WriteLine("ArgumentNullException: {0}", f) Catch f As System.Net.Sockets.SocketException Console.WriteLine("SocketException: {0}", f) End Try MsgBox(server) MsgBox(port) 'Console.WriteLine(ControlChars.Cr + " Press Enter to continue...") ' Console.Read() End Sub Private Sub cmdCustomise_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustomise.Click Me.Visible = False frmCustomise.Visible = True End Sub End Class
No doubt I have other errors, but I'd like to try and sort out sending the strings right now so any help would be greatCode:Public Class frmCustomise Public message As String 'Defines message as a string Public server As String 'Defines server as a string Public port As Int32 Private Sub cmdSetButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetButton1.Click Dim MyVariable As String If radType.Checked = True Then frmMain.message = (comB11.Text + comB12.Text + ComB13.Text + comB14.Text + comB15.Text + comB16.Text + comB17.Text + comB18.Text + comB19.Text) frmMain.cmdButton1.Text = txtCommandTitle.Text Else MyVariable = comButton1.Text If MyVariable = "On" Then frmMain.cmdButton1.Text = MyVariable frmMain.message = "%1POWR 1" & vbCr Else frmMain.cmdButton1.Text = MyVariable frmMain.message = "%1POWR 0" & vbCr End If End If If txtPort.Text = "" Then MsgBox("Please enter a port number") ElseIf txtServer.Text = "" Then MsgBox("Please enter an IP address") Else frmMain.port = txtPort.Text frmMain.server = txtServer.Text End If Me.Visible = False frmMain.Visible = True 'MsgBox(frmMain.message(0)) 'MsgBox(frmMain.port) 'MsgBox(frmMain.server) End Sub Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radType.CheckedChanged comB11.Enabled = True comB12.Enabled = True ComB13.Enabled = True comB14.Enabled = True comB15.Enabled = True comB16.Enabled = True comB17.Enabled = True comB18.Enabled = True comB19.Enabled = True txtCommandtitle.Enabled = True lblType.Enabled = True lblTitle.Enabled = True comButton1.Enabled = False End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radChoose.CheckedChanged comB11.Enabled = False comB12.Enabled = False ComB13.Enabled = False comB14.Enabled = False comB15.Enabled = False comB16.Enabled = False comB17.Enabled = False comB18.Enabled = False comB19.Enabled = False txtCommandtitle.Enabled = False lblType.Enabled = False lblTitle.Enabled = False comButton1.Enabled = True End Sub
Thanks
Emily





Reply With Quote