i have a single form, if i just click on it then it should close
how it can be done??
Printable View
i have a single form, if i just click on it then it should close
how it can be done??
i want to show a form as long as work is done
if i show that form in the start of a work and then it should close after when work is done.
for example if i have a function like this
when this function is started executing then this form should display and when this work is performed on the end of procedure then that form should close.Code:LoadTree()
End Sub
thanks.
useto unload the formCode:me.close
If you want the window to close when you click on it create a subroutine such as this:
Code:Sub FormCloser(sender As Object, e As EventArgs) Handles Me.Click
me.Close
End Sub
If you want an example on how a routine could put out a from to tell user to wait (or to show a progressbar) while a job is in progress and close it bycode when the job is ended, here is a small example (you can do it in different ways, and you have to adapt it to your code: in this example I put a main module as startup of app, and show a form from there)
I have done it with Net 2008, but there is nothing of 3.5 in it , so it should work also with earlier versions , but you might have to grab the code from modules and recreate the solution and the project)
if i use a progress bar on my form1 and then use this method in buttons click event then progress bar should start and when this methos did all work then this progress bar should stop.
progress bar should displayed as long photos are being loaded and after successfull loading progress bar should stop.
the load tree method is as follow
and this is called in click event as followCode:Private Sub LoadTree()
Try
' Start with clear tree
treeAlbum.Nodes.Clear()
' If the connection is closed, open it.
If SqlConn.State = ConnectionState.Closed Then
SqlConn.Open()
End If
' Just to be sure make sure it was opened
If SqlConn.State = ConnectionState.Open Then
' Create the stored proc command to retrieve the records
Using sqlCmd As New SqlCommand("sp_GetPhotoAlbums", SqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
' Now execute the command
Using sqlPhotoAlbum As SqlDataReader = sqlCmd.ExecuteReader()
Dim nAlbumID As Integer = 0
' Keep track of the current album
Dim nAlbumIndex As Integer = -1
' Keep track of the tree index
' Keep reading all the records returned
Dim imgList As New ImageList()
'Add image to represent an Album from resources
Dim albumImg As Image = My.Resources.IMG
imgList.Images.Add("Album", albumImg)
imgList.ColorDepth = ColorDepth.Depth32Bit
While (sqlPhotoAlbum.Read())
'Populate all images
Dim data As Byte() = CType(sqlPhotoAlbum("photo"), Byte())
Dim ms As New System.IO.MemoryStream(data, 0, data.Length)
Dim photo As Image = Image.FromStream(ms) ''
imgList.Images.Add(sqlPhotoAlbum("Photo").ToString(), photo)
treeAlbum.ImageList = imgList
' Check if a new album needs to be added to the tree
InsertAlbum(sqlPhotoAlbum("Album").ToString(), sqlPhotoAlbum("Photo").ToString(), CInt(sqlPhotoAlbum("AlbumID")), sqlPhotoAlbum("Album_Desc").ToString(), sqlPhotoAlbum("PhotoID").ToString())
End While
sqlPhotoAlbum.Close()
End Using
End Using
If (SqlConn.State = ConnectionState.Closed) Then
SqlConn.Open()
End If
Using sqlCmd2 As New SqlCommand("sp_GetPhotoAlbums", SqlConn)
Using sqlMatchPhoto As SqlDataReader = sqlCmd2.ExecuteReader()
While (sqlMatchPhoto.Read())
Dim node As TreeNode
For Each node In treeAlbum.Nodes
If (node.Name = sqlMatchPhoto("Album").ToString()) Then
Dim i As Integer
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Name = sqlMatchPhoto("Photo").ToString() Then
For i = 0 To treeAlbum.ImageList.Images.Count - 1
If treeAlbum.ImageList.Images.Keys(i) = sqlMatchPhoto("Photo").ToString() Then
childNode.ImageIndex = i
Exit For
End If
Next
Exit For
End If
Next
End If
Next
End While
sqlMatchPhoto.Close()
End Using
End Using
End If
Catch e As SqlException
'MessageBox.Show(e.Message)
End Try
End Sub
Code:LoadTree()
you have two main cycles:
To keep track of this one while going on (=if you want to show a progress of thisCode:While (sqlPhotoAlbum.Read())
'Populate all images
Dim data As Byte() = CType(sqlPhotoAlbum("photo"), Byte())
Dim ms As New System.IO.MemoryStream(data, 0, data.Length)
Dim photo As Image = Image.FromStream(ms) ''
imgList.Images.Add(sqlPhotoAlbum("Photo").ToString(), photo)
treeAlbum.ImageList = imgList
' Check if a new album needs to be added to the tree
InsertAlbum(sqlPhotoAlbum("Album").ToString(), sqlPhotoAlbum("Photo").ToString(), CInt(sqlPhotoAlbum("AlbumID")), sqlPhotoAlbum("Album_Desc").ToString(), sqlPhotoAlbum("PhotoID").ToString())
End While
partial operation) , you should know how many you have before. but as it is a
SqlDataReader, it is a reader forward only, thus it cannot tell you how many record it has.
One solution is to make a query to get the recordcount,
first, then use that value to divide a counter inside the reading cycle like in:
the second main Cylce has some nested cycles:Code:'inside your cycle
counter= counter+1
progressbar.value= counter*100/recordcount
at end is like the firsty: you do operation for each record you find in a sqlDataReaderCode:While (sqlMatchPhoto.Read())
Dim node As TreeNode
For Each node In treeAlbum.Nodes
If (node.Name = sqlMatchPhoto("Album").ToString()) Then
Dim i As Integer
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Name = sqlMatchPhoto("Photo").ToString() Then
For i = 0 To treeAlbum.ImageList.Images.Count - 1
If treeAlbum.ImageList.Images.Keys(i) = sqlMatchPhoto("Photo").ToString() Then
childNode.ImageIndex = i
Exit For
End If
Next
Exit For
End If
Next
End If
Next
End While
You could consider using same strategy as above. ou could correct the value of progressbar
to count corrected loaded item and discarding not loaded one from both the counter and the
recodcount, but I do belive it would not add any good to a simpler
"recordProcessed *100 / recordcount" formula....