Panel Back ground image flickering when loading the form VB net - vba

Im loading an image to the background of a panel but when i load the form it flickers as hell and, the controlls flicker as much when they are showing up.
The BackgroungImageLayout is streched and the form as DoubleBuffer = True.
Im loading the image with code and not on the design interface. Just as simple as this
Me.Panel1.BackgroundImage = Image.FromFile(My.Application.Info.DirectoryPath & "\Assets\MenuInicial.bmp")
im doing this on the load event where i make the form invisible while loading everything
and then on the show event i set the form visible = true like this :
Private Sub Critérios_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Visible = False
util.MaximizarForm(Me)
'Cursor = Cursors.WaitCursor
Me.Panel1.BackgroundImage = Image.FromFile(My.Application.Info.DirectoryPath & "\Assets\MenuInicial.bmp")
Form_Principal.lbl_relogio.BackColor = ColorTranslator.FromHtml(hexColorGrey)
Form_Principal.Lbl_Data.BackColor = ColorTranslator.FromHtml(hexColorGrey)
Form_Principal.lbl_relogio.ForeColor = ColorTranslator.FromHtml(hexColorBlue)
Form_Principal.Lbl_Data.ForeColor = ColorTranslator.FromHtml(hexColorBlue)
Me.lbl_Titulo.ForeColor = Color.White
Me.Lbl_1.ForeColor = Color.White
Me.Lbl_2.ForeColor = Color.White
Me.Lbl_4.ForeColor = Color.White
Me.Lbl_NomeCliente.ForeColor = Color.White
Me.Lbl_Logotipo.ForeColor = Color.White
MenuStrip1.BackColor = ColorTranslator.FromHtml(hexColorGrey)
Me.Btn_Descelecionar.BackColor = Color.White
Me.BackColor = ColorTranslator.FromHtml(hexColorGrey)
cliente = New DAL.Clientes(xml.GetSetting("Servidor"), xml.GetSetting("Port"), xml.GetSetting("BaseDados"), xml.GetSetting("UtilizadorBD"), xml.GetSetting("Password"))
CarregarCMBClientes()
Block(True)
Btn_Descelecionar.Visible = False
End Sub
Private Sub Form_Criterios_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Visible = True
'Cursor = Cursors.Default
End Sub
I already tried to resize the images to smaller sizes and change its format to png, jpg etc.. Nothing seems to work.
Can anyone help me ?
Thanks in advance, and also, sry for my english :)

Related

Is it possible to stop one sub with another midway through it's process? VB

I'm working on forms with vb and I have a process where I need to be able to cut it off at any point, sort of like an emergency stop, and then be able to resume it. I've managed to make the code for the actual traffic lights (which need to be able to be cut off) though it's very repetitive and I've only just started coding so apologies about that.
It looks like this right now
Public Class Form1
Dim x = True
Private Async Sub btn_start_Click(sender As Object, e As EventArgs) Handles btn_start.Click
While x
If Not x Then
Exit While
End If
Await Task.Delay(5000)
SB_yellow.BackColor = Color.Yellow
If Not x Then
Exit While
End If
Await Task.Delay(3000)
SB_green.BackColor = Color.Green
SB_yellow.BackColor = Color.White
SB_red.BackColor = Color.White
If Not x Then
Exit While
End If
Await Task.Delay(10000)
SB_green.BackColor = Color.White
SB_yellow.BackColor = Color.Yellow
If Not x Then
Exit While
End If
Await Task.Delay(3000)
SB_yellow.BackColor = Color.White
SB_red.BackColor = Color.Red
If Not x Then
Exit While
End If
Await Task.Delay(5000)
SA_yellow.BackColor = Color.Yellow
If Not x Then
Exit While
End If
Await Task.Delay(3000)
SA_red.BackColor = Color.White
SA_yellow.BackColor = Color.White
SA_green.BackColor = Color.Green
If Not x Then
Exit While
End If
Await Task.Delay(10000)
SA_green.BackColor = Color.White
SA_yellow.BackColor = Color.Yellow
If Not x Then
Exit While
End If
Await Task.Delay(3000)
SA_yellow.BackColor = Color.White
SA_red.BackColor = Color.Red
End While
End Sub
Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click
x = False
MsgBox("stop")
End Sub
End Class
SB and SA are setb and seta traffic lights
Currently, when I run my form and I press btn_stop it waits until it finishes changing the traffic light to another colour, and then it stops the while loop.
I was using threading.thread.sleep() before but I got rid of that because it shut down the UI, so I ended up using await and it works for now, but I'm not sure on how, if at all possible, to cut off the code/pause it.
I chose a while loop because I couldn't figure out another way to make the code loop.
For further context (in case it helps), my assignments asks that we make a program that runs a sequence of predetermined traffic lights for set a and set b, which I managed to make, and then to be able to override it in order to set both sets of lights to red in case of an "emergency", and then safely restart the lights afterwards. Finally to develop a mobile app that can control the traffic lights.
• Provide the correct operational sequence for the Set A traffic lights
• Provide the correct operational sequence for the Set B traffic lights
• Provide an option to override the operational sequence and change both sets of traffic lights to red, e.g. to stop all traffic on both sides of the roadworks when delivery vehicles need to enter or leave the repair area
• Be able to safely restart the traffic light operational sequence, e.g. after the override has been triggered or at any other time.
• Develop a mobile app that will control the traffic lights remotely.
In developing the software solution system, you should consider enhanced user experiences and how it would deal with any unexpected events that may occur.
Currently at a wall and not sure what to do. I'm aware I can clean up the code and make it look nicer, this is more of just a first draft/attempt at it and I didn't intend to get this far.
Any help is greatly appreciated! As well as any tips or habits to develop.
I assume you have access to timer control since you are creating a forms. I think using a it will help you in this.
I changed x to make a counter for seconds and it will be useful when determining what is the current time
Dim x As Integer = 0
Private Sub btn_start_Click(sender As Object, e As EventArgs) Handles btn_start.Click
Timer1.Interval = 1000
Timer1.Start()
End Sub
The stop button will stop the timer, reset the counter, and set the traffic light to stop
Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click
Timer1.Stop()
x = 0
SB_green.BackColor = Color.White
SB_green.BackColor = Color.White
SB_red.BackColor = Color.Red
SA_green.BackColor = Color.White
SA_yellow.BackColor = Color.White
SA_red.BackColor = Color.Red
MsgBox("stop")
End Sub
The Timer will set the color of the traffic lights depending on what you've set it.
The case statement is a placeholder only. I don't think using case statement will be the best for this scenario because the delay won't be customizable
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Select Case x
Case 0
SB_green.BackColor = Color.Green
SB_yellow.BackColor = Color.White
SB_red.BackColor = Color.White
SA_green.BackColor = Color.White
SA_yellow.BackColor = Color.White
SA_red.BackColor = Color.Red
Case 6
SB_green.BackColor = Color.White
SB_yellow.BackColor = Color.Yellow
SB_red.BackColor = Color.White
SA_green.BackColor = Color.White
SA_yellow.BackColor = Color.Yellow
SA_red.BackColor = Color.White
Case 9
SB_green.BackColor = Color.White
SB_yellow.BackColor = Color.White
SB_red.BackColor = Color.Red
SA_green.BackColor = Color.Green
SA_yellow.BackColor = Color.White
SA_red.BackColor = Color.White
Case 13
x = 0
Exit Sub
End Select
x += 1
End Sub

