I have a problem with the TextBox1.TextChanged event.
My code :
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
MsgBox("txt was changed")
TextBox1.Clear()
End Sub
The problem is that the MsgBox is shown twice, but I want to show it just one time and clear the TextBox. How can i do that?
Two ways:
Temporarily remove the handler to prevent the event firing again:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
MsgBox("txt was changed")
RemoveHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
TextBox1.Clear()
AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
End Sub
or
Create a field to check if the event is originating from itself:
Dim textBoxAlreadyChanging As boolean = False
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If Not textBoxAlreadyChanging Then
MsgBox("txt was changed")
textBoxAlreadyChanging = True
TextBox1.Clear()
textBoxAlreadyChanging = False
End If
End Sub
Related
I tried to update a DataGridView every 1 second.
I used a timer, but I see the user interface for the form become very slow.
I used a BackgroundWorker, but I didn't get any data in the DataGridView
Here is the DataGridView code:
Private Sub OrderBookOffersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Me.OrderBookOfferBidsTableAdapter.Fill(Me.BinanceDataSet.OrderBookOfferBids)
Me.OrderBookOfferAsksTableAdapter.Fill(Me.BinanceDataSet.OrderBookOfferAsks)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
BackgroundWorker1.RunWorkerAsync()
End Sub
Here is the timer code:
Me.OrderBookOfferBidsTableAdapter.Fill(Me.BinanceDataSet.OrderBookOfferBids)
Me.OrderBookOfferAsksTableAdapter.Fill(Me.BinanceDataSet.OrderBookOfferAsks)
So I've been working on this project to autorun on my flash drive, whenever it gets plugged it. It just displays my contact info and a little message, so I can get it returned.
Because I want people to actually read the message, the close button is disabled. You have to check a little box, and then hit the "OK" button.
Of course, I don't want to do this whenever I plug in my own flash drive. I'm trying to make a keyboard shortcut to close it automatically. I saw this post here but it didn't work for me. Here's the code from that post:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub
Any help is very appreciated. Thanks!
Also, if you think my code is sloppy and want to clean it up, here it is.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
End
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
ButtonOK.Enabled = True
Else
ButtonOK.Enabled = False
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
End
End If
End Sub
End Class
Firstly, NEVER EVER use End. That is just plain bad under any circumstances.
What you should be doing is testing the Checked value of your CheckBox in the FormClosing event handler and cancelling if and only if it's not checked. Logically, you should then check the CheckBox and call Close on the form when you detect that desired key combination.
Actually, I'd probably not call Close either, but rather call PerformClick on the Button. That way, the key combination is guaranteed to do exactly the same thing as clicking the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
Close()
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ButtonOK.Enabled = CheckBox1.Checked
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
CheckBox1.Checked = True
ButtonOK.PerformClick()
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not CheckBox1.Checked
End Sub
So i have a form as below:
Public Class IPADSOFT
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
IPADSOFTTS.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
Me.Hide()
End Sub
End Class
which has 3 checkboxes labeled IPADSOFTBOX1, IPADSOFTBOX2, IPADSOFTBOX3
So... i have another form as follows:
Public Class IPADSOFTTS
Private Sub onload()
If IPADSOFT.IPADSOFTBOX1.Checked Then
Button1.Visible = True
Button3.Visible = True
Button5.Visible = True
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
IPADSOFT.Hide()
Me.Hide()
End Sub
End Class
Now the idea is that all the buttons on that second form are set to visible-false and i want the page to check which checkboxes are checked on the last form and then make the required buttons on this form visible... but it isnt working
What am i doing wrong?? i apologise im very very new to vb.net
Open the second form with this
Dim newForm As New IPADSOFTTS With
{.MainForm = Me}
newForm .Show()
Set Public MainForm As IPADSOFT below the Public Class of second form
Then use in the Load event
if MainForm.IPADSOFTBOX1.Checked = true then
'Do whatever
End if
I use the below code to verify if the text into a TextBox was changed and ask for saving changes:
Private TBoxTxtBefore As String = String.Empty
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Try
TBoxTxtBefore = CType(sender, TextBox).Text
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
If CType(sender, TextBox).Text <> TBoxTxtBefore Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
But when I click a button (for example button1) while the cursor is inside TextBox1, only TextBox1.Leave event was raised.
How can I have Button?.click event raise after TextBox1.Leave event?
How can I have button1.click event raise after TextBox1.Leave event?
If TextBox1 has the focus and you click a button the leave event will fire before the click event. To test click in Texbox1, then click Button1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Try to clarify your question please.
edit: This recreates the problem I think you are having,
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
MessageBox.Show("FOO")
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Did some searching and found that the Message box causes pending focus events to be lost.
Thanks to dbasnett's hints I was able to get the name of the control that fired the Leave event, so to call it (if it's a button) after the Message Box was showed:
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
Dim FiringCtrl As Control = Me.Activecontrol
If CType(sender, TextBox).Text <> TBoxTxtBefore _
AndAlso FiringCtrl.Name <> "Btn_SaveChanges" Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
If TypeOf FiringCtrl Is Button Then
DirectCast(FiringCtrl, Button).PerformClick()
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Anyway Plutonix showed me a better solution here
You can call multiple events one after the another. Your TextBox1_Leave event could trigger Button1_click event.
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
'Take the control to another sub. Pass the sender control and the event details as arguments
Call Button1_Click(sender,e)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Do your stuff here
End Sub
Hope this helps.
Can you help me with this, my code doesn't have error but when I click the button to go back to the previous group box it doesn't work :( Idk why can you help me guys figure it out?
The app has login form when you login it directs you to the another form that you can choose categories then if I select a category example I chose gadgets it redirects me to the gadgets groupbox then if I try to go back it doesn't go back in the categories page
The button2.click, button6.click and button3.click are the back buttons
Here's my code
Public Class Form3
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
GroupBox2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sbutton As DialogResult
sbutton = MessageBox.Show("Do you want to logout now?", "Logging out", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If sbutton = DialogResult.Yes Then
Form1.Show()
Me.Hide()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
GroupBox1.Show()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
GroupBox3.Show()
End Sub
Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
GroupBox4.Show()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
MsgBox("Message was sent!", MsgBoxStyle.Information)
TextBox1.Clear()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
GroupBox2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
MsgBox("Message was sent!", MsgBoxStyle.Information)
TextBox2.Clear()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
GroupBox2.Show()
GroupBox3.Hide()
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GroupBox2.Hide()
GroupBox3.Hide()
GroupBox4.Hide()
End Sub
End Class
Umm did you type this yourself or generate it from a UI? Shouldn't it be button_OnClick not button_click?