Make Buttons clickable once in VB.net - vb.net

I'm Trying To make A button Only clickable once in Vb
I was thinking of this code
Sub B64_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B64.Click
Dim BClick As Integer = 0
If BClick = 0 Then
BClick = 1
'Other instructions
End If
Any Other Ideas!
Also Can I do something so that the button will make a sound when it is clicked ?!
Any Other Ideas!
Thank u

In your click event you can do:
B64.Enabled = False
You can also play a .WAV file on click:
Dim player As New System.Media.SoundPlayer()
player.SoundLocation = path
player.Load()
player.Play()

Just Disable Button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Enabled = False
'Do Stuff....
'& Enable it if you want
Button1.Enabled = True
End Sub
you can disable button or any control with this code
by using Button1_Click(Button1, Nothing) or Button1_Click(Button2, Nothing)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim Element As Button = DirectCast(sender, Button)
Element.Enabled = False
'Do Stuff....
'if you want delay use: Threading.Thread.Sleep(500)
Element.Enabled = True
End Sub

Related

Autocomplete text box. Selecting a value

good day!
there is a textbox autocomplete code
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim day_source As New AutoCompleteStringCollection()
day_source.AddRange(System.Enum.GetNames(GetType(DayOfWeek)))
PrepareTextBox(TextBox1, day_source, AutoCompleteMode.SuggestAppend)
End Sub
Private Sub PrepareTextBox(ByVal txt As TextBox, ByVal source As AutoCompleteStringCollection, ByVal mode As AutoCompleteMode)
txt.AutoCompleteSource = AutoCompleteSource.CustomSource
txt.AutoCompleteCustomSource = source
txt.AutoCompleteMode = mode
End Sub
Tell me how you can display a message with the name of the selected value when you select a value?
The KeyDown event fires with e.KeyCode = 13 when the user presses Enter or Double Clicks on an item in the list.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = 13 Then
MsgBox(TextBox1.Text)
End If
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

VB.Net multiple process with checkbox

I want to know the code to the following illustration.
i have one form with some checkboxs and one button,
screen is here
i've try with this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True And CheckBox4.Checked = True Then
'when the button is clicked will be the process for moving images
'Like
System.IO.File.Copy(Application.StartupPath + "\File\Pic1.jpg", "D:\File\Pic1.jpg")
End If
End Sub
I was tired with that code, is there a shorter coding ?
for example, if the checkbox1.checked = true and another checkbox not checked then only moving one pict
If I understand the question, you want to copy pictures 1 to 4 if the checkboxes 1 to 4 are checked.
Try this:
Dim SourcePath As string = Application.StartupPath + "\File\"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CopyFile(CheckBox1, "Pic1.jpg")
CopyFile(CheckBox2, "Pic2.jpg")
CopyFile(CheckBox3, "Pic3.jpg")
CopyFile(CheckBox4, "Pic4.jpg")
End Sub
Private Sub CopyFile(CB As CheckBox, FileName As String)
If CB.Checked Then
System.IO.File.Copy(SourcePath + FileName, "D:\File\" + FileName)
End If
End Sub

How Do I Force An Action When A Dialog Box Closes In VB.Net?

I am wondering how to force my main gui to update when a dialog box such as a SaveFileDialog box closes. I have tried Main.LostFocus, Main.GotFocus, Main.Enter, Main.MouseEnter, Main.MouseLeave, and Main.MouseMove, but no matter what function I try I can never get the result I am looking for.
The dialog box is opened when a picture is clicked. The picture changes when it is clicked and again when the icon_new.MouseUp is called. The problem is that it acts correctly until the dialog is closed. At this point the picture changes back to the image it had when the mouse was over it.
Here is what the picture does regularly:
Private Sub icon_new_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseDown
icon_new.Image = My.Resources.NewMapClick
End Sub
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
icon_new.Image = My.Resources.NewMapHover
End Sub
Private Sub icon_new_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles icon_new.MouseEnter
icon_new.Image = My.Resources.NewMapHover
End Sub
Private Sub icon_new_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles icon_new.MouseLeave
icon_new.Image = My.Resources.NewMapDefault
End Sub
This works until the dialog box is closed, at which point it the image becomes NewMapHover when it should be NewMapDefault, because the mouse is no longer within the bounds of the picture. In the calls such as Main.LostFocus, Main.GotFocus, or Main.Whatever I have icon_new.image = My.Resources.NewMapDefault, but even if this call triggers, the image ends up as NewMapHover. I'm not sure why this is happening or how to fix it. Any help is greatly appreciated.
EDIT: This is the click event that calls the dialog_box
Private Sub icon_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icon_new.Click
If file_created = True Then
save()
Else 'file_created = false'
SaveWindow.FileName = txt_name.Text
If SaveWindow.ShowDialog() = DialogResult.OK Then
file_path = SaveWindow.FileName
End If
file_created = True
End If
save()
new_file()
End Sub
If the file hasn't been saved, then a dialog box opens, prompting the user to save the file. I've also toyed with a MsgBox() that has Yes, No, and Cancel, prompts, but for simplicity I took it out because the results were the same and one third of the time the SaveFile dialog will come up anyway.
Try doubleclicking your SaveFileDialog, which will open up the SaveFileDialog_FileOk event code, and then put the same code there as in the MouseLeave event.
This event will fire when the SaveFileDialog is about to close after you've pressed the "Save" button.
EDIT:
You can try doing this in your click event:
Dim DResult As DialogResult
DResult = SaveFileDialog1.ShowDialog()
If DResult = System.Windows.Forms.DialogResult.OK Then
'Code for when you press the save button, and when the image should change
ElseIf DResult = System.Windows.Forms.DialogResult.Cancel Then
'Code for image change...
End If
Try
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
Dim pnt As Point
Dim rect As Rectangle
rect.X = icon_new.Location.X
rect.Y = icon_new.Location.Y
rect.Width = icon_new.Width
rect.Height = icon_new.Height
pnt = PointToClient(Cursor.Position)
If Not rect.Contains(pnt) Then
Return
End If
icon_new.Image = My.Resources.NewMapHover
End Sub
Valter
This is the code I originally had.
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
icon_new.Image = My.Resources.NewMapHover
End Sub
I added the variable dialog_open as a boolean and set it to true whenever I called SaveFile.ShowDialog(). I then changed my MouseUp event to this:
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
icon_new.Image = My.Resources.NewMapHover
If dialog_open = True Then
icon_new.Image = My.Resources.NewMapDefault
dialog_open = False
End If
End Sub
Thanks to #valter and #Kevin Hogg for suggesting I edit the MouseUp event.

vb2010 Getting Button Name on MouseDown Event

How do I capture the name of a button so I can use it in another form to query a database. I am new to vb and still at the very early learning stage so any help would be greatfully appreciated.
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
frmRacks
Here is where I need to capture name to query database
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim name As String = DirectCast(sender, Button).Name
End Sub
There are many ways to do this, one is to declare public field/variable in frmRack, another is using ShowDialog instead of Show:
I'll go with the first one (public) first:
Public buttonName as String
And in the button click from your frmMain you pass the value like:
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.buttonName = "btnA" ' Or you could use DirectCast as proposed by dbasnett
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
And then in Loading your frmRacks you have now the option of assigning button Name, like:
Private Sub frmRacks_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim query as String = "SELECT * FROM " + buttonName 'This is only an example you could make your own here
End Sub
It should be MouseButtons.Left instead of Windows.Forms.MouseButtons.Left
If e.Button = MouseButtons.Left Then
MsgBox("Left Button Clicked") 'OR WHATEVER YOU WANT IT TO DO?!
End If