Any buddy please correct my script, TextBox variable working done but MaskTextBox Returning error in this EnterEvent.
I want to use one Function of event-handling for TextBox and MaskTextBox
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles OrgNameTextBox.Enter, AddressTextBox.Enter, ContactNumMaskedTextBox.Enter
Dim Tb As TextBox = CType(sender, TextBox)
Dim Mtb As MaskedTextBox = CType(sender, MaskedTextBox)
If Type = MASKTEXTBOX Then
MTb.BackColor = Color.Yellow
MTb.ForeColor = Color.Black
ElseIf Type = TextBox Then
Tb.BackColor = Color.Yellow
Tb.ForeColor = Color.Black
End If
End Sub
The event handler cannot work because there is always type conversion error
The correct version is
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles OrgNameTextBox.Enter, AddressTextBox.Enter, ContactNumMaskedTextBox.Enter
If TypeOf sender Is MaskedTextBox Then
Dim Mtb As MaskedTextBox = CType(sender, MaskedTextBox)
Mtb.BackColor = Color.Yellow
Mtb.ForeColor = Color.Black
ElseIf TypeOf sender Is TextBox Then
Dim Tb As TextBox = CType(sender, TextBox)
Tb.BackColor = Color.Yellow
Tb.ForeColor = Color.Black
End If
End Sub
Better is using common ancestor of both controls TextBoxBase
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles OrgNameTextBox.Enter, AddressTextBox.Enter, ContactNumMaskedTextBox.Enter
Dim bt As TextBoxBase = TryCast(sender, TextBoxBase)
If bt IsNot Nothing Then
bt.BackColor = Color.Yellow
bt.ForeColor = Color.Black
End If
End Sub
i am used Example of #Jimi and its Working...
Private Sub MaskedTextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrgNameTextBox.Enter, OrgNameTextBox.Leave
CType(sender, Control).BackColor = Color.Yellow
CType(sender, Control).ForeColor = Color.Black
End Sub
Related
I have 28 CheckBoxes in a windows form. Each box has PictureBox above it. When the user clicks on a PictureBox, I want to change the BackColor of the PictureBox to green, and make its corresponding CheckBox.Checked = True
The code I am using:
Private Sub PictureBox1_Click
PictureBox1.BackColor = Color. Green
CheckBox1.Checked = true
For 28 it will be a lengthy process. Is there any easy solution?
Programmatically add MouseClick even handlers to all your PictureBoxes in Form_Load. The event handler will parse the sender (PictureBox) and find the CheckBox based on the fact that the corresponding controls' names end in the same index. Remove the handlers when the form closes.
Private pictureBoxPrefix As String = "PictureBox"
Private checkBoxPrefix As String = "CheckBox"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pb In Me.Controls.OfType(Of PictureBox).Where(Function(p) p.Name.Contains(pictureBoxPrefix))
AddHandler pb.MouseClick, AddressOf PictureBox_MouseClick
Next
End Sub
Private Sub PictureBox_MouseClick(sender As Object, e As MouseEventArgs)
Dim index = Integer.Parse(pb.Name.Replace(pictureBoxPrefix, ""))
Dim pb = CType(sender, PictureBox)
Dim cb = CType(Me.Controls.Find($"{checkBoxPrefix}{index}", True).First(), CheckBox)
pb.BackColor = Color.Green
cb.Checked = True
End Sub
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
For Each pb In Me.Controls.OfType(Of PictureBox).Where(Function(p) p.Name.Contains(pictureBoxPrefix))
RemoveHandler pb.MouseClick, AddressOf PictureBox_MouseClick
Next
End Sub
In the Load() event of your form, use Controls.Find() to get a reference to both the PictureBoxes and CheckBoxes. Store the CheckBox reference in the Tag() property of each PictureBox. Wire up the Click() event of you PB. In that event, change the color of the PB, then retrieve the CheckBox from the Tag() property and check the box as well:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 1 To 28
Dim PB As PictureBox = Me.Controls.Find("PictureBox" & i, True).FirstOrDefault
Dim CB As CheckBox = Me.Controls.Find("CheckBox" & i, True).FirstOrDefault
If Not IsNothing(PB) AndAlso Not IsNothing(CB) Then
PB.Tag = CB
CB.Tag = PB
AddHandler PB.Click, AddressOf PB_Click
AddHandler CB.CheckedChanged, AddressOf CB_CheckedChanged
End If
Next
End Sub
Private Sub PB_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim cb As CheckBox = DirectCast(pb.Tag, CheckBox)
If pb.BackColor.Equals(Color.Green) Then
pb.BackColor = Color.Empty
cb.Checked = False
Else
pb.BackColor = Color.Green
cb.Checked = True
End If
End Sub
Private Sub CB_CheckedChanged(sender As Object, e As EventArgs)
Dim cb As CheckBox = DirectCast(sender, CheckBox)
Dim pb As PictureBox = DirectCast(cb.Tag, PictureBox)
pb.BackColor = If(cb.Checked, Color.Green, Color.Empty)
End Sub
I have a databound ListItems combobox with AutoComplete.SuggestAppend
and would like to navigate out of the combobox to different controls using the up/down arrow keys rather than scrolling the items.
The issue is that if the text isn't completed the suggested text remains highlighted while the next control has focus.
Link to image example
Here is some code showing a simple example of what I am doing
Public Class Form1
Dim PreventCboBoxChanging As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = New List(Of String)(New String() {10, 11, 20, 30})
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End Sub
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If PreventCboBoxChanging = True Then
e.Handled = True
End If
PreventCboBoxChanging = False
End Sub
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
PreventCboBoxChanging = True
TextBox1.Select()
End If
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.Select()
End If
End Sub
Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
Dim index As Integer = sender.FindString(sender.Text)
If index > -1 Then
sender.SelectedIndex = index
Else
e.Cancel = True
Me.Text = ""
Beep()
End If
End Sub
End Class
Is there any way to deselect the text?
I found the solution in another thread. Needed to turn off the Combobox AutoComplete Mode, change focus then reenable SuggestAppend mode under the PreviewKeyDown event.
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.AutoCompleteMode = AutoCompleteMode.None
TextBox1.Select()
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End If
End Sub
This should make it:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.SelectionLength = 0
End Sub
With the help of the code below (by LarsTech - drag and detach tabpages
) I was able to detach a tabpage and place it into a new form. But when I close that form the dragged tabpage doesn't return to its original position.
If anyone out there can help me program this, it'll be great!
Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
If (e.Button = MouseButtons.Left) Then
TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
End If
End Sub
Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
e.UseDefaultCursors = False
End Sub
Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
If Control.MouseButtons <> MouseButtons.Left Then
e.Action = DragAction.Cancel
Dim f As New Form
f.Size = New Size(400, 300)
f.StartPosition = FormStartPosition.Manual
f.Location = MousePosition
Dim tc As New TabControl
tc.Dock = DockStyle.Fill
tc.TabPages.Add(TabControl1.SelectedTab)
f.Controls.Add(tc)
f.Show()
Me.Cursor = Cursors.Default
Else
e.Action = DragAction.Continue
Me.Cursor = Cursors.Help
End If
End Sub
I've been able to produce the code which returns the tabpage to its original place when the form closes, however the page doesn't return to the original index position. Below is the updated code:
Public f As Form
Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
If (e.Button = MouseButtons.Left) Then
TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
End If
End Sub
Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
e.UseDefaultCursors = False
End Sub
Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
f = New Form
If Control.MouseButtons <> MouseButtons.Left Then
e.Action = DragAction.Cancel
AddHandler f.FormClosing, AddressOf ClosingDraggableWindow_EventHandler
f.Size = New Size(400, 300)
f.Name = TabControl1.SelectedTab.Text
f.TabIndex = TabControl1.SelectedTab.TabIndex
f.StartPosition = FormStartPosition.Manual
f.Location = MousePosition
Dim tc As New TabControl()
tc.Dock = DockStyle.Fill
tc.TabPages.Add(TabControl1.SelectedTab)
f.Controls.Add(tc)
f.Show()
End If
End Sub
Sub ClosingDraggableWindow_EventHandler()
Dim tbp As New TabPage()
tbp.Text = f.Name
Dim tbc As TabControl = f.Controls(0)
Dim tbp2 As TabPage = tbc.TabPages(0)
TabControl1.TabPages.Insert(f.TabIndex + 1, tbp2)
End Sub
Changing this sentence
f.TabIndex = TabControl1.SelectedTab.TabIndex
to
f.TabIndex = TabControl1.SelectedIndex
it works fine.
TabIndex represents the order into the form but not the index into the TabControl1.
This is what I have for clicking on one label so it could change color, but I would like to know how to make it so the next labels will also turn red only if clicked.
In
In total, I have 48 labels If Seat1.BackColor = Color.White Then
Seat1.BackColor = Color.Red
Else
Seat1.BackColor = Color.White
End If
You can have the same sub routine handle all of the seat label click events by casting the sender parameter as a Label:
Private Sub HandleSeatClick(sender As Object, e As EventArgs) Handles Seat1.Click, Seat2.Click, Seat3.Click
Dim lblTarget As Label = CType(sender, Label)
If lblTarget.BackColor = Color.White Then
lblTarget.BackColor = Color.Red
Else
lblTarget.BackColor = Color.White
End If
End Sub
If all of your seat labels are named in the same way (example = Seat5, Seat6, Seat7, ... , Seat48) then you can leverage AddHandler so you don't have to wire up the 48 labels with the Handles in the HandleSeatClick routine definition:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim intCursor As Integer = 1
Do Until intCursor = 48
Dim lblTarget As Label = CType(Me.Controls.Find("Seat" & intCursor.ToString(), False).First(), Label)
AddHandler lblTarget.Click, AddressOf HandleSeatClick
intCursor += 1
Loop
End Sub
Following is my code:
Public Class Form1
Private DT_LocalTransactionList As DataTable
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
DT_LocalTransactionList = New DataTable
DT_LocalTransactionList.Columns.Add("TransactionName")
DT_LocalTransactionList.Columns.Add("TransactionType")
For iVisible As Integer = 0 To 5
DT_LocalTransactionList.Rows.Add()
DT_LocalTransactionList.Rows(iVisible).Item("TransactionName") = "Name " & iVisible
DT_LocalTransactionList.Rows(iVisible).Item("TransactionType") = "Add " & iVisible
Next
If e.ColumnIndex = colName.Index Then
Dim dgvCbo As New DataGridViewComboBoxCell
dgvCbo = TryCast(DataGridView1(colName.Index, e.RowIndex), DataGridViewComboBoxCell)
dgvCbo.DataSource = DT_LocalTransactionList
dgvCbo.DisplayMember = "TransactionName"
dgvCbo.ValueMember = "TransactionType"
End If
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Select Case Me.DataGridView1.CurrentCell.ColumnIndex
Case colName.Index
If TypeOf e.Control Is ComboBox Then
Dim cb As ComboBox = TryCast(e.Control, ComboBox)
cb.DropDownStyle = ComboBoxStyle.DropDown
cb.AutoCompleteSource = AutoCompleteSource.ListItems
cb.AutoCompleteMode = AutoCompleteMode.Suggest
RemoveHandler cb.DrawItem, AddressOf GridCombo_DrawItem
RemoveHandler cb.DropDownClosed, AddressOf cbDropDownClosed
RemoveHandler cb.Validating, AddressOf GridCombo_Validating
RemoveHandler cb.KeyDown, AddressOf GridCombo_KeyDown
AddHandler cb.DrawItem, AddressOf GridCombo_DrawItem
cb.DrawMode = DrawMode.OwnerDrawFixed
AddHandler cb.DropDownClosed, AddressOf cbDropDownClosed
AddHandler cb.Validating, AddressOf GridCombo_Validating
AddHandler cb.KeyDown, AddressOf GridCombo_KeyDown
End If
End Select
End Sub
Private Sub GridCombo_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim text As String = sender.GetItemText(sender.Items(e.Index))
e.DrawBackground()
Using br As New SolidBrush(e.ForeColor)
e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds)
End Using
e.DrawFocusRectangle()
End Sub
Private Sub cbDropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Private Sub GridCombo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim cb As ComboBox = TryCast(sender, ComboBox)
If Not IsItemExistInList(cb) Then
e.Cancel = True
End If
End Sub
Private Sub GridCombo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim cb As ComboBox = TryCast(sender, ComboBox)
cb.Refresh()
If e.KeyCode = Keys.Enter Then
If IsItemExistInList(cb) Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
End If
End Sub
Public Function IsItemExistInList(ByRef cboCombo As ComboBox) As Boolean
Dim blnContinue As Boolean
Dim intCount As Integer
blnContinue = False
If cboCombo.Text.Trim = "" Then
blnContinue = True
End If
If blnContinue = False Then
For intCount = 0 To cboCombo.Items.Count - 1 And blnContinue = False
If cboCombo.Text.Trim = cboCombo.GetItemText(cboCombo.Items(intCount)).Trim Then
blnContinue = True
End If
Next
End If
IsItemExistInList = blnContinue
End Function
End Class
When I type 'n' in the combobox control of datagridview then it shows all the values from Autocomplete feature, then selecting one and pressing tab it does not show the selected value in that field. When second time doing the same thing it shows correct selection there. How to implement that thing in first time?
You should look at CommitEdit method of DataGridView once the editing control is validating as per the criteria specified.
Private Sub GridCombo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim cb As ComboBox = TryCast(sender, ComboBox)
If Not IsItemExistInList(cb) Then
e.Cancel = True
Else
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
End If
End Sub
In this case you can ignore the value returned by CommitEdit method since the provided sample only allows values from the collection. So if value does not exist cell will not be populated.
Recommended read
I have also experienced while working with DataGridViewComboBoxColumn that it require 2-3 click in order to edit value of the cell. I would suggest to use:
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
at some starting point. Above code itself elaborates what it does.