timer control in vb.net - vb.net

I have a timer in vb.net and it's interval is 1000ms ,. i have placed in it's timer_tick event a code that will print screen the screen and save it to a database.
The problem is when i click outside of the form, or loosing the focus of the mouse to the form containing that timer/printscreen, the timer stops. As a result the printscreen also stops.
here are it's properties:
generate member = true
interval = 1000
modifiers = friend
I will appreciate any reply or tip regarding this problem ,. thank you.,

A simple test creating a form with a Timer with
Interval = 1000,
Enabled = True
and the following code in the form
Dim i As Integer = 0
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Debug.WriteLine(i)
i += 1
End Sub
continued to Tick (and produce output) regardless of whether or not the form had the focus.
Are you sure that you are not calling Stop() or setting Enabled to False anywhere in your code?
I'd recommend putting a breakpoint anywhere you call Stop() or change Enabled; then you can see if these lines are being executed when the form loses focus.

Related

Radio buttons fire in VB net at form load

I have a simple VB net form with two radio buttons in a group box and button outside the group box that calls another form that sets up other parameters. The radio buttons both send data over the serial port.
Form1_load event has a boolean value form_loading = True. This is checked in the rbtn handler and if true should exit the subroutine. On debug, the check changed even fires one button's event that is checked at design time and at this point the form_loading value is set to false and I have no idea why. There is no form_loading = false statement. If I remove the rbtn handler, the form_loading = True persists when the other form is called and returned. The groupbox with buttons is activated as it sees a rbtn1-CheckChanged when the form loads and form_loading value get set to false. I suspect that the rbtn event is firing as the form begins to load, before the form_loading = True statement is reached, but how do I stop it firing the button event?
As its stands, when debug start, there is an IO exception error:
Serialport is closed, and the code associated with the button is in
the buffer to send to a (closed) com port
Private Sub rbtnDon_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnDon.CheckedChanged
If form_loading Then
Exit Sub
ElseIf rbtnDoff.Checked = True Then 'event fires when other button checkchanged = true, this stops it
Exit Sub
Else
data_out = (SOT + "N" + EOT)
SendtoBoard(data_out)
End If
End Sub`enter code here`
Thanks for your help Everyone. Elsewhere I found this tip: Remove the "handles rbtnDon checkchanged" from the sub:
Private Sub rbtnDon_CheckedChanged(sender As Object, e As EventArgs)handles rbtnDon checkchanged
and add this to the form1_load sub:
AddHandler rbtnDon.CheckedChanged, New EventHandler(AddressOf rbtnDon_CheckedChanged)
That seems to have solved the problem completely.
I could not add the startup code on my original post as for some reason the box will not accept two lots of separate code - or I am not doing it right:
It is here:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ccbComPort.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
If ccbComPort.Items Is Nothing Then
lblMessage.ForeColor = Color.Red
lblMessage.Text = "No Serial Ports found"
Else
ccbComPort.Text = My.Settings.oldPort
End If
ccbBaudrate.Text = My.Settings.oldBaud
pnlComPorts.Visible = True
'form_loading = True
rbtnDon.Enabled = True
AddHandler rbtnDon.CheckedChanged, New EventHandler(AddressOf rbtnDon_CheckedChanged)
End Sub
Originally I had one form with a panel for the Com port setup and everything was fine, the issue only started when I moved the port set up to form2, after which the rbtnDon event fired before form1 started.
I did try the Sub New() approach but then I just get a small blank form on debug. Adding Form1_load to it results in "NotImplemented Exception".
The method described above seems OK and fairly simple to add but maybe it is not good practice?

More info on click event in VB

