I've got the basis of the program working (I'm doing a lot of experimentation here as this is the first real program I'm writing) And I had everything working perfectly with simple button click events. But, my issue is I need to have a tcplisten running in the background.
I have found out through a little bit of research that I need to use threads. All good.
The listen thread works as required. Holds the port open, receives the data, passes that onto my texbox creator. But I can't get the textbox to actually go onto the flowlayoutpanel.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tcpthread = New System.Threading.Thread(AddressOf tcplisten.MyTcpListener.Main)
tcpthread.Start()
txtthread = New System.Threading.Thread(AddressOf txtboxcreate)
txtthread.Start()
textboxestime = New Dictionary(Of TextBox, Integer)
textboxestime.Add(Nothing, 0)
End Sub
Public Class tcplisten
Public Shared datastring As String
Public Shared received As Boolean
Class MyTcpListener
Public Shared Sub Main()
Dim lf As Decimal = 23
Dim server As TcpListener
Dim datastring As String = Nothing
server = Nothing
Try
' Set the TcpListener on port 13000.
'Dim port As Int32 = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Iain-KVS", "p1", Nothing)
' Dim ipstring As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Iain-KVS", "IP", Nothing)
Dim port As Int32 = 13000
Dim localaddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localaddr, port)
' Start listening for client requests.
server.Start()
' server.AcceptSocket()
' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing
' Enter the listening loop.
'While True
' MsgBox("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
'While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
'MsgBox(data)
' Process the data sent by the client.
'data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
' MsgBox("Sent: {0}", data)
' i = stream.Read(bytes, 0, bytes.Length)
tcplisten.datastring = (data)
client.Close()
received = True
Form1.txtboxcreate()
' End While
' Shutdown and end connection
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
' server.Stop()
End Try
End Sub 'Main
End Class 'MyTcpListener
End Class
Public Sub txtboxcreate()
Dim check As Boolean
check = tcplisten.received
If check = True Then
Dim name As String
Dim text As String
Dim counttotal As Integer
Dim count As Integer
Dim tb As New TextBox()
text = tcplisten.datastring
name = ("TxtBox" & CStr(counttotal))
With tb
.Name = name
.Text = text
.Multiline = True
.Height = 150
.Width = 250
.ReadOnly = True
.ScrollBars = False
.MaxLength = 100
.Tag = 20
.BackColor = Color.Black
.ForeColor = Color.White
Me.FlowLayoutPanel1.Controls.Add(tb)
Form1.textboxestime.Add(tb, 0)
AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
End With
count = (count + counttotal)
End If
End Sub
I can see it being passed from the tcplistener through to txtboxcreate without an issue, but it just does not seem to be adding the textbox to the flowlayoutpanel.