How to avoid triggering MouseLeave event when hovering over a child control? - vb.net

I am trying to make it so if you hover into a panel, it will change its color, and when you hover into a label, the panel will not change its color or go back to default, let me show pictures of what I am trying to explain:
If you didn't hover into anything, it would be at its default state:
If you hovered into it, panel color will change:
Then, if you hover into the label, panel color shouldn't change:
But instead, what it does is if you hover into the label, the panel color goes back to what its default was. This is my code:
Private Sub Panel13_MouseEnter(sender As Object, e As EventArgs) Handles Panel13.MouseEnter
Panel13.BackColor = Color.DarkGreen
End Sub
Private Sub Panel13_MouseLeave(sender As Object, e As EventArgs) Handles Panel13.MouseLeave
Panel13.BackColor = Color.LimeGreen
End Sub
How can I fix this problem? Is there any way to make the code clean or do I have to copy and paste the code and replace words?

You may use the GetChildAtPoint method to check if the mouse cursor is over a child control.
Replace the code in your MouseLeave event handler with the following:
Dim childControl = Panel13.GetChildAtPoint(Panel13.PointToClient(Cursor.Position))
If childControl Is Nothing Then
Panel13.BackColor = Color.LimeGreen
End If

Related

Show ContextMenuStripItem without clicking off the current cell

I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx

VB.Net 2015 "Disable btn_MouseLeave on btn_MouseClick"

In VS 2015 Community, My form as a button.
Current Code is:
Private Sub CellStart_MouseHover(sender As Object, e As EventArgs) Handles CellStart.MouseHover
CellStart.BackgroundImage = My.Resources.graycell_hilight
End Sub
Private Sub CellStart_MouseLeave(sender As Object, e As EventArgs) Handles CellStart.MouseLeave
CellStart.BackgroundImage = My.Resources.graycell
End Sub
Private Sub CellStart_Click(sender As Object, e As EventArgs) Handles CellStart.Click
CellStart.BackgroundImage = My.Resources.graycell_select
End Sub
What I want is that if the button is in the post click state (Current BackgroundImage is graycell_select), for the MouseLeave and Mouse Hover to no longer be enabled. If the button becomes deselected I need them to be enabled again. I have tried if statements, assigning an integer +1 on click but nothing seems to override the leave and hover events. I am dreadfully new so any help or points to something similar is appreciated. Thanks in advance. This is in a winform.
Click your button and Find the GotFocus and LostFocus in the Event tab.
Click GotFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell_hilight
Click LostFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell
leave the click on what it has.
Dont forget to remove not just copy remove the codes in the MouseHover and MouseLeave

How to change backcolor of text when textbox is readonly

I am working on windows form application.i given my text box back color is green.then i changed the textbox read only property true.but while running the back color is not appearing,
i have tried a lot to find this,but no answer ..
if any help is very appriciable.
How can I change the backcolor of the text, when the textbox is made readonly
Thanks
Add
If textBox1.ReadOnly
Then TextBox1.BackColor = Color.Blue
End If
to the event handler of the desired event.
If you want to change the back color when the form is loaded just use
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If textBox1.ReadOnly
Then TextBox1.BackColor = Color.Blue
End If
End Sub
If you want to change the color after a button is pressed or any other event is fired just implement the event handler and change the color there.
Go to code view and on the load function type in
TextBox1.BackColor = Color.Red
Obviously where TextBox1 is you'll enter the designer name for the textbox.

How can I place/drop an image evertime you click the mouse button using vb.net?

