[2005] Showing another form
I have a loading form that i want to show when i'm reading something from the serial port. My only problem is that i'm not 100% sure what's with the new stuff like delegates, but i've got it to work with a MyDelegate sub. But to invoke on the loading form, it throws an exception that the window needs a handle before you can invoke... So if I make it visible and then hide it again in the main form load, it works... Any ideas how i could avoid the flicker of the form showing and hiding and how it actually should be done? :D
Thanks
Re: [2005] Showing another form
Can't you just call the show and hide method?
Form1.Show()
Form1.Hide()
1 Attachment(s)
Re: [2005] Showing another form
Here is what i have so far... Any comments would be useful :)
Re: [2005] Showing another form
There is no need to call the visible properties in the for load.
Just change you Show and Hide functions to call
VB Code:
Private Sub ShowRead()
fRead.Show()
End Sub
Private Sub HideRead()
fRead.Hide()
End Sub
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Serial.IsOpen = True Then
Serial.Close()
End If
Index = 0
fRead.pbRead.Minimum = 0
fRead.pbRead.Maximum = 63
fRead.pbRead.Value = 0
Try
With Serial
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
Serial.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Re: [2005] Showing another form
I get an exception :
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
on this line :
fRead.Invoke(New myDelegate(AddressOf ShowRead))
Re: [2005] Showing another form
Question, why are you using delegates to show the form? Why not just call the show method in your DataRecieved sub like so
VB Code:
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles Serial.DataReceived
If fRead.Visible = False Then
fRead.Show()
End If
fRead.pbRead.Invoke(New myDelegate(AddressOf ChangePB))
Tdata(Index) = Serial.ReadByte
Index += 1
If Index = 64 Then
Index = 0
Call Display()
End If
End Sub
Re: [2005] Showing another form
Cross-thread operation not valid: Control 'frmRead' accessed from a thread other than the thread it was created on.
Re: [2005] Showing another form
Have I worded it wrong? I can't be the first to run into this, can I? :)
Re: [2005] Showing another form
Someone? Anyone? Please? :(
Re: [2005] Showing another form
Re: [2005] Showing another form
Here is something similiar out of an application that I have done.
My ShowProcessing.vb class
Code:
Imports System.ComponentModel
Public Class ShowProcessing
Implements IDisposable
Private _TextToDisplay As String
Private WithEvents _bw As New BackgroundWorker
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
End If
_bw.Dispose()
End If
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
#Region " Property Declarations "
Friend Property TextToDisplay() As String
Get
Return _TextToDisplay
End Get
Set(ByVal value As String)
_TextToDisplay = value
End Set
End Property
#End Region
Public Sub New()
_bw.WorkerSupportsCancellation = True
End Sub
Friend Sub StartProcessing()
_bw.RunWorkerAsync()
End Sub
Friend Sub StopProcessing()
_bw.CancelAsync()
End Sub
Private Sub _bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _bw.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
e.Result = DisplayForm(worker, _TextToDisplay, e)
End Sub
Private Function DisplayForm(ByVal worker As BackgroundWorker, ByVal inText As String, ByVal e As System.ComponentModel.DoWorkEventArgs) As Boolean
Dim frm As New frmProcessing
With frm
.Text = inText
.ShowDialog(worker, e)
.Dispose()
End With
End Function
End Class
My ProgressBar form:
Code:
Public Class frmProcessing
Private _worker As System.ComponentModel.BackgroundWorker
Private _e As System.ComponentModel.DoWorkEventArgs
Private Sub frmProcessing_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
_e.Cancel = True
End Sub
Private Sub frmProcessing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
With ProgressBar1
.Minimum = 0
.Maximum = 100
.Value = 0
End With
With Timer1
.Interval = 500
.Start()
End With
Cursor = Cursors.WaitCursor
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If _worker.CancellationPending Then
Me.Close()
End If
If ProgressBar1.Value = 100 Then
ProgressBar1.Value = 0
Else
ProgressBar1.Value += 1
End If
End Sub
Public Overloads Function ShowDialog(ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs) As System.Windows.Forms.DialogResult
_worker = worker
_e = e
MyBase.ShowDialog()
End Function
End Class
Re: [2005] Showing another form
The example above is shown by FormA when it starts a SQL Stored Procedure that takes about 5 minutes to run. I just wanted the user to understand that it was still working.
That should give you a push in the right direction.
PS - Why all of the Delegates in your code? Is there something I am missing?
Re: [2005] Showing another form
I had the Delegates, so I could change the values of the label and the progressbar without getting the errors I've written above (Cross-thread operation not valid) :( I might be going about this the wrong way... I'll look at your code, try and compare. So thanks, I'll have a look :)