Printform Dialogbox Appears In PrintPreview - vb.net

I created an app for calculation of an HVAC equipment. Everything is OK (My last question in this app) but i'm facing with a new problem. When i click the print button and then select the printer, print preview dialog shows me the printer selection dialog's traces.
I tried sleep function at different rows but it doesn't work.
How can i fix my app ?
Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Visible = False
ComboBox1.Visible = False
PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then
Thread.Sleep(3000)
With Me.PrintForm1
PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings
.PrintAction = Printing.PrintAction.PrintToPreview
Dim MyMargins As New Margins
With MyMargins
.Left = 40
.Right = 40
.Top = 40
.Bottom = 40
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
PrintForm1.DocumentName = notasyon_lbl.Text
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.CompatibleModeClientAreaOnly)
End With
End If
Me.Close()
End Sub

Related

Making keydown code only run once, at the START of the press

I am writing a piece of code that when a button is pressed, it creates a picture box, but when you hold down the same key, that picture box needs to grow.
At first my code repeated the creation of the box, but now I have tried the code below and instead it creates it once, but at the end of the code. Does anyone know how some code to execute part of a key down code on the first instance that it is held down, and only once?
Private Class Form1
Dim KeyHolding As Boolean = False
Private Sub Btn_1_KeyDown(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyDown
If Not KeyHolding Then 'the events which are activated once only when the key is pressed
KeyHolding = True
Dim PB As New PictureBox With {.Width = Btn_1.Width - 2, .Height = 10, .Top = Btn_1.Top + 20, .Left = Btn_1.Left + 1, .BackColor = Color.Cyan}
PB.Name = "PB_1"
TestPanel.Controls.Add(PB)
Else 'the events which are constantly done when the key is held
End If
End Sub
Private Sub Btn_1_KeyUp(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyUp
Btn_1.BackColor = SystemColors.Control
KeyHolding = False
End Sub
Try this
Dim KeyHolding As Boolean = False
Dim PB As PictureBox
Private Sub Btn_1_KeyDown(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyDown
If Not KeyHolding Then 'the events which are activated once only when the key is pressed
KeyHolding = True
PB = New PictureBox With {.Width = Btn_1.Width - 2, .Height = 10, .Top = Btn_1.Top + 20, .Left = Btn_1.Left + 1, .BackColor = Color.Cyan}
PB.Name = "PB_1"
TestPanel.Controls.Add(PB)
Else 'the events which are constantly done when the key is held
PB.Width += 10
PB.Height += 10
End If
End Sub
Private Sub Btn_1_KeyUp(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyUp
Btn_1.BackColor = SystemColors.Control
End Sub

Web Browser auto login error (Object reference not set to an instance of an object.)

I have 2 buttons on my form and here is the two bits of code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If LoggedInToAdastra = True Then
LogOutOfAdastra()
Exit Sub
End If
strUsername = txtUsername.Text
strPassword = txtPassword.Text
LoggedInToAdastra = False
ProgressBar1.Value = 10
Try
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
WaitForPageLoad()
WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
ProgressBar1.Value = 40
WaitForPageLoad()
ProgressBar1.Value = 80
WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
WaitForPageLoad()
ProgressBar1.Value = 100
LoggedInToAdastra = True
txtPassword.Enabled = False
txtUsername.Enabled = False
Button2.Enabled = False
Button1.Text = "Log Out"
Button2.BackColor = Color.MintCream
Button1.BackColor = Color.MintCream
txtPassword.BackColor = Color.MintCream
txtUsername.BackColor = Color.MintCream
ProgressBar1.Value = 0
btnUpdate.Enabled = True
btnDoAll.Enabled = True
btnSearchSelected.Enabled = True
doc = Nothing
Catch ex As Exception
MsgBox("Login Failed")
Button2.BackColor = Color.MistyRose
Button1.BackColor = Color.MistyRose
txtPassword.BackColor = Color.MistyRose
txtUsername.BackColor = Color.MistyRose
txtUsername.Text = ""
txtPassword.Text = ""
ProgressBar1.Value = 0
End Try
End Sub
And the second button, once logged in, you should be able to search for a patient using this button and choosing a patient from a listbox...
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
If LoggedInToAdastra = False Then
MsgBox("Please re-login to Adastra")
End If
strPatientNHS = lstPatients.SelectedItems(0).SubItems(0).Text
EditNote(strPatientNHS)
End Sub
Public Sub EditNote(strPatientNHS As String)
'Try
doc = WebBrowser1.Document
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Document.OpenNew(True)
WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
ProgressBar1.Value = 10
WaitForPageLoad()
ProgressBar1.Value = 20
WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
'WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/MainMenu.aspx")
WaitForPageLoad()
ProgressBar1.Value = 30
WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
WaitForPageLoad()
ProgressBar1.Value = 40
WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")
End Sub
It errors here on the EditNote sub
WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")
I have tried all sorts, but not sure why its bringing through the Error (Object reference not set to an instance of an object.)
The control does exist on the web page, and that is the correct ID for it. I have tried using Document.All and looking for the element too but to no avail.
I did resolve this in the end, in quite bizarre fashion...
so it was giving me an error basically because it couldn't find the control
WebBrowser1.Document.All("ctl00$MainContent$txtUserName")
so as a last resort, I copied and pasted the exact control from the page source and it worked... even though every character was the same...
This fixed my problem anyway. Don't type the control out, copy and paste it! Maybe someone with more knowledge can explain how this works? is there hidden characters in the HTML?

How to write a common code to display many picturebox in vb.net 2.0 ?

Imports System.Threading.Thread
Public Class Form1
Dim delay As Integer = 200
Dim i As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
While i <= 5
PictureBox1.Visible = True
PictureBox1.Refresh()
Sleep(delay)
PictureBox1.Visible = False
PictureBox2.Visible = True
PictureBox2.Refresh()
Sleep(delay)
PictureBox2.Visible = False
PictureBox3.Visible = True
PictureBox3.Refresh()
Sleep(delay)
PictureBox3.Visible = False
PictureBox4.Visible = True
PictureBox4.Refresh()
Sleep(delay)
PictureBox4.Visible = False
PictureBox5.Visible = True
PictureBox5.Refresh()
Sleep(delay)
PictureBox5.Visible = False
i = i + 1
If i = 6 Then
i = 1
End If
End While
End Sub
i wrote the above code. it's working. but the following code is not not working. i want to minimize the code. i want to use 20 picture box. instead of the above code i want to use small code but it will do the same work. please help me.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
While i <= 5
Dim pic As PictureBox
Dim matches() As Control
matches = Me.Controls.Find("PictureBox" & i.ToString(), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
pic = DirectCast(matches(0), PictureBox)
pic.Visible = True
pic.Refresh()
Sleep(delay)
pic.Visible = False
End If
i = i + 1
If i = 6 Then
i = 1
End If
End While
End Sub
End Class
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
While i <= 5
Dim pic As PictureBox
Dim matches() As Control
matches = Me.Controls.Find("PictureBox" & i.ToString(), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
pic = DirectCast(matches(0), PictureBox)
pic.Visible = True
pic.Refresh()
Wait(delay)
pic.Visible = False
End If
i = i + 1
If i = 6 Then
i = 1
End If
End While
End Sub
this code is correct... sorry i wrote wrong code 1st time.. now it's correct.. thank u all..

Collision detection in vb.net

I am trying to recreate Copter in visual basic, so far I have the player, roof and helicopter but I can't seem to get the game to end when the player touches the floor or roof. Sorry for posting so much, this is my first time on here and I didn't know what to paste. Any help is appreciated :D
Public Class Form1
Dim pb_field As PictureBox
Private Sub create_field()
pb_field = New PictureBox
With pb_field
.Top = 20
.Left = 20
.Width = 500
.Height = 300
.BackColor = Color.Black
End With
Me.Controls.Add(pb_field)
pb_field.BringToFront()
End Sub
Dim pb_player As PictureBox
Private Sub create_player()
pb_player = New PictureBox
With pb_player
.Width = 20
.Height = 20
.BackColor = Color.Red
.Top = pb_field.Top + pb_field.Bottom / 2
.Left = pb_field.Left + 20
End With
Me.Controls.Add(pb_player)
pb_player.BringToFront()
End Sub
#Region "Roof Stuff"
Dim roof(10000) As PictureBox
Dim num_of_roof As Integer = -1
Dim r As New Random
Private Sub create_roof()
num_of_roof += 1
roof(num_of_roof) = New PictureBox
With roof(num_of_roof)
.Top = pb_field.Top
.Left = pb_field.Right
.Height = r.Next(20, 40)
.Width = 20
.BackColor = Color.RoyalBlue
End With
Me.Controls.Add(roof(num_of_roof))
roof(num_of_roof).BringToFront()
End Sub
#End Region
#Region "floor Stuff"
Dim floor(10000) As PictureBox
Dim num_of_floor As Integer = -1
Private Sub create_floor()
num_of_floor += 1
floor(num_of_floor) = New PictureBox
With floor(num_of_floor)
.Left = pb_field.Right
.Height = r.Next(20, 40)
.Width = 20
.Top = pb_field.Bottom - floor(num_of_floor).Height
.BackColor = Color.YellowGreen
End With
Me.Controls.Add(floor(num_of_floor))
floor(num_of_floor).BringToFront()
End Sub
#End Region
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
Me.Text = e.KeyChar
If e.KeyChar = "w" Then
pb_player.Top -= 10
End If
**Dim collision As Boolean
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If**
Next
End Sub
Private Sub form1_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
create_field()
create_roof()
create_player()
tm_background.Start()
tm_gravity.Start()
End Sub
Private Sub tm_background_Tick(sender As Object, e As EventArgs) Handles tm_background.Tick
For i = 0 To num_of_roof
roof(i).Left -= 20
If roof(i).Left < pb_field.Left Then
Me.Controls.Remove(roof(i))
End If
Next
create_roof()
For i = 0 To num_of_floor
floor(i).Left -= 20
If floor(i).Left < pb_field.Left Then
Me.Controls.Remove(floor(i))
End If
Next
create_floor()
End Sub
Private Sub tm_gravity_Tick(sender As Object, e As EventArgs) Handles tm_gravity.Tick
pb_player.Top += 5
End Sub
This is the code I was attempting to use after looking online at possible solutions
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) HandlesMe.KeyPress
Me.Text = e.KeyChar
If e.KeyChar = "w" Then
pb_player.Top -= 10
End If
Dim collision As Boolean
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
Next
End Sub
Your problem is where you exit the for loop before displaying the message:
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For ' Note that exiting skips your check after the End If below
Else : collision = False
End If
' Whenever this is true you have already exited your 'for' loop
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
Next
Instead you need something like this where you evaluate the condition after the loop:
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
Next
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
First you need to set tag's in the floor and ceiling regions
With floor(num_of_floor)
.Tag = "boundaries"
Then you can refer to each picturebox in your controls
For Each box As PictureBox In Me.Controls
If box.Tag <> "boundaries" Then Continue For
If pb_player.Bounds.IntersectsWith(box.Bounds) Then
collision = True
Exit For
Else : collision = False
End If
Next
However, you are still going to have a problem that when it hits the floor it will not pass as a collision, because all this code is going on in the key press,
If a user lets the coptor fall, it will only lose the next time they click on the keyboard

looking for a way to re enable labels without specifying which ones vb.net

I am looking for a way, if its even possible, to re enable a label without actually needing to use its label name?
I have a game with labels I am using as click able boxes. The first box becomes disabled after the click event occurs, I want after the second box is clicked to re enable that first box. Any Ideas? Here is the code for the first two box click events.
Edit: there will be 15 labels, 2 can be chosen at a time. The first will be disabled so that it can't be chosen a second time.
Private Sub lblMemory1_Click(sender As Object, e As EventArgs) Handles lblMemory1.Click
Dim intClickBox As Integer = 1
Dim intClickAnswer As Integer
intClickAnswer = GuessClick(intClickBox)
If blnActive = False Then
blnActive = True
whatClicked1 = intClickBox
lblMemory1.BackColor = Color.Green
lblMemory1.Text = intClickAnswer.ToString
lblMemory1.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory1.BackColor = Color.Cyan
lblMemory1.Text = "X"
lblMemory1.Enabled = False
End If
If blnActive = True Then
whatClicked2 = intClickBox
lblMemory1.BackColor = Color.Green
lblMemory1.Text = intClickAnswer.ToString
lblMemory1.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory1.BackColor = Color.Cyan
lblMemory1.Text = "X"
blnActive = False
End If
End Sub
Private Sub lblMemory2_Click(sender As Object, e As EventArgs) Handles lblMemory2.Click
Dim intClickBox As Integer = 2
Dim intClickAnswer As Integer
intClickAnswer = GuessClick(intClickBox)
If blnActive = False Then
blnActive = True
whatClicked1 = intClickBox
lblMemory2.BackColor = Color.Green
lblMemory2.Text = intClickAnswer.ToString
lblMemory2.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory2.BackColor = Color.Cyan
lblMemory2.Text = "X"
lblMemory2.Enabled = False
End If
If blnActive = True Then
whatClicked2 = intClickBox
lblMemory2.BackColor = Color.Green
lblMemory2.Text = intClickAnswer.ToString
lblMemory2.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory2.BackColor = Color.Cyan
lblMemory2.Text = "X"
blnActive = False
End If
End Sub
you can do something like
Public Sub game(sender As Object)
Dim lbl As Label = sender
If lbl.Enabled Then
For i = 0 To Me.Controls.Count - 1
Dim lbl2 As Label = Me.Controls(i)
If Not (lbl2.Enabled) Then
lbl2.Enabled = True
GoTo exitLoop
End If
Next
End If
exitLoop:
lbl.Enabled = False
End Sub
then in each label just call the game sub and pass sender like so...
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click
game(sender)
End Sub
this will allow only 1 label to be diabled at any give time...
you can also (within the loops) use lbl and lbl2 to set the properties for each label.
if you use a var to save the name of the first textbox you can also specifically re-enable only that box...
let me know if you want the code for that too.