Disable Tab Control when pressing Button VB.NET - 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

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

Make Buttons clickable once in 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

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

Latency picking up click event

I am having a little problem with latency when I check a checkbox on and try to drag and drop. When I select one checkbox and try to move it over it won't move. If I have click that checkbox and click on a different row then try to move it will work. It works the same no matter how many I check it won't get the newest row without clicking somewhere else first. Do I need to add another event to handle or pick up that the box now has been checked?
Private Sub datagridview_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseDown
mouseDownPosition = e.Location
End Sub
Private Sub datagridview_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseMove
If CheckMouseMovement(sender, datagridview, e) Then
listofBuilds = New List(Of Build)
For Each row As DataGridViewRow In dataGridView.Rows
If Convert.ToBoolean(row.Cells.Item(0).Value) Then
Dim t As Build = DirectCast(row.DataBoundItem, Build)
listofBuilds.Add(t)
End If
Next
If listofBuilds.Count > 0 Then
dataGridView.EndEdit()
dataGridView.DoDragDrop(sender, dropEffect)
End If
End If
End Sub
Private Sub TabControl_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragEnter
e.Effect = DragDropEffects.All
End Sub
Private Sub TabControl_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragDrop
Dim DropPage As TabPage = GetTabPageByTab(TabControl.PointToClient(New Point(e.X, e.Y)))
If DropPage IsNot TabControl.SelectedTab Then
If DropPage Is Page1 Then
If DropPage Is Page2 Then
If DropPage Is Page3 Then
//etc
End If
End If
End Sub
Private Function GetTabPageByTab(ByVal point As Point) As TabPage
For i As Integer = 0 To TabControl.TabPages.Count - 1
If TabControl.GetTabRect(i).Contains(point) Then
Return TabControl.TabPages.Item(i)
End If
Next
Return Nothing
End Function
Adding a call to datagridview.EndEdit() at the beginning of the datagridview_MouseMove method will commit the current edit operation and update the source data so that you can see the updated value in your code.

Drag drop a usercontrol in vb.net

I have, what should be , a very simple problem, but now I have used 5 hours without results.
I have a usercontrol, UserControl1, which I want to drag and drop on my form, Form1.
That’s it. It should be simple, but I have googled for hours without results. Does anybody have a sample code to fix this?
I dont know what a user control is (I'm still learning) but I found something that might help.
In this code, add two TextBox controls to a form and set the AllowDrop property of the second TextBox control to True.
Then use this code to enable drag and drop
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
I hope that you can use this for a usercontrol. Good luck and comment!
Here is the code, I have used, to get it work.
Now I have a form, Form1, and a usercontrol, Usercontrol1. To drag the usercontrol, I inserted a panel in the top of the usercontrol, and only if the user pressed the panel (panel1), the control should to move - like normal windows forms.
Public Class UserControl1
Shared mypositionX As Integer
Shared mypositionY As Integer
Shared mBlnFormDragging As Boolean
Shared drawBeginX As Integer
Shared drawBeginY As Integer
Shared drawing As Boolean
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If mBlnFormDragging = True Then
Dim position As Point = Form1.PointToClient(MousePosition)
Me.Location = New Point(position)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = False
Dim position As Point = Form1.PointToClient(MousePosition)
Location = New Point(position)
End Sub
Public Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
'Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = True
End Sub