Bring Windows Forms window back to front after opening an external application

I am opening from my VB.net Windows Forms application a PDF:
Try
Process.Start(fileName)
Catch e As System.ComponentModel.Win32Exception 'no PDF viewer installed, use browser
Dim startinfo As New ProcessStartInfo
startinfo.FileName = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
startinfo.Arguments = String.Format(fileName)
startinfo.UseShellExecute = False
Process.Start(startinfo)
End Try
I want my application form to come back to front after the PDF is opened. I have tried all of those, but neither works:
Me.Activate()
Me.BringToFront()
Me.TopMost = True
Me.TopMost = False
Just using Me.TopMost=True in fact works, but I do not want to enforce my application to be in front of all others windows. I just want to bring it to front once after PDF opening. As soon as I add the command Me.TopMost = False to reset it, it does not work any more.
I don't know if you can use a specific API to do what you ask, but from my point of view you have already found the solution:
Me.TopMost = True
Me.TopMost = False
You have to use these two lines right after the end of the PDF opening operations.
Here is an example using the following code:
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Green
Label1.Text = "Start"
End Sub
Private Sub PdfOperation_Simulation(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
Me.BackColor = Color.Red
Label1.Text = "Wait..."
Me.Refresh()
Thread.Sleep(3000) 'Pdf opening... I manually bring to front VisualStudio
Me.TopMost = True
Me.TopMost = False
Me.BackColor = Color.Green
Label1.Text = "Finish"
End Sub
End Class
Output:
UPDATE
It just does not work for me. I have simplified my code to Dim
startinfo As New ProcessStartInfo startinfo.FileName
="C:\README.TXT" startinfo.UseShellExecute = True Process.Start(startinfo) Me.TopMost = True Me.TopMost = False This
opens notepad, but my application is not coming to front.
This is because the notepad window showing operation take some time. Depending on your needs, you can simply wait some seconds by using a Thread.Sleep just before Me.TopMost = False or do something like this:
Dim startinfo As New ProcessStartInfo
startinfo.FileName = "C:\README.TXT"
startinfo.UseShellExecute = True
Process.Start(startinfo)
Dim t As New Thread(
Sub()
Thread.Sleep(1000)' One second
Me.Invoke(Sub() MoveOnTop())
End Sub
)
t.Start()
Where MoveOnTop is
Private Sub MoveOnTop()
Me.TopMost = True
Me.TopMost = False
End Sub
Using the thread, your application will not be frozen by the Sleep operation.
Just another option. Have you considered setting notepad's WindowStyle to minimized? Here is an example:
Dim startinfo As New ProcessStartInfo("Notepad")
startinfo.Arguments = "C:\README.TXT"
startinfo.UseShellExecute = True
startinfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startinfo)

