I want to disable the mousewheel to prevent scrolling in the ComboBoxes.
For one ComboBox this works:
Private Sub CmbDienst_MouseWheel(sender As Object, e As MouseEventArgs) Handles CmbDienst.MouseWheel
Dim HMEA As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
HMEA.Handled = True
End Sub
But how can I add this to ALL ComboBoxes? There are a lot of them in the form.
I was looking for something like
Private Sub Combo_Mouse()
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'And then...?
Next
End Sub
Thanks!
It works. The problem I had was that the comboboxes are in several containers, such as Panels and Datagridviews. Then is "me.controls, etc" not enough.
So I finally made this out of it:
In the Form load:
EnumControls(Me)
In the Programm:
Private Sub ComboBoxes_MouseWheel(sender As Object, e As MouseEventArgs)
Dim hmea = DirectCast(e, HandledMouseEventArgs)
hmea.Handled = True
End Sub
Private Sub EnumControls(ByVal ctl As Control)
If ctl.HasChildren Then
For Each c As Control In ctl.Controls
For Each comboBox In c.Controls.OfType(Of ComboBox)()
AddHandler comboBox.MouseWheel, AddressOf ComboBoxes_MouseWheel
Next
EnumControls(c)
Next
End If
End Sub
It works. Suggestions are welcome!
Try this
Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
and in FormLoad
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'You cann acces to ComboBox her by c
AddHandler c.MouseWheel, AddressOf buttonHandler
Next
End Sub
Related
Hi I want to show a Label for hint so if mouse hover then show Label and mouse leave then hide Label.
But if mouse click then show label and don't execute leave event, because leave event means hide mouse. So how can I perform it? My code is here.
Click Event
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
control("set")
End Sub
Hover Event
Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
control("show")
End Sub
Leave Event
Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
control("remove")
End Sub
Control Sub
Public Sub control(ByVal c As String)
If c = "set" Then
Label3.Visible = True
ElseIf c = "show" Then
Label3.Visible = True
ElseIf c = "remove" Then
Label3.Visible = False
End If
End Sub
You can remove the EventHandler when Label2 is clicked:
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub
Not sure what exaclty is the purpose of the control-method...but the code could be reduced to this:
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub
Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
Label3.Visible = True
End Sub
Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
Label3.Visible = False
End Sub
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
Here is my code that does not work. What am I doing wrong?
You're looping over all the Controls contained in Me (probably the Form)
In that collection there is more than just the PictureBoxes so you need to filter to only get those :
(see OfType)
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
i currently have multiple forms (around 30 forms) and i am switching between forms. The Main form (Form1) has 29 buttons and each button will take me to the respective form number (example: button3 = form3, button20=form20, etc).
I understand that I can use the code:
me.hide
form1.show
I want a method to pass the form name dynamically, something along the lines of:
me.controls(FormName).show
Is this possible?
Create a new project with three forms (Form1, Form2, and Form3). Put two buttons on Form1 (named Button2 and Button3), then place the following source code in Form1:
Option Strict On
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Call FormFromButton(DirectCast(sender, Button))
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Call FormFromButton(DirectCast(sender, Button))
End Sub
Public Sub FormFromButton(btn As Button)
Dim i As Integer = CInt(btn.Name.Substring(6)) 'Get number after "button" (6 characters)
Dim f As Form = GetForm("Form" & i.ToString)
f.Show()
f.Activate()
End Sub
Public Function GetForm(formClassName As String) As Form
'see if it is already instanced
For Each f As Form In My.Application.OpenForms
If f.GetType.Name = formClassName Then Return f
Next f
'create new instance
Dim strFullName As String = Me.GetType.Namespace & "." & formClassName
Dim o As Object = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(strFullName)
Dim frm As Form = DirectCast(o, Form)
Return frm
End Function
End Class
Myself created 4 forms one is parent another 3(Form3,Form4,Form5) is child and i was created 3 buttons in parent form and that button Text is Form3,Form4,Form5
Imports System
Imports System.Reflection
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
Dim btn As Button
btn = DirectCast(ctrl, Button)
AddHandler btn.Click, AddressOf Me.buttonclick
End If
Next
End Sub
Private Sub buttonclick(sender As Object, e As EventArgs)
Dim frmname As Button = DirectCast(sender, Button)
Dim frmAssembly As Assembly = Assembly.LoadFile(Application.ExecutablePath)
For Each type As Type In frmAssembly.GetTypes
If type.BaseType = GetType(Form) Then
If (type.Name = frmname.Text) Then
Dim frmshow As Form = DirectCast(frmAssembly.CreateInstance(type.ToString()), Form)
For Each frm As Form In Me.MdiChildren
frm.Close()
Next
frmshow.Show()
End If
End If
Next
End Sub
End Class
Private Sub ButtonClick(ByVal sender As Object, e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim formname As String = "form" & btn.Name(btn.Name.Length - 1)
Dim frm As Form = GetForm(formname)
frm.Show()
End Sub
Private Function GetForm(ByVal Formname As String) As Form
Dim t As Type = Type.GetType(Formname) ', True, True)
If t Is Nothing Then
Dim Fullname As String = Application.ProductName & "." & Formname
t = Type.GetType(Fullname, True, True)
End If
Return CType(Activator.CreateInstance(t), Form)
End Function
Private Sub AddHandlers()
AddHandler Button1.Click, AddressOf ButtonClick
AddHandler Button2.Click, AddressOf ButtonClick
AddHandler Button3.Click, AddressOf ButtonClick
AddHandler Button4.Click, AddressOf ButtonClick
End Sub
Private Sub RemoveHandlers()
RemoveHandler Button1.Click, AddressOf ButtonClick
RemoveHandler Button2.Click, AddressOf ButtonClick
RemoveHandler Button3.Click, AddressOf ButtonClick
RemoveHandler Button4.Click, AddressOf ButtonClick
End Sub
Private Sub Form2_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
AddHandlers()
End Sub
Private Sub Form2_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
RemoveHandlers()
End Sub
How do I check if my mouse cursor enters any component in a panel .
I stared to write this code to check when the mouse enters any check box in my panel then I realised that I had no idea how to actually check if the mouse enters the components I the panel .
Private Sub GenCheck()
For Each CheckBox In datapanel1.Controls
Next
End Sub
how do i go about doing this ?
Edit
I have an Idea but i'm not too sure about it
I could say
Private Sub GenCheck()
Dim cb As CheckBox
For Each cb In datapanel1.Controls
AddHandler cb.MouseEnter, AddressOf cb_MouseEnter
AddHandler cb.MouseLeave, AddressOf cb_MouseLeave
Next
End Sub
Private Sub cb_MouseEnter(sender As Object, e As EventArgs)
End Sub
Private Sub cb_MouseLeave(sender As Object, e As EventArgs)
End Sub
You can use MouseHover to determine when the cursor hovers above the checkbox like this.
Private Sub CheckBox1_MouseHover(sender As Object, e As System.EventArgs) Handles CheckBox1.MouseHover
MsgBox("Mouse over!")
End Sub`
Edit:
I have put a panel on a form with two checkboxes to mimic your requirements, here is what you're looking for:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each pnlCheckBox As CheckBox In Panel1.Controls
AddHandler pnlCheckBox.MouseHover, AddressOf Me.CheckBoxMouseOver
Next
End Sub
Private Sub CheckBoxMouseOver()
MsgBox("Mouse over!")
End Sub
*PERFECT WAY TO ACCOMPLISH THIS *
Dim con As Control
For Each con In datapanel1.Controls
AddHandler con.MouseEnter, AddressOf con_MouseEnter
AddHandler con.MouseLeave, AddressOf con_MouseLeave
Next
End Sub
Private Sub con_MouseEnter(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub
Private Sub con_MouseLeave(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub
Could any body kindly give me code example how to capture datagridview cell keypress event?
Datagridview_keypress does not help.
Thanks
As per Fco Navarro's answer, except that using e.Control does not always work because e is passed in to the EditingControlShowing event ByVal meaning any changes to the control (eg changing the .Text property) are NOT reflected in the DataGridView. If you need to do anything with the actual TextBox control in your event handler, you can use DataGridView1.EditingControl instead of e.Control.
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
txtNumeric = CType(DataGridView1.EditingControl, DataGridViewTextBoxEditingControl)
End Sub
Private Sub txtNumeric_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNumeric.KeyPress
txtNumeric.Text = txtNumeric.Text.ToUpper()
End Sub
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl)
End Sub
Private Sub txtNumeric_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumeric.KeyDown
If (e.KeyData >= Keys.A And e.KeyData <= Keys.Z) Then
e.SuppressKeyPress = True
End If
End Sub
Try this:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If Me.DataGridView1.CurrentCell.ColumnIndex = 0 And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
AddHandler tb.KeyDown, AddressOf TextBox_KeyDown
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
End If
End Sub
Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Space Then
flag = True
End If
End Sub
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.Handled = flag
flag = False
End Sub
Extracted from here.
I use the KeyUp event instead of KeyPress. The trick is to attach the handler event to the ActiveControl property of form when the cell has the active state.
Private Sub grid_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles grid.CellBeginEdit
AddHandler Me.ActiveControl.KeyUp, AddressOf Cell_KeyUp
End Sub
Private Sub Cell_KeyUp(sender As Object, e As KeyEventArgs)
Console.WriteLine(sender.Text) 'content of cell
End Sub
If you want to use also RemoveHandler method, you can add a global variable to the class (using this variable instead of Me.ActiveControl) and call RemoveHandler for example into the CellEndEdit event of DataGridView.
if your using your gridview in asp.net (Website) its not possible. there is no keypress Event.
its possible indeed but you have to use JavaScript to make a postback on every keychanged (Client side). but this is not a nice programming style and should not be used (you are getting rly many postbacks which are slowing down the complete System).
if your using Windows forms: have a look at the answer from sysdragon.
hope this helps a bit.
best regards, noone
the following code work perfectly:
Private WithEvents txtmontant As DataGridViewTextBoxEditingControl
Private Sub DGdivers_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGdivers.EditingControlShowing
If DGdivers.CurrentCell.ColumnIndex = 1 Then
Dim txtmontant = CType(e.Control, DataGridViewTextBoxEditingControl)
AddHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
Else
RemoveHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
End If
End Sub
Private Sub txtmontant_keypress(sender As Object, e As KeyPressEventArgs) Handles txtmontant.KeyPress
If e.KeyChar = vbCr Then
DGdivers.Rows.Add()
Exit Sub
End If
If e.KeyChar = vbBack Then
Exit Sub
End If
If InStr("0123456789.,", e.KeyChar) = 0 Then
e.KeyChar = ""
End If
End Sub