I looked at "How do I place an image with a mouse-click in Javascript?" but it had a small snippet of Java; immensely larger than my knowledge of Java. And that is the closest I've come to finding an answer in the past week.
Here's what I would like to do (don't know if its even possible):
I have a panel and a toolstrip with 3 buttons. Each button represents a different image. I want to click on a button (once) and then move into the panel and everytime I click the mouse button it drops the image where ever I clicked. This only ends when either I click back on the same button or one of the other buttons. I do not want to drag an image into the panel each time. In other words the button stays depressed and the event/action stays active.
Any help would be greatly appreciated.
Here is an example application. It's just a form with a ToolStrip on it, along with a couple of buttons with an image added to each button. The key property for each button is CheckOnClick=True, which will keep the button pressed down.
There isn't a radio button like feature for ToolStrips, so you have to "uncheck" the other ToolStripButtons yourself, which I have handled in the ItemClicked event.
Public Class Form1
Private _ActiveImage As Image = Nothing
Private Class ImagePoint
Public Location As Point
Public Image As Image
Public Sub New(ByVal image As Image, ByVal location As Point)
Me.Image = image
Me.Location = location
End Sub
End Class
Private _Images As New List(Of ImagePoint)
Public Sub New()
InitializeComponent()
Me.DoubleBuffered = True
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
For Each imageItem As ImagePoint In _Images
e.Graphics.DrawImage(imageItem.Image, imageItem.Location)
Next
End Sub
Private Sub ToolStrip1_ItemClicked(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
For Each toolButton As ToolStripButton In ToolStrip1.Items.OfType(Of ToolStripButton)()
If toolButton.CheckOnClick Then
If e.ClickedItem.Equals(toolButton) Then
_ActiveImage = e.ClickedItem.Image
Else
toolButton.Checked = False
End If
End If
Next
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
If _ActiveImage IsNot Nothing AndAlso e.Button = MouseButtons.Left Then
_Images.Add(New ImagePoint(_ActiveImage, e.Location))
Me.Invalidate()
End If
End Sub
End Class
This example just uses a simple class to hold which image was placed at what location and the paint event just loops through the list and paints the image.
If deleting images is in your future, then you would have to call e.Graphics.Clear(Color.White) before painting any images.
For the button UI, check out the alternate style for radio buttons/check boxes. They have a "toggle button" mode which sounds like exactly what you need.
You could go through the motions of detecting mouse down events on the panel, getting the coordinates, creating an image control, and placing a copy of the image in it, but there's a better approach.
Fill the panel with a single image control (fill so that it handles resizes, the image control should always be the same size as the panel). Create a new Bitmap the same size as the image control and associate it with it (set the Image property). Obtain a Graphics object for the Bitmap (Graphics.FromImage() I think). Clear() it with the background color (Color.White?).
Preload your three images on startup and write the code to toggle between them, selecting the "active one" every time a different button is selected. On the mouse down event, you can get the coordinates of the click easily. Use myGraphics.DrawImage(...) to draw the active image at that location onto the Bitmap. You can then save the Bitmap to a file or do whatever you want with it. All of these concepts have lots of examples, Google them.
If you want to interact with the images after you "drop" them (like move them around again or something), then you will need to maintain a data structure that tracks what and where you've dropped. A simple class that has a Point and Image reference will be sufficient. Each drop should add an entry to a List(Of ...) these objects. You'll probably then need to write code such as "which image is under the current mouse location?". This can be accomplished by iterating through the list and doing point/rectangle intersection testing.
Private Sub ToolStripSound_Click(sender As Object, e As EventArgs) Handles ToolStripSound.Click
If ToolStripSound.Checked = False Then
ToolStripSound.Checked = True
Else
ToolStripSound.Checked = False
End If
End Sub
Private Sub ToolStripSound_CheckedChanged(sender As Object, e As EventArgs) Handles ToolStripSound.CheckedChanged
' ToolStripSound.Checked = True
If ToolStripSound.Checked = True Then
Me.ToolStripSound.Image = Global.Traffic_Lights.My.Resources.Resources.Oxygen_Icons_org_Oxygen_Status_audio_volume_high
Else
Me.ToolStripSound.Image = Global.Traffic_Lights.My.Resources.Resources.Oxygen_Icons_org_Oxygen_Status_audio_volume_muted
End If
End Sub

How to change textbox border color and width in winforms?

I would like to know how do I change the border color and border width of textbox as something shown below
If it is mouse hover I need to display one colour and on mouse down I need to display another colour.
Can anyone explain me the detailed process with the source if available.
You could do the following:
Place the TextBox inside a Panel
Give the panel 1 pixel padding
Set the text dock to Fill
Make the text box to have no border
Then, handle mouse events on the text box, switch the background color of the panel between your two colors, when the mouse enters/leaves.
This isn't the most elegant approach in terms of using resources/handles but it should work without any custom drawing.
Same as above with a little twist. Unfortunately I can't comment due to reputation.
Make a UserControl
Set usercontrol padding on all to 1
Put a Panel inside the usercontrol
Set panel dock style to fill
Set panel padding to 6, 3, 6, 3 (left, top, right, bottom)
Put a TextBox inside the panel
Set textbox dock style to fill
Set textbox borderstyle to None
...then for border colour changing properties, you could use this
Dim tbxFocus As Boolean = False
Private Sub tbx_GotFocus(sender As Object, e As EventArgs) Handles tbx.GotFocus
tbxFocus = True
Me.BackColor = Color.CornflowerBlue
End Sub
Private Sub tbx_LostFocus(sender As Object, e As EventArgs) Handles tbx.LostFocus
tbxFocus = False
Me.BackColor = SystemColors.Control
End Sub
Private Sub tbx_MouseEnter(sender As Object, e As EventArgs) Handles tbx.MouseEnter
If tbxFocus = False Then Me.BackColor = SystemColors.ControlDark
End Sub
Private Sub tbx_MouseLeave(sender As Object, e As EventArgs) Handles tbx.MouseLeave
If tbxFocus = False Then Me.BackColor = SystemColors.Control
End Sub
It's pretty self-explanatory.