How do I make a more efficient color changer? VB.net

I am trying to make a more efficent button color changer for every 500ms. I don't want to make a ton of timers so I thought if I put a threading time in the tick it would work. NOPE. I failed that. My code just turns them all on at the same time.
Any help to accomplish this would be greatly appreciated.
Code:
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
BTN1.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN2.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN3.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN4.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN5.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN6.BackColor = Color.Red
Threading.Thread.Sleep(500)
End Sub
I don't have much information here but depending on which control you use, the timer will be running on the UI thread. This mean, the UI will update only when the "Tick" is finish. You'll need to set your timer to 500ms and change only one button on each tick. Something like this:
Dim buttonIndex As Integer = 1 ' Keep the value of which button to change
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
If buttonIndex = 1 Then BTN1.BackColor = Color.Red
If buttonIndex = 2 Then BTN2.BackColor = Color.Red
If buttonIndex = 3 Then BTN3.BackColor = Color.Red
If buttonIndex = 4 Then BTN4.BackColor = Color.Red
If buttonIndex = 5 Then BTN5.BackColor = Color.Red
If buttonIndex = 6 Then BTN6.BackColor = Color.Red
End Sub
buttonIndex += 1
Instead of having a global variable, you can initialize the index as static inside the method. I think this should work.
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
Static buttonIndex As Integer = 1
If buttonIndex = 1 Then BTN1.BackColor = Color.Red
If buttonIndex = 2 Then BTN2.BackColor = Color.Red
If buttonIndex = 3 Then BTN3.BackColor = Color.Red
If buttonIndex = 4 Then BTN4.BackColor = Color.Red
If buttonIndex = 5 Then BTN5.BackColor = Color.Red
If buttonIndex = 6 Then BTN6.BackColor = Color.Red
buttonIndex += 1
End Sub
If course, there are "better" ways of doing this like using a list but I hope you get the general idea.
This randomly sets the backcolor of buttons
Private PRNG As New Random
Private AnimTask As Task
Private isClosing As New Threading.ManualResetEvent(False)
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
AnimateButtons.Stop()
AnimTask = Task.Run(Sub()
'random backcolor of these buttons
Dim btns() As Button = {BTN1, BTN2, BTN3}
'the colors
Dim bColors() As Color = {Color.Red, Color.LightYellow, Color.Yellow, Color.LightGreen, Color.LightBlue}
For Each b As Button In btns
Me.BeginInvoke(Sub()
'pick random color
b.BackColor = bColors(PRNG.Next(bColors.Length))
End Sub)
Threading.Thread.Sleep(50)
Next
Me.BeginInvoke(Sub()
If isClosing.WaitOne(0) Then
Me.Close()
Else
AnimateButtons.Start()
End If
End Sub)
End Sub)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not isClosing.WaitOne(0) Then
isClosing.Set()
e.Cancel = True
End If
End Sub

Printform Dialogbox Appears In PrintPreview

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

VB.NET PictureBox / Controls

So interesting dilemma, I've managed to make a label completely invisible, to where I can use it for a click event on certain parts of a picture.
I then use that click event to call another picturebox into focus using picturebox3.visible = true..
The issue I'm having is when it's calling that picturebox visibility..the controls from the new picturebox (Invisible labels) seem to not function or be missing from the picture in picturebox2 completely.
I need to do this with about 30 different pictures in order to create a kind of "emulator" project for someone.
Any ideas on this? I can post code if needed. Picturebox + controls on picturebox = headache.
Public Class InvisibleLabel
Inherits Label
Public Sub New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim CC As CreateParams = MyBase.CreateParams
CC.ExStyle = CC.ExStyle Or &H20
Return CC
End Get
End Property
End Class
This is the code for the invisible label, then I'm just using picturebox2.visible = true when certain parts of a picture are clicked.
I made 3 textboxes
textbox1 for X 'just for you to see
textbox2 for Y 'just for you to see
and
CurPicture to compare current image
my picturebox is 300,300
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
Dim LocX As Integer = e.X
Dim LocY As Integer = e.Y
TextBox1.Text = e.X.ToString
TextBox2.Text = e.Y.ToString
If LocX > 200 Then ' click right side op the picture , change LocX With LocY to make it bottom
If CurPicture.Text = "1" Then
PictureBox1.Image = My.Resources.Pic2
CurPicture.Text = "2"
ElseIf CurPicture.Text = "2" Then
PictureBox1.Image = My.Resources.Pic3
CurPicture.Text = "3"
ElseIf CurPicture.Text = "3" Then
PictureBox1.Image = My.Resources.Pic4
CurPicture.Text = "4"
ElseIf CurPicture.Text = "4" Then
PictureBox1.Image = My.Resources.Pic5
CurPicture.Text = "5"
ElseIf CurPicture.Text = "5" Then
PictureBox1.Image = My.Resources.Pic1
CurPicture.Text = "1"
End If
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = My.Resources.Pic1
CurPicture.Text = "1"
End Sub
Hope this will help you get on the way :)
Use:
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs) _
Handles PictureBox.MouseDown
'The code to change the picture goes here
End Sub