I would like to move my borderless form once a timer get's called, however, it is not doing anything. It also doesn't give an error. It just does nothing... Hope someone can help.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Debug.WriteLine("Timer has ticked. " & TimeOfDay)
Try
If Screen.AllScreens.Length = 2 Then
Debug.WriteLine("Screen is connected!")
Me.Location = New Point(Screen.AllScreens(1).Bounds.X, Screen.AllScreens(1).Bounds.Y)
Else
Debug.WriteLine("Screen is not connected!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Use this :
If Screen.AllScreens.Length > 1 Then
' To make it work with more than 2 screens (never tried though)
Dim SecondaryScreenIndex As Int32 = -1
' Find Secondary Screen...
' Because it happens AllScreens(0) is not always PrimaryScreen..?
For i As Int32 = 0 To Screen.AllScreens.Length - 1
If Screen.AllScreens(i) IsNot Screen.PrimaryScreen Then
SecondaryScreenIndex = i
Exit For
End If
Next
If SecondaryScreenIndex > -1 Then
Debug.WriteLine("Screen is connected!")
Me.Location = New Point( _
Screen.AllScreens(SecondaryScreenIndex ).Bounds.Left, _
Screen.AllScreens(SecondaryScreenIndex ).Bounds.Top)
' Try Left and Top if it makes any difference.
Else
Debug.WriteLine("Screen is not connected!")
End If
End If
The following is optional
.
(or garbage)
I would advise you to create a Class, like Screen_Class or ScreenTools with below content that handles the monitoring/checks on the available Screens if you plan to load more Forms...
' ...
Public Shared Event ScreensChanged() ' <- capture this event..
Private Shared ps_ScreensMonitor As System.Windows.Forms.Timer = Nothing
Private Shared ps_MonitorCount As Int32 = 0
Private Shared ps_PrimaryScreenIndex As Int32 = 0
Public Shared Sub BeginScreensMonitoring()
If ps_ScreensMonitor Is Nothing Then
ps_ScreensMonitor = New System.Windows.Forms.Timer()
ps_ScreensMonitor.Interval = 500
AddHandler ps_ScreensMonitor.Tick, AddressOf HandleScreensMonitoring
End If
If Not ps_ScreensMonitor.Enabled Then
ps_ScreensMonitor.Enabled = True
End If
End Sub
Public Shared Sub SuspendScreensMonitoring()
If ps_ScreensMonitor IsNot Nothing Then
ps_ScreensMonitor.Enabled = False
RemoveHandler ps_ScreensMonitor.Tick, AddressOf HandleScreensMonitoring
ps_ScreensMonitor.Dispose()
ps_ScreensMonitor = Nothing
End If
End Sub
Private Shared Sub HandleScreensMonitoring(sender As Object, e As System.EventArgs)
Dim i As Int32
Dim AnythingChanged As Boolean = False
' Locate PrimaryScreen...
For i = 0 To Screen.AllScreens.Length - 1
If Screen.AllScreens(i) Is Screen.PrimaryScreen Then
If ps_PrimaryScreenIndex <> i Then
ps_PrimaryScreenIndex = i
AnythingChanged = True
End If
Exit For
End If
Next
' Find if a screen has been connected or disconnected
If ps_MonitorCount <> Screen.AllScreens.Length Then
ps_MonitorCount = Screen.AllScreens.Length
AnythingChanged = True
End If
' Fire the event to notify the changes.
If AnythingChanged Then
RaiseEvent ScreensChanged
End If
End Sub
' WARNING !!! THIS IS NOT THREAD SAFE !
Then launch monitoring from your main Form constructor or Loading :
' ...
Screen_Class.BeginScreensMonitoring()
AddHandler Screen_Class.ScreensChanged, AddressOf HandleScreensUpdate
' ...
Private Sub HandleScreensUpdate()
' Do here whatever you want here...
' Get the index of the PrimaryScreen for example,
' (^^ you can also make a static ReadOnly PrimaryScreenIndex Property
' in the custom class above and even add a
' Static ReadOnly SecondaryScreenIndex if you want)
End Sub
Then you could control the loading of any form depending on the members you make available on this custom Class. Making this approach useable for your requirements depends on how you're using your forms...
Related
So I've been looking around for a solution to this for a little over a week to no avail. I have a program that needs to be able to print htm(l) files and I'm having a terrible time getting it to comply.
This is the code I'm using at the moment:
Private Sub HtmlPrinterLaunch(i As Integer)
'Dim htmlWBPrinter As New WebBrowser()
'AddHandler htmlWBPrinter.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf HtmlPrinter)
'htmlWBPrinter.Visible = True
'htmlWBPrinter.ScriptErrorsSuppressed = False
'htmlWBPrinter.Show()
''frmHTMLPrint.wbPrintHtml.AllowNavigation = True
''AddHandler frmHTMLPrint.wbPrintHtml.DocumentCompleted, AddressOf HtmlPrinter
''frmHTMLPrint.wbPrintHtml.Visible = False
''frmHTMLPrint.wbPrintHtml.Navigate("file:///" & IO.Path.GetFullPath(_prints(i).SourcePathFileName))
''Application.Run(frmHTMLPrint)
''Dim appPath As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
''Dim reportPath As String = Path.Combine(appPath, Path.GetFileName(_prints(i).SourcePathFileName))
''htmlWBPrinter.Url = New Uri(reportPath) 'New Uri(Path.Combine("file:///", reportPath)) 'New Uri("file://" & IO.Path.GetFullPath(_prints(i).SourcePathFileName))
'htmlWBPrinter.Url = (New Uri(Path.Combine("file:///" & IO.Path.GetFullPath(_prints(i).SourcePathFileName))))
'While ((htmlWBPrinter.DocumentText = ""))
' Thread.Sleep(10000)
'End While
'htmlWBPrinter.ShowPrintDialog()
'' htmlWBPrinter.Dispose()
Dim wb As New WebBrowser
AddHandler wb.DocumentCompleted, Sub() If wb.ReadyState = WebBrowserReadyState.Complete Then wb.Print()
wb.ScriptErrorsSuppressed = True
Dim url As New Uri(Path.Combine("file:///" & IO.Path.GetFullPath(_prints(i).SourcePathFileName)))
wb.Navigate(url)
End Sub
Private Sub HtmlPrinter(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
state = 4
Dim wbPrinter As WebBrowser = CType(sender, WebBrowser)
wbPrinter.Print()
wbPrinter.Dispose()
'frmHTMLPrint.BeginInvoke(New Action(Sub() frmHTMLPrint.Close()))
End Sub
As you can see I have a couple attempts in there (kept some older code that sort of worked but I'd rather not us it as I kept getting pop ups)
Some likely related issues:
-the WebBrowser state stays in loading (1)
-the Url never updates with the file:///path even if I pass it directly
So to put it in brief, my in code WebBrowser control won't hit the DocumentCompleted event, nor will it print out files. I need this code to print documents with no input from the user. What am I missing here?
Edit:
So I've messed around with this some more. I have the webbrowser control on its own form and I can get it to load/print when called from the main thread, but I'm having no luck invoking it. My current code for invocation:
If wbPrintHtml.InvokeRequired Then
If url.SourcePathFileName = "about:blank" Then
wbPrintHtml.Invoke(CType(Sub()
wbPrintHtml.Navigate(url.SourcePathFileName)
End Sub, MethodInvoker))
Else
wbPrintHtml.Invoke(CType(Sub()
wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
End Sub, MethodInvoker))
End If
Else
If url.SourcePathFileName = "about:blank" Then
wbPrintHtml.Navigate(url.SourcePathFileName)
Else
wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
End If
End If
I finally got this to work as intended. It's not the prettiest thing in the world, but it functions, and I'm ok with that.
Public Class frmHTMLPrint
Public Shared formHandle As frmHTMLPrint
Private Sub wbPrintHtml_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles wbPrintHtml.DocumentCompleted
Dim wbPrinter As WebBrowser = CType(sender, WebBrowser)
If wbPrinter.ReadyState = WebBrowserReadyState.Complete AndAlso Not wbPrinter.Url.ToString() = "about:blank" Then
wbPrinter.Print()
End If
End Sub
Shared Function setURL(url As Reporter.ReportServer.PrintMessageType) As Boolean
If formHandle.wbPrintHtml.InvokeRequired Then
If url.SourcePathFileName = "about:blank" Then
formHandle.wbPrintHtml.Invoke(CType(Sub()
formHandle.wbPrintHtml.Navigate(url.SourcePathFileName)
End Sub, MethodInvoker))
Else
formHandle.wbPrintHtml.Invoke(CType(Sub()
formHandle.wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
End Sub, MethodInvoker))
End If
Dim wbReady As WebBrowserReadyState
formHandle.wbPrintHtml.Invoke(CType(Sub()
wbReady = formHandle.wbPrintHtml.ReadyState
End Sub, MethodInvoker))
While ((Not wbReady = WebBrowserReadyState.Complete))
Application.DoEvents()
formHandle.wbPrintHtml.Invoke(CType(Sub()
wbReady = formHandle.wbPrintHtml.ReadyState
End Sub, MethodInvoker))
End While
Return wbReady = 4
Else
If url.SourcePathFileName = "about:blank" Then
formHandle.wbPrintHtml.Navigate(url.SourcePathFileName)
Else
formHandle.wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
End If
While ((Not formHandle.wbPrintHtml.ReadyState = WebBrowserReadyState.Complete))
Application.DoEvents()
End While
Return formHandle.wbPrintHtml.ReadyState = 4
End If
End Function
Private Sub frmHTMLPrint_Load() Handles Me.Load
InitializeComponent()
Dim wbInitializer As New Reporter.ReportServer.PrintMessageType
formHandle = Me
wbInitializer.SourcePathFileName = "about:blank"
setURL(wbInitializer)
End Sub
End Class
I'm creating a multiclient-server application (chat room). I 'store' every connection (actually the connection-objects) in a hashtable. I also want to see and select one client from a combobox. I managed to bind the combobox to the hashtable but when I try to update the hashtable and then the combobox it changes every item to:
System.Collections.DictionaryEntry
to
user_selector is the combobox
registered_clients is the hashtable(clients connected through tcp to the server)
DB_clients is another hashtable (it takes users from a database).The combobox will show users from this hashtable.
The Form Code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
form = Me ''The variable is used in the module
conn_listener.Start()
conn_listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf new_client), conn_listener)
End Sub
Private Sub new_client(ByVal ar As IAsyncResult)
total_logged_clients += 1
temp_client = New Client(conn_listener.EndAcceptTcpClient(ar))
AddHandler temp_client.Connected, AddressOf Connected
AddHandler temp_client.Disconnected, AddressOf Disconnected
AddHandler temp_client.New_Message, AddressOf New_Message
Update_Log_Data("New user found & added. Waiting for details...")
conn_listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf new_client), conn_listener) ''Loop
End Sub
Private Sub Connected(ByVal user As Client_Info)
total_registered_clients += 1
registered_clients.Add(user.ID, temp_client) '' Register each client based on its ID (in the hashtable)
Update_Log_Data("Confirmed client:" & user.Computer_Name & "," & user.ID)
RaiseEvent Update_GUI() ''Function from the module
End Sub
Private Sub Disconnected(ByVal user As Client_Info, ByVal reason As System.Exception)
Dim class_for_disposal As Client
total_logged_clients -= 1
If Not user.Connection_Status = Enum_Connection_Status.NO_INFO Then
total_registered_clients -= 1
End If
''Dispose this class
If registered_clients.ContainsKey(user.ID) Then
class_for_disposal = registered_clients.Item(user.ID)
registered_clients.Remove(user.ID)
class_for_disposal.Dispose()
End If
Update_Log_Data("Deconnected:" & user.Computer_Name & "," & user.ID & " because:" & reason.Message, 1)
RaiseEvent Update_GUI()
End Sub
Private Sub user_selector_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles user_selector.SelectionChangeCommitted
Dim temp_selection As New DictionaryEntry
temp_selection = user_selector.SelectedItem
If registered_clients.ContainsKey(temp_selection.Key) Then
'' If the selected user is in the hastable then
''set "selected_user" to the object created by the user
'' With this object I can sendand receive from the selected user.
selected_user = registered_clients.Item(temp_selection.Key)
Else
selected_user = Nothing
End If
''Update_Data_GUI()
RaiseEvent Update_GUI()
End Sub
The Module
Private Sub Update_GUI() Handles form.Update_GUI
''Called often by controls
Interface_DB_clients()
Interface_bottom_conn_status()
End Sub
Private Sub Interface_bottom_conn_status()
With form
If .bottom_band.InvokeRequired Then
.Invoke(New Repeat(AddressOf Interface_bottom_conn_status))
Else
If selected_user Is Nothing Then
.bottom_client_status.ForeColor = Color.Red
.bottom_client_status.Text = "Offline"
Else
.bottom_client_status.ForeColor = Color.Green
.bottom_client_status.Text = "Online"
End If
End If
End With
End Sub
Private Sub Interface_DB_clients()
Dim preserve_item As DictionaryEntry
Dim bind As New BindingSource
With form
If .user_selector.InvokeRequired Then
.Invoke(New Repeat(AddressOf Interface_DB_clients))
Else
'' Here it's being made the refresh
preserve_item = .user_selector.SelectedItem
bind.DataSource = DB_clients
.user_selector.DataSource = Nothing ''clear first
.user_selector.DataSource = bind
.user_selector.ValueMember = "Value"
'' Selected old value
.user_selector.SelectedItem = preserve_item
End If
End With
End Sub
I've created special classes for the clients and that's why you can see properties like user.id and structures like Header_Info and Client_Info. I tried overlaping the data sources ,here
.user_selector.DataSource = Nothing ''clear first
.user_selector.DataSource = bind
by simply removing the .user_selector.DataSource = Nothing part.It worked when I added a new element but what if I remove a user? And why is it showing that error?
Where is the problem?
I should change the color to a cell which contains the parameter 'tarjeta_fam'. I tried to change the cell default property and then invalidate the row to refresh it, but (obviously) nothing happens. It's possible to change a cell color out of the cell formatting event?
Public Sub New(user As Usuario, ByVal tarjeta_fam As String)
InitializeComponent()
gridFamiliares.DataSource = BD.getTable(a query)
If Me.gridFamiliares.Rows.Count > 0 Then
For i As Integer = 0 To Me.gridFamiliares.Rows.Count - 1
If Me.gridFamiliares.Rows(i).Cells("tarjeta_fam").Value = tarjeta_fam Then
Me.gridFamiliares.Rows(i).DefaultCellStyle.BackColor = Color.Black
Me.gridFamiliares.InvalidateRow(i)
End If
Next
End If
End Sub
The DataGridView control really wants you to use the CellFormatting event for this, so declare a form level variable to be used by that event:
Private tarjeta_fam_Value As String = String.Empty
Public Sub New(user As Usuario, ByVal tarjeta_fam As String)
InitializeComponent()
gridFamiliares.DataSource = BD.getTable(a query)
tarjeta_fam_Value = tarjeta_fam
End Sub
Private Sub gridFamiliares_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridFamiliares.CellFormatting
If tarjeta_fam_Value <> String.Empty Then
With gridFamiliares.Rows(e.RowIndex)
If .Cells("tarjeta_fam").Value = tarjeta_fam_Value Then
.DefaultCellStyle.BackColor = Color.Black
End If
End With
End If
End Sub
I'm trying to create a thread so when I click a button it creates a new PictureBox from a class, this is how far I've got but nothing comes up on the screen at all.
Form1 code:
Public Class Form1
Private pgClass As New SecondUIClass
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
pgClass = New SecondUIClass
pgClass.x += 100
pgClass.thread()
End Sub
End Class
Class Code:
Imports System.Threading
Public Class SecondUIClass
Public Const count As Integer = 1000
Public emeny(count - 1) As PictureBox
Public counter As Integer = 0
Public x As Integer = 0
Private trd As Thread
Public Sub thread()
trd = New Thread(AddressOf NewUIThread)
trd.SetApartmentState(ApartmentState.STA)
trd.IsBackground = False
trd.Start()
End Sub
Private Sub NewUIThread()
emeny(counter) = New PictureBox
emeny(counter).BackColor = Color.Red
emeny(counter).Visible = True
emeny(counter).Location = New System.Drawing.Point(x, 100)
emeny(counter).Size = New System.Drawing.Size(10, 50)
Form1.Controls.Add(emeny(counter))
For z = 0 To 13
emeny(counter).Location = New Point(emeny(counter).Location.X + 10, emeny(counter).Location.Y)
Application.DoEvents()
Threading.Thread.Sleep(100)
Next
counter += 1
End Sub
End Class
I have posted something similar before on here but it was different, the pictureBoxes were showing on the screen but I was trying to get them to move at the same time but they wouldn't move, they only moved one at a time. The question that I asked before was this Multi threading classes not working correctly
I made a few assumptions for this answer so it may not work for you out of the box but I think it will put you on the right track without using any Thread.Sleep calls because I personally don't like building intentional slows to my apps but that's a personal preference really.
So For my example I just used a bunch of textboxes because I didn't have any pictures handy to fiddle with. But basically to get it so that the user can still interact with the program while the moving is happening I used a background worker thread that is started by the user and once its started it moves the textboxes down the form until the user tells it to stop or it hits an arbitrary boundary that I made up. So in theory the start would be the space bar in your app and my stop would be adding another control to the collection. For your stuff you will want to lock the collection before you add anything and while you are updating the positions but that is up to your discretion.
So the meat and potatoes:
in the designer of the form I had three buttons, btnGo, btnStop and btnReset. The code below handles the click event on those buttons so you will need to create those before this will work.
Public Class Move_Test
'Flag to tell the program whether to continue or to stop the textboxes where they are at that moment.
Private blnStop As Boolean = False
'Worker to do all the calculations in the background
Private WithEvents bgWorker As System.ComponentModel.BackgroundWorker
'Controls to be moved.
Private lstTextBoxes As List(Of TextBox)
'Dictionary to hold the y positions of the textboxes.
Private dtnPositions As Dictionary(Of Integer, Integer)
Public Sub New()
' Default code. Must be present for VB.NET forms when overwriting the default constructor.
InitializeComponent()
' Here I instantiate all the pieces. The background worker to do the adjustments to the position collection, the list of textboxes to be placed and moved around the form
' and the dictionary of positions to be used by the background worker thread and UI thread to move the textboxes(because in VB.NET you can not adjust controls created on the UI thread from a background thread.
bgWorker = New System.ComponentModel.BackgroundWorker()
Me.lstTextBoxes = New List(Of TextBox)
Me.dtnPositions = New Dictionary(Of Integer, Integer)
For i As Integer = 0 To 10
Dim t As New TextBox()
t.Name = "txt" & i
t.Text = "Textbox " & i
'I used the tag to hold the ID of the textbox that coorelated to the correct position in the dictionary,
' technically you could use the same position for all of them for this example but if you want to make the things move at different speeds
' you will need to keep track of each individually and this would allow you to do it.
t.Tag = i
dtnPositions.Add(i, 10)
'Dynamically position the controls on the form, I used 9 textboxes so i spaced them evenly across the form(divide by 10 to account for the width of the 9th text box).
t.Location = New System.Drawing.Point(((Me.Size.Width / 10) * i) + 10, dtnPositions(i))
Me.lstTextBoxes.Add(t)
Next
'This just adds the controls to the form dynamically
For Each r In Me.lstTextBoxes
Me.Controls.Add(r)
Next
End Sub
Private Sub Move_Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
'Don't need to do anything here. Placeholder
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
Try
If Not bgWorker.IsBusy Then
'User starts the movement.
bgWorker.RunWorkerAsync()
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
Try
'Reset the positions and everything else on the form for the next time through
' I don't set the blnStop value to true in here because it looked cooler to keep reseting the textboxes
' and have them jump to the top of the form and keep scrolling on their own...
For Each r In Me.lstTextBoxes
r.Location = New System.Drawing.Point(r.Location.X, 10)
Next
For i As Integer = 0 To dtnPositions.Count - 1
dtnPositions(i) = 10
Next
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub bgWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
Try
'This is where we do all the work.
' For this test app all its doing is scrolling through each value in the dictionary and incrementing the value
' You could make the dictionary hold a custom class and have them throttle themselves using variables on the class(or maybe travel at an angle?)
For i As Integer = 0 To dtnPositions.Count - 1
dtnPositions(i) += 1
Next
Catch ex As Exception
blnStop = True
End Try
End Sub
Private Sub bgWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
Try
'Once the background worker is done updating the positions this function scrolls through the textboxes and assigns them their new positions.
' We have to do it in this event because we don't have access to the textboxes on the backgroun thread.
For Each r In Me.lstTextBoxes
r.Location = New System.Drawing.Point(r.Location.X, dtnPositions(CInt(r.Tag)))
Next
'use linq to find any textboxes whose position is beyond the threshhold that signifies they are down far enough.
' I chose the number 100 arbitrarily but it could really be anything.
Dim temp = From r In Me.lstTextBoxes Where r.Location.Y > (Me.Size.Height - 100)
'If we found any textboxes beyond our threshold then we set the top boolean
If temp IsNot Nothing AndAlso temp.Count > 0 Then
Me.blnStop = True
End If
'If we don't want to stop yet we fire off the background worker again and let the code go otherwise we set the stop boolean to false without firing the background worker
' so we will be all set to reset and go again if the user clicks those buttons.
If Not Me.blnStop Then
bgWorker.RunWorkerAsync()
Else
Me.blnStop = False
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Try
'The user clicked the stop button so we set the boolean and let the bgWorker_RunWorkerCompleted handle the rest.
Me.blnStop = True
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
End Class
Theres a lot of code there but a lot of it is comments and I tried to be as clear as possible so they are probably a little long winded. But you should be able to plop that code on a new form and it would work without any changes. I had the form size quite large (1166 x 633). So I think that's when it works best but any size should work(smaller forms will just be more cluttered).
Let me know if this doesn't work for your application.
This is a problem that is well suited to async/await. Await allows you to pause your code to handle other events for a specific period of time..
Private Async Function Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) As Task Handles Button1.Click
pgClass = New SecondUIClass
pgClass.x += 100
await pgClass.NewUIThread()
End Sub
End Class
Class Code:
Imports System.Threading
Public Class SecondUIClass
Public Const count As Integer = 1000
Public emeny(count - 1) As PictureBox
Public counter As Integer = 0
Public x As Integer = 0
Private Async Function NewUIThread() As Task
emeny(counter) = New PictureBox
emeny(counter).BackColor = Color.Red
emeny(counter).Visible = True
emeny(counter).Location = New System.Drawing.Point(x, 100)
emeny(counter).Size = New System.Drawing.Size(10, 50)
Form1.Controls.Add(emeny(counter))
For z = 0 To 13
emeny(counter).Location = New Point(emeny(counter).Location.X + 10, emeny(counter).Location.Y)
await Task.Delay(100) 'The await state machine pauses your code here in a similar way to application.doevents() until the sleep has completed.
Next
counter += 1
End Sub
End Class
hi i want to delay the execution of code for some time on buton click i cal a func named chance() ..
which gets called after a picture box.image change .. bt the image does not change nd func chance() starts ... i want delay in chance() after the picture is changed ... thus help me ..
code
Private Sub p11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p11.Click
row = 1
col = 1
chck() 'function which returns hit var value
p11.Enabled = False
If hit = 1 Then
p11.Image = Image.FromFile("G:\visual progs\BATTLESHIP\hit.png")
ElseIf hit = 0 Then
p11.Image = Image.FromFile("G:\visual progs\BATTLESHIP\miss.png")
lblstatus.Text = "COMPUTER's TURN ... PLEASE WAIT ... "
chance() ' func begins
End If
End Sub
Function chance()
***'here i want a pause for 2 sec***
Dim z As Int16 = 1
While z = 1
row = mnw.Next(9) + 1
col = mnw.Next(9) + 1
If c(row, col) = False Then
c(row, col) = True
z = 0
End If
End While
chck1() ' checks for hit or miss for computer
changepic() 'changes pic hit or miss for computer
Return 0
End Function
To get a pause of 2 seconds, you simply need to suspend the thread by doing this:
Thread.Sleep(2000)
Although you can use Thread.Sleep to introduce a delay, it has generally undesirable side-effects, in particular the form and its controls become unresponsive. A better way is to use a timer - that way the form is still responsive (e.g. you can move it around).
Also, you seem to be a little unsure of where to use a Sub and where to use a Function. A Sub does something, and a Function is used to return a value, ideally with no side-effects.
You could try this with a new Windows Forms project and just a PictureBox named p11 and a Label named lblStatus:
Imports System.IO
Public Class Form1
Dim tim As Windows.Forms.Timer
Const GAMEPATH As String = "G:\visual progs\BATTLESHIP\"
Dim HitImgFile As String = Path.Combine(GAMEPATH, "hit.png")
Dim MissImgFile As String = Path.Combine(GAMEPATH, "miss.png")
Private Sub SetUpTimer()
tim = New Timer
tim.Interval = 2000 ' milliseconds
tim.Enabled = False
AddHandler tim.Tick, AddressOf Chance
End Sub
Private Sub Chance(sender As Object, e As EventArgs)
tim.Enabled = False
' your code for the computer's turn goes here
lblStatus.Text = "Your turn"
p11.Enabled = True
End Sub
Private Function IsHitByUser() As Boolean
' placeholder code for the actual check
If Rnd() < 0.5 Then
Return True
End If
Return False
End Function
Private Sub DoComputerTurn()
lblStatus.Text = "COMPUTER's TURN ... PLEASE WAIT ... "
p11.Enabled = False
tim.Enabled = True
End Sub
Private Sub p11_Click(sender As Object, e As EventArgs) Handles p11.Click
If IsHitByUser() Then
p11.Image = Image.FromFile(HitImgFile)
lblStatus.Text = "HIT"
Else
p11.Image = Image.FromFile(MissImgFile)
DoComputerTurn()
End If
End Sub
Private Sub StartGame()
lblStatus.Text = "Your turn"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetUpTimer()
StartGame()
End Sub
End Class