I would like to create a bot that repeats the same action many times. Is there any way I can get more info about a click event. Lets say I click somewhere on the opened file explorer window. Can the program tell that I clicked on a specific area or button in that window? Can I make the program click or type on a specific(opened) window?
Thanks
Yes the program can tell when you click on a specific button. Whatever button it is that you clicked on will have its click event fired, if it exists. In order to manually tell the program to perform this action, you can implement the phrase..
btn.PerformClick()
where "btn" is the name of whatever button you are needing to click. Whenever this phrase is called, the btn_Click event handler will be fired just the same as if you yourself actually clicked the button.
To do the same thing with an actual window or form in your program is trickier because there are no built in methods you can call to trigger a windows form click event. But it is possible. The code that will define a window's event handler and trigger it will be..
Private Sub myWindow_Click() Handles MyBase.Click
'code you want to run on click event here
End Sub
myWindow_Click() 'where you want the click to be triggered
The above would be much simpler to simulate in a method call however.
And lastly to simulate the typing of text into a window, you could simply access the text property of the control you are wanting to alter, and then change that text property to the desired text. But judging from your question, you are wanting this to be done in a similar fashion to how a human would.
This can also be accomplished with some work, using a timer interval that appends to the text of the control.
Lets say for example, you wanted "hello world" to be typed into a text box on screen as a person would. Add a timer control to your form and set its interval to 250 or however fast you want the word to be type and in a method..
Dim str as String = ""
Dim pos as Integer = 0
Public Sub humanType(word as String)
str = word
Timer1.enabled = true
End Sub
public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
myTextBox.Text = myTextBox.Text + str[pos]
pos = pos + 1
If pos = str.length Then
Timer1.enabled = false
End If
End Sub
If you have never worked with timer intervals before, this tutorial has some good info on them for VB.Net. http://www.techrepublic.com/article/perform-actions-at-set-intervals-with-vbnets-timer-control/
Hope it helps!

Checkbox events when Form is opened and closed fire when form is reloaded

