Event fired twice if call a form - vb.net

Can anyone help avoid this ?
At an event handler, I need cal a form but after the form is unloaded the event is fired again.
Private Sub MyHandler(sender As System.Object, e As System.EventArgs) Handles txObjName.Leave
Dim MyVar As Integer = SomeValue
dim myForm as SomeForm
MyForm.ShowDialog()
After myForm be closed, the event is fired again

A quick thought is to try make use of the FormClosing event.
So your code will look like this:
Private _Closing as boolean = False
Private Sub MyHandler(sender As System.Object, e As System.EventArgs) Handles txObjName.Leave
If Not _Closing Then
Dim MyVar As Integer = SomeValue
Dim myForm as SomeForm
MyForm.ShowDialog()
End If
End Sub
Private Sub FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
_Closing = True
End Sub
And you make use of the FormClosing event to toggle the boolean _Closing to true, so the code will not execute when the form is closed :)

Solved changing :
Handles txObjName.Leave
by
Handles txObjName.LostFocus
Thank you all

Related

How can I make Timer do different things based on how it was started

How can I make my Timer do different things depends on what activated it? I've tried using this code
Dim a As Integer = 0
Dim b As Integer = 0
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Timer1.Start
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
Timer1.Start
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
If sender Is Button1 Then
a = a + 1
TextBox1.Text = a
End If
If sender Is Button2 Then
b = b + 1
TextBox2.Text = b
End If
End Sub
but Textbox just add 1 once. This means that the Timer just act one time not continuously like Timer usually do. So is there anything I do wrong there, or i can do something different?.
Another possibly simpler approach, would be to use the Timer.Tag property and use one handler for both buttons:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Timer1.Tag = sender
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
a += 1
If Timer1.Tag Is Button2 Then
TextBox1.Text = a.ToString
End If
If Timer1.Tag Is Button1 Then
TextBox2.Text = a.ToString
End If
End Sub
Since Tag is already of type Object no external casting is needed.
I included the Start function, since I wasn't sure how you're initially starting the timer. If you're doing in a different manner it can be left out of the button event handler
I am not sure what is value of this and this is not, generally speaking, something people do, but if you want to know which action activated timer you need to register it
Private _timer As Timer
Private _activatingControl As Object
Private Sub ActivateTimer(c as Object)
_activatingControl = c ' this is first
_timer.Start()
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
ActivateTimer(sender)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
. . . . . .
System.Diagnostics.WriteLine("timer was activated by" + _activatingControl.ToString())
If DirectCast(_activatingControl, Control).Name = "Button1" Then
. . . .
ElseIf DirectCast(_activatingControl, Control).Name = "Button2" Then
. . . .
End If
End Sub
So, you will always need to activate timer via ActivateTimer. Timer1_Tick(sender As Object will always be Timer itself
I like tinstaafl's Tag property method. A similar approach would be to create a custom timer which inherits from System.Windows.Forms.Timer and has a Button property which can be set in the button's MouseHover event handler.
The timer's Start method only needs to be called once so I moved this to the form's load event handler. I am not sure exactly what you are wanting to achieve. Depending on this, the timer's Start method could be left where it was in the button MouseHover event handler and Timer1.Stop could be called at the end of the timer's tick event handler. This would then increment the value of the counter (a) only once in response to each MouseHover event. Alternatively Timer1.Stop could be called in the buttons' MouseLeave events if you only wanted the counter to increment while the mouse is hovering over the buttons.
Public Class Form1
Private Class CustomTimer
Inherits System.Windows.Forms.Timer
Private m_myButton As Button
Public Property Button() As Button
Get
Return m_myButton
End Get
Set(ByVal value As Button)
m_myButton = value
End Set
End Property
End Class
Private WithEvents Timer1 As New CustomTimer
Private a As Integer
Private Sub Form1_Load(sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub Button_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover, Button2.MouseHover
Timer1.Button = DirectCast(sender, Button)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
a += 1
If Timer1.Button Is Button1 Then
TextBox1.Text = a.ToString
ElseIf Timer1.Button Is Button2 Then
TextBox2.Text = a.ToString
End If
End Sub
End Class

result of a modal form in vb.net

I create a form 'frmX' and i call it as a modal form :
res = frmX.ShowDialog()
This form has 3 buttons, Abort(3), Retry(4) and Ignore(5), but when the form opens, all the buttons on the first click return 2.
I don't know why this occurs--all of the buttons has their property DialogResult right.
*Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
btnIgnorar.DialogResult = DialogResult.Ignore
End Sub
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
btnAbortar.DialogResult = DialogResult.Abort
End Sub
Private Sub btnReintentar_Click(sender As Object, e As EventArgs) Handles btnReintentar.Click
btnReintentar.DialogResult = DialogResult.Retry
End Sub*
Can someone help me?
Could do with seeing a bit more context, but the following should do what I think you want:
Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
DialogResult = DialogResult.Ignore
Close
End Sub
This will close the dialog and return the associated result code to the caller. As to the original code, it seems a bit strange setting the values in the buttons click handlers?
The error comes from the fact that you set the DialogResult of the buttons. You must set the DialogResult of the form !
You actually have more than one option.
Option 1 : Set the Form.DialogResult
Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
Me.DialogResult = DialogResult.Ignore
End Sub
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
Me.DialogResult = DialogResult.Abort
End Sub
Private Sub btnReintentar_Click(sender As Object, e As EventArgs) Handles btnReintentar.Click
Me.DialogResult = DialogResult.Retry
End Sub
Option 2 : Set the Button.DialogResult
Public Sub New()
InitializeComponents()
'Your init code here
'...
'By setting the buttons DialogResults, you don't even have to handle the click events
btnIgnorar.DialogResult = DialogResult.Ignore
btnAbortar.DialogResult = DialogResult.Abort
btnReintentar.DialogResult = DialogResult.Retry
End Sub
'However, if you need to do some stuff before closing the form, you can
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
'Do some stuff
'You don't need the following line, as it will be done implicitly
'Me.DialogResult = DialogResult.Abort
End Sub

Disable Tab Control when pressing Button VB.NET

I would like to ask if how could possibly disable tabs in tabcontrol.
This is what the codes looks like when disable:
Public Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
If e.TabPageIndex = 3 Then
e.Cancel = True
End If
End Sub
This code only disable while you load the form
I was trying to convert a code from c# however it doesn't work as I expected.
See this code:
Public Sub EnableTabs(ByVal Page As TabPage, ByVal bolFlag As Boolean)
EnableControls(Page.Controls, bolFlag)
End Sub
Private Sub EnableControls(ByVal Ctrls As Control.ControlCollection, ByVal bolFlag As Boolean)
For Each Ctrl As Control In Ctrls
Ctrl.Enabled = bolFlag
EnableControls(Ctrl.Controls, bolFlag)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'I have problems with this line
EnableTabs(TabControl1.TabPages(TabControl1.SelectedIndex) = 0, False)
End Sub
Is there anyway that I could possibly disable a tab while clicking a button?
Let me know!
Thanks,
Regards,
Alvin
Try this:
Private Sub Button_Click( sender As Object, e As EventArgs) Handles Button.Click
Dim tabPage As TabPage
For Each tabPage In TabControl1.TabPages
If tabPage.Text ="TabPage1"
tabPage.Enabled =False
End If
Next
End Sub
or
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.TabPages(0).Enabled =false
End Sub
Yet another answer.
At some point if you want to disable a tab - use this code at the appropriate point
TabControl1.TabPages(x).Enabled = False
Where x is the zero-based index of the tab page you want to disable.
When the user clicks on a TabPage, the Selecting event fires for the whole control. Using the e eventargs parameter you can see the index of the TabPage being selected. The code in this event checks to see if it is disabled and if so, cancels the tab click.
Private Sub TabControl1_Selecting(sender As Object, e As TabControlCancelEventArgs) Handles TabControl1.Selecting
If e.TabPage.Enabled = False Then
e.Cancel = True
End If
End Sub
I already answered it. Anyhow, I would like to share it for you guys.
I just change the code from:
EnableTabs(TabControl1.TabPages(TabControl1.SelectedIndex) = 0, False)
to:
EnableTabs(TabControl1.TabPages(1), False)
This code only the contain of tab not by disable while selecting/clicking the tab header. I think I just use this one for now. If you have other source of code that is useful enough. Just leave on the answer section below. I loved to hear them all.
Thanks anyway.
Regards,
Alvin
I have already my own answer based on it. And I used this code right now, for example I have 3 tabs with 0-2 index respectively.
Public Sub Tab0Flag As Boolean
Public Sub Tab1Flag As Boolean
Public Sub Tab2Flag As Boolean
Public Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
If e.TabPageIndex = 0 Then
e.Cancel = Tab0Flag
End If
If e.TabPageIndex = 1 Then
e.Cancel = Tab1Flag
End If
If e.TabPageIndex = 2 Then
e.Cancel = Tab2Flag
End If
End Sub
Private Sub EnableTabs(ByVal Tab0 As Boolean, ByVal Tab1 As Boolean, ByVal Tab2 As Boolean)
Tab0Flag = Tab0
Tab1Flag = Tab1
Tab2Flag = Tab2
End Sub
Private Sub frmG_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'I'll Only Disable the 2nd tab
EnableTabs(False, True, False)
End Sub

how to run a function/sub after loading the form window in VB?

I have a function that gets User ID from USB badge reader, used to log in an application.
when I run the app, the log in window does not appear until I swipe the tag.
I need to know if it`s possible to load the windows, then to start running the function that gets the data from the USB.
Thanks :)
Private Sub SerialPort1_DataReceived()
'Threading.Thread.SpinWait(1000)
OpenPort()
If SerialPort1.IsOpen() Then
byteEnd = SerialPort1.NewLine.ToCharArray
'read entire string until .Newline
readBuffer = SerialPort1.ReadLine()
readBuffer = readBuffer.Remove(0, 1)
readBuffer = readBuffer.Remove(8, 1)
WWIDTextBox.AppendText(readBuffer)
End If
End Sub
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
SerialPort1_DataReceived()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'SerialPort1_DataReceived()
End Sub
The problem is that you are calling the ReadLine method, which is a blocking (synchronous) method. In other words, when you call it, the method does not return the value until it has the value to return. Because of that, it stops execution on the current thread until a complete line is read (when the badge is swiped). Since you are on the UI thread when you call it, it will lock up the UI until the badge is swiped.
Instead of calling your SerialPort1_DataReceived method from the UI thread, you can do the work from a different thread. The easiest way to do that is to drag a BackgroundWorker component onto your form in the designer. Then you can add code like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
OpenPort()
If SerialPort1.IsOpen() Then
byteEnd = SerialPort1.NewLine.ToCharArray
Dim readBuffer As String = SerialPort1.ReadLine()
readBuffer = readBuffer.Remove(0, 1)
readBuffer = readBuffer.Remove(8, 1)
e.Result = readBuffer
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
WWIDTextBox.AppendText(CStr(e.Result))
End Sub
Working on VS2013, I came across the same issue, I needed to to a datagridview refresh (colors in the gridrows). This worked for me.
Sub MyForm_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
'do action...
End If
End Sub
Try Form Activated Event
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
'Call your function here
End Sub
It call the function After the Form Loads...
Private Sub loadCombo()
Dim sqlconn As New OleDb.OleDbConnection
Dim connString As String
connString = ""
Dim access As String
access = "select slno from atable"
Dim DataTab As New DataTable
Dim DataAdap As New OleDbDataAdapter(access, connString)
DataAdap.Fill(DataTab)
ComboBox1.DataSource = DataTab
ComboBox1.DisplayMember = "slno"
End Sub

To get the commandargument in rowdatabound event in gridview

Following is code for buttonclick in gridview.
Protected Sub lnkChangeStatus_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim gvr As GridViewRow = TryCast(DirectCast(sender, LinkButton).Parent.Parent, GridViewRow)
Dim lngProfileId As Long = Convert.ToInt64(gvwBusinessProfiles.DataKeys(gvr.RowIndex).Value)
End Sub
I want to get commandargument of lnkChangeStatus in its click.Is there any way
I guess the syntax for the eventhandler (Command event as against Click) should be as below.
Protected Sub lnkChangeStatus_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
.....
End Sub
You can use e.CommandName or e.CommandArgument inside the code block.
Refer this