' Collection of code snippets by Arne Vajhøj ' posted to eksperten.dk, usenet and other places (2002-now) Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Threading Namespace DefaultNamespace Public Class MainClass Public Shared Sub Main Dim mf As New MainForm Dim server As TcpListener = New TcpListener(IPAddress.Any, 1234) server.Start() Dim t As Listen = New Listen(server, mf) Call (New Thread(New ThreadStart(AddressOf t.WaitForConnectAndAccept))).Start() mf.ShowDialog() End Sub End Class Public Class Listen Private server As TcpListener Private mainform As MainForm Public Sub New(srv As TcpListener, mf As MainForm) server = srv mainform = mf End Sub Public Sub WaitForConnectAndAccept() MessageBox.Show("started") While True Dim client As TcpClient = server.AcceptTcpClient Dim rdr As StreamReader = New StreamReader(client.GetStream) Dim line As String = rdr.ReadLine mainform.SetLabel2(line) rdr.Close client.Close End While server.Stop() End Sub End Class Public Class MainForm Inherits System.Windows.Forms.Form Private label1 As Label Private button1 As Button Private button2 As Button Private label2 As Label Private button3 As Button Public Sub SetLabel2(txt As String) label2.Text = txt End Sub Public Sub New() MyBase.New Me.InitializeComponent End Sub Private Sub InitializeComponent() SuspendLayout ClientSize = New Size(300, 300) Name = "MainForm" Text = "MainForm" label1 = New Label label1.Location = New Point(50, 50) label1.Size = new Size(200, 25) label1.Name = "label1" label1.TabIndex = 0 label1.Text = "Du har ikke trykket endnu" Controls.Add(label1) button1 = New Button button1.Location = New Point(50, 100) button1.Size = new Size(200, 25) button1.Name = "button1" button1.TabIndex = 1 button1.Text = "A" AddHandler button1.Click, AddressOf ClickA Controls.Add(button1) button2 = New Button button2.Location = New Point(50, 150) button2.Size = new Size(200, 25) button2.Name = "button2" button2.TabIndex = 2 button2.Text = "B" AddHandler button2.Click, AddressOf ClickB Controls.Add(button2) label2 = New Label label2.Location = New Point(50, 200) label2.Size = new Size(200, 25) label2.Name = "label2" label2.TabIndex = 3 label2.Text = "" Controls.Add(label2) button3 = New Button button3.Location = New Point(50, 250) button3.Size = new Size(200, 25) button3.Name = "button3" button3.TabIndex = 4 button3.Text = "Exit" AddHandler button3.Click, AddressOf ClickExit Controls.Add(button3) ResumeLayout(false) End Sub Private Sub ClickA(o As Object, e As EventArgs) label1.Text = "Du har trykket A" End Sub Private Sub ClickB(o As Object, e As EventArgs) label1.Text = "Du har trykket B" End Sub Private Sub ClickExit(o As Object, e As EventArgs) Application.Exit Environment.Exit(0) End Sub End Class End Namespace