First... I am open to skinning this cat a different way if I am going at it wrong to begin with. Using VB 2010 .net 4.0 and I am very much a beginner.
I am making a product billing application that has a main form and a subform with additional options. Whenever that subform is reopened after being opened once, the checkbox events that were selected are blank by default. If I recheck them (so someone can uncheck) then any that are rechecked all refire and increase the variable again.
I ultimately need to be able to open that second form after closing it, display any checkboxes that were selected before as selected again and not increase the variable in the process.
Main form Checkbox code to set booleans and increase or decrease subtotal variable of most used products.
Private Sub chkbox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox1.CheckedChanged
If chkbox1.Checked = True Then
bChkbox1 = True
Subtotal += 15
Else
bChkbox1 = False
Subtotal -= 15
End If
End Sub
Main form button to launch subform with all products listed.
Private Sub btnAllProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllProducts.Click
Form3.Show()
End Sub
Subform checkbox code works perfectly the first time it is opened but not when relaunched.
Private Sub chkbox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox2.CheckedChanged
If chkbox2.Checked = True Then 'also tried without the nested if with same results
If Me.IsHandleCreated = True Then 'me.visible behaves the same way
MsgBox("form visible true")'launches after clicking button but before form is actually on screen
Form1.bcheckbox2 = True
Form1.Subtotal += 105
End If
Else
Form1.bcheckbox2 = False
Form1.Subtotal -= 105
End If
End Sub
Booleans are used to check boxes that were checked on the main page or when it was open before.
If Form1.bcheckbox2 = True Then
chkbox2.Checked = True
End If
As I said, I can completely rework the code if it makes sense to do so or just fix something if I have made some sort of mistake.
For example, I was thinking of changing to wipe the subtotal on each form load and rebuild it based off the toggled booleans but it seems like there should be a much more elegant way with less overhead and I am just doing something incorrectly.
It is not common to have to tell checks and radios to ignore events while loading the form. You just need an Ignore or Loaded flag:
Public Class Form1
Private ignore As Boolean = True
...
Private Sub Form1_Load(...
' do normal stuff
ignore = False ' should be the ONLY place it is set
End Sub
Private Sub CheckBox2_CheckedChanged(...
If ignore Then Exit Sub
End Sub
The Form Designer code will fire events as it creates the form and controls, which CAN be handy for initializing stuff but often it causes trouble. Some controls will even get the same event twice. There isnt really a "reload" action for forms. If you hide them, Show() won't fire the Load event again.
You can avoid the flag and manually add the handlers for the troublesome controls when the form loads, but that can be tedious if there are lots of them. Flags can be abused and misused, but if it is set in that one spot only, its fine.
If someone is looking for alternative or have similar problem here's my workaround to detect event change so checkbox wouldn't get triggered on re-load:
If ((Me.CheckBox2.Value <> Sheets(1).Range("t6").Value) And (Me.CheckBox2 = True)) = True Then
' do your stuff
Me.CheckBox2.Value = False
Else
Me.CheckBox2.Value = True
End If
Where Sheets(1).Range("t6").Value is where checkbox2 value is being stored.
I have this assigned to a msgbox input so when vbno event is being triggered else is executed.
Cheers.

How to create a fading form on focus lost &got event

I am doing project in vb.net
When i click on button open I opened form with no control box(minimize,maximize etc).set borderStyle to FixedToolWindow
I want to change the opacity of form on got focus & lost focus event.
I also used activated & deactivated event but doesnt working
Private Sub form_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs)HandlesMyBase.Deactivate
Me.Opacity =0
End Sub
Private Sub form_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Opacity = 1
End Sub
To do this you need to use a System.Windows.Forms.Timer. the implementation is very simple:
Have two variables called _fromOpactity and _toOpacity, and a constant OpacityStep = 0.05
on Form Activate or Deactivate set _fromOpacity and _toOpacity and start the timer to fade in/out.
In the timer Elapsed event handler, Increment or Decrement OpacityStep (depending on from/to) until the desired value is reached.
For a full example of how to do this, see this article.
Best regards,
Try 0.01 in your 2nd line. You used 0 and it will hide your form.
Because that when you click in form area , the form_Actived don't run.

NumericUpDown.value is not saved in the User Settings

I have a NumericUpDown Control on a form. In the Application Settings / Properties Binding, for the value parameter, i can't select my USER setting called : Heures (Integer / User).
I tried to save the value by this way :
Private Sub NumericUpDownHeures_Leave(sender As System.Object, e As System.EventArgs) Handles NumericUpDownHeures.Leave
My.Settings.Heures = NumericUpDownHeures.Value
My.Settings.Save()
End Sub
But it's not saved.
No problem for other settings (String / User). But i don't understand why the settings (Integer / User) are not saved.
Please help, Thanks.
As you are putting "NumericUpDown1.Value" you have to set the value at My.Settings.Heures to decimal.
In Form1_Load add:
NumericUpDownHeures.Value = My.Settings.Heures
and add to the event listener for your button or other widget:
My.Settings.Heures = NumericUpDownHeures.Value
I would guess the issue is that the Leave event is not being fired as you expect it to be, especially if the user just clicks the up/down arrows. I suspect that it is only fired when the user actually clicks into the value area, then leaves. You could verify this by debugging to see if your code is ever hit or by showing a simple msgbox from that event.
I think that you will have better luck if you hook the LostFocus or ValueChanged event.
I want to add to this as well for anyone looking at this in the future.
Save your settings as shown already by putting
My.Settings.Heures = NumericUpDownHeures.Value into your ValueChanged event, and then doing reverse in the form load event.
The problem is, this value changed event fires before the form load when you first initialize, so it will keep defaulting to whatever value you have set in the designer because you're overwriting the setting value with the designer value.
To get around this, you need a private/public boolean at the top of your code that is only set to true once your form has loaded (set to true at the bottom of your form_load event), then you can add the condition to the ValueChanged event checking if the form is loaded yet or not. If it is, then change the setting value, if not, then don't.
An example:
Private IsFormLoaded As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NumericUpDown1.Value = My.Settings.SavedNumValue
IsFormLoaded = True
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If IsFormLoaded = False Then Exit Sub
My.Settings.SavedNumValue = NumericUpDown1.Value
End Sub
OR
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If IsFormLoaded Then
My.Settings.SavedNumValue = NumericUpDown1.Value
End If
End Sub