Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27356

VS 2010 How to reset a form when .hide? and shown again. (Fresh like a new launched form)

$
0
0
I have a question how can I reset a form when it will be hide? like its thread started and etc. I just want my form back to normal just like a fresh new launched form when .show again after .hide. Is there a command for that? I am having problem terminating my project. I am currently playing with a Desktop Viewer code I found over the internet . I want to figure out how to terminate if the client will disconnect or the server will disconnect then once the disconnected form will return, it will continue the stream or can resume the stream. I am getting cannot connect error when doing it after closing and reopening my client.

Here is my code

SERVER
Server Code:
  1. Imports System.IO
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Threading
  5. Imports System.Drawing
  6. Imports System.Runtime.Serialization.Formatters.Binary
  7.  
  8. Public Class Server
  9.     Dim client As New TcpClient
  10.     Dim server As New TcpListener(8085)
  11.     Dim nstream As NetworkStream
  12.     Dim listening As New Thread(AddressOf Listen)
  13.     Dim getImage As New Thread(AddressOf receiveImage)
  14.     Private Sub receiveImage()
  15.         Try
  16.             Dim bf As New BinaryFormatter
  17.             While client.Connected = True
  18.                 nstream = client.GetStream
  19.                 PictureBox1.Image = bf.Deserialize(nstream)
  20.             End While
  21.         Catch ex As Exception
  22.             CheckForIllegalCrossThreadCalls = False
  23.  
  24.             Button1.Text = "Listen"
  25.  
  26.         End Try
  27.     End Sub
  28.  
  29.     Private Sub Listen()
  30.         While client.Connected = False
  31.             server.Start()
  32.             client = server.AcceptTcpClient
  33.         End While
  34.         getImage.Start()
  35.     End Sub
  36.     Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
  37.  
  38.     End Sub
  39.  
  40.     Private Sub Server_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  41.  
  42.     End Sub
  43.  
  44.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  45.         If Button1.Text = "Listen" Then
  46.             Try
  47.                 listening.Start()
  48.             Catch ex As Exception
  49.                 If ex.Message.Contains("Thread is running or terminated; it cannot restart.") Then
  50.                     server.Start()
  51.  
  52.  
  53.                 End If
  54.             End Try
  55.  
  56.             Button1.Text = "Stop Listening"
  57.  
  58.         Else
  59.             client.Close()
  60.             Button1.Text = "Listen"
  61.         End If
  62.  
  63.  
  64.     End Sub
  65.  
  66.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  67.         Clientform.Show()
  68.  
  69.     End Sub
  70. End Class


CLIENT
CLIENT Code:
  1. Imports System.IO
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Threading
  5. Imports System.Drawing
  6. Imports System.Runtime.Serialization.Formatters.Binary
  7. Public Class Clientform
  8.     Dim client As New TcpClient
  9.     Dim nstream As NetworkStream
  10.  
  11.     Private Sub Clientform_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
  12.         Timer1.Enabled = False
  13.         Button2.Text = "Stream"
  14.  
  15.     End Sub
  16.  
  17.     Private Sub Clientform_Leave(sender As Object, e As EventArgs) Handles Me.Leave
  18.  
  19.     End Sub
  20.     Private Sub Client_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  21.  
  22.     End Sub
  23.  
  24.     Public Function Desktop() As Image
  25.         Dim bounds As Rectangle = Nothing
  26.         Dim screenshot As System.Drawing.Bitmap = Nothing
  27.         Dim graph As Graphics = Nothing
  28.         bounds = Screen.PrimaryScreen.Bounds
  29.         screenshot = New Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  30.         graph = Graphics.FromImage(screenshot)
  31.         graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
  32.         Return screenshot
  33.     End Function
  34.  
  35.     Private Sub SendImage()
  36.         Try
  37.  
  38.        
  39.         Dim bf As New BinaryFormatter
  40.         nstream = client.GetStream
  41.             bf.Serialize(nstream, Desktop())
  42.         Catch ex As Exception
  43.             Timer1.Enabled = False
  44.             client.Close()
  45.  
  46.         End Try
  47.     End Sub
  48.  
  49.  
  50.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  51.         SendImage()
  52.     End Sub
  53.  
  54.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  55.         Try
  56.             If Button1.Text = "Connect" Then
  57.                 client.Connect("127.0.0.1", 8085)
  58.                 Button1.Text = ("Disconnect")
  59.             Else
  60.                 client.Close()
  61.                 Button1.Text = ("Connect")
  62.             End If
  63.  
  64.         Catch ex As Exception
  65.             MsgBox("Failed to connect...")
  66.         End Try
  67.     End Sub
  68.  
  69.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  70.         If Button2.Text = "Stream" Then
  71.             Timer1.Enabled = True
  72.             Button2.Text = "Stop Stream"
  73.         Else
  74.             Timer1.Enabled = False
  75.             Button2.Text = "Stream"
  76.  
  77.         End If
  78.  
  79.     End Sub
  80.  
  81.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  82.         Server.Show()
  83.     End Sub
  84.  
  85.     Private Sub Button2_ControlRemoved(sender As Object, e As ControlEventArgs) Handles Button2.ControlRemoved
  86.  
  87.     End Sub
  88. End Class

Viewing all articles
Browse latest Browse all 27356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>