I have a VBA/Excel that user clicks on labels (Active X - Text Label) to perform some actions. The label property is BackStyle Transparent, but when the user click, the label keep opaque, like white or whatever the BackColor property is set.
How can I keep transparent when user click on the label?
Don't use an ActiveX control for this. Any Shape can be assigned to a macro, so instead of having Click event handlers for ActiveX labels like so:
Private Sub Label2_Click()
'do stuff
End Sub
Make the handlers public, give them a meaningful name:
Public Sub BuscaPorPalavraChave()
'do stuff
End Sub
Replace the labels with TextBox shapes - make the shape fill and border transparent, right-click the shape, and select "assign macro" - then pick BuscaPorPalavraChavre. Done!
Rinse & Repeat for every label. I know, painful - but worth it!
That navigation UI looks very nice BTW =)
I came up with a different solution in case you have faced a related problem, like the transparency/opaque problem when executing a macro hovering over a transparent label but that becomes opaque when clicking on it.
The workaround consists basically in changing the visibility status when hovering in and out. It might be counterintuitive at first (Because you are making disappear the same label you are using to execute the macro) but it works really well. Let's assume we have two Active X labels overlap. Label1 is the bigger one and label2 is the smaller one, completely contained in Label1. The code I used is like:
Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.ScreenUpdating = False
Label2.Visible = False
Label1.Visible = True
### Put your code in here
Application.ScreenUpdating = True
End Sub
Then as you hover out label2, and assuming the labels are correctly overlap, you will hover over Label 1, now visible because you "activated it", and the following code is executed
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.ScreenUpdating = False
Label2.Visible = True
Label1.Visible = False
### Put your code in here
Application.ScreenUpdating = True
End Sub
Please bear in mind that you need to have a figure or button in between the two Active x labels, otherwise you will get a strange result because in the exact moment you hover over Label 2, it will disappear, leaving Label 1 beneath it which will also disappear as soon as you move, makin Label 2 appear again and ... you get the idea. This will not break the excel file but will make it make strange things like blinking or delaying the mouse movement. To prevent thus. I recommend to have that shape as a safe zone.
Hope this helps.
Related
I have been struggling on this one for a few days now and can not seem to work it out. I am using images as buttons, which have a mouseover effect that changes the border to GREEN when the mouse is hovered over them, apart from them flashing on hover, this works fine apart from when the problem kick in, see below for problem.
I then have an onClick event which changes the border to GREEN if that image/button is selected, this indicates that this is active as the click opens a multi page, this bit also seems to work fine as I had to put in a repaint event in as it would not work without it. UserForm1.Repaint.
The Problem I am having
Once a image/button has been selected and then de-selected the mouse over event NO longer works on the image/button that was previously selected.
Example, If I scroll the mouse over the two buttons below, their borders will change to green on hover, If I selected the WebBrowser button, its border will change to green, indicating that it is active. Once de-selected the mouse over will NO longer work on it. The de-selecting is done by selecting another button.
This is my mouse over code
Private Sub WebBrowserNewButt_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
''Set WebBrowserNewButt Border Colour Green
If WebBrowserNewButt.BorderColor <> 8435998 Then
Me.WebBrowserNewButt.BorderColor = 8435998
''Grey Borders
Me.MainMenuButton.BorderColor = -2147483627
Me.SecondMenuButton.BorderColor = -2147483627
Me.EmailButton.BorderColor = -2147483627
Me.SearchButton.BorderColor = -2147483627
Me.SaveNewButton.BorderColor = -2147483627
End If
End Sub
Onclick code
Private Sub WebBrowserNewButt_click()
Me.WebBrowserNewButt.BorderColor = 8435998 'GREEN BORDER
'GREY BORDERS, for all other images(buttons)
Me.MainMenuButton.BorderColor = -2147483627
Me.SecondMenuButton.BorderColor = -2147483627
Me.EmailButton.BorderColor = -2147483627
Me.SearchButton.BorderColor = -2147483627
Me.SaveNewButton.BorderColor = -2147483627
UserForm1.Repaint
'MULTIPAGES
Me.MultiPage1.Pages(0).Visible = True
MultiPage1.Value = 0
Me.MultiPage1.Pages(1).Visible = False
Me.MultiPage1.Pages(2).Visible = False
Me.MultiPage1.Pages(3).Visible = False
Me.MultiPage1.Pages(4).Visible = False
End Sub
What I have tried so far.
I have tried it with and without the Me. (I left it on Me.)
I have tried to google the answer
I have tried it without repaint, did not work without repaint, repaint
does cause the images to flash.
I have tried to place the change boarder colour in the multi-page, so when active that multi page is
visible = true then the image border colour will change, could not get it to work.
I removed the option of turning the borders to Grey on hover, that did not fix the problem.
The multi-pages will not have their tabs set to visible.
Please could someone advise on what to do.
As always thanks in advance.
I am having an issue with flickering labels on my userform, when I do a mouse over. Some labels are grouped. I have ungrouped them but I am still having the flickering issue. For each button I have used 3 labels, 1 for background, 1 for Icon and 1 for text as nothing would position the icon and text as I wanted. See image
Apart from the flicker they work fine. I am using labels as buttons as the look better than the vba userform buttons and because, when I tried to you an image as a button, the picture would not display correct, the quality would reduce, but it is fine in a label.
The labels start to flicker when I move the mouse over them, I was looking at these as some possible solution on,Mr Excel Forum & My Original Thread on Mr Excel (there is a code at the botom of this thread), but I am not 100% sure how to change it for my labels. In the second thread that I posted there is a link to the original question which was raised by another member, within this thread they talk of label groups "LabelGroup.Parent.Parent.Controls". I have NO idea what this is and how to do it. Please could someone advise of how I can implement one of these codes with my code, useform or guide me to another solution.
My code is a simple IF statement, that works on a mouse over event
Private Sub MainMenuPageButton_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'MainMenuPageButton border change green on mouse over
MainMenuPageButton.BorderColor = 8435998 'change boarder to green
'Grey Borders for all other labels
SearchButton.BorderColor = -2147483627
WebBrowserButton.BorderColor = -2147483627
SaveFileButton.BorderColor = -2147483627
EmailButton.BorderColor = -2147483627
LoadFileButton.BorderColor = -2147483627
Sheet2Button.BorderColor = -2147483627
Sheet3Button.BorderColor = -2147483627
Sheet12Button.BorderColor = -2147483627
End Sub
Thanks in advance
As the title states, I want to have a shape with text in it (think of a flowchart), then make an active label over it. So when one moves their mouse over the label, a GIF will pop up. Then when they move the mouse off of the label, it will disappear.
The closest thing I have so far is this (but it could be entirely wrong):
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Sheet1.Shapes("situation.GIF").Visible = True
End Sub
Any advice or help would be amazing, if you have any questions please don't hesitate to ask!
Regards
This will only work for the labels sender but, I need to use this also for the button sender so it can hide if the label is red only when the button is clicked.
I have 48 Seats
Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Seat9.Click, Seat8.Click, Seat7.Click, Seat6.Click, Seat5.Click, Seat48.Click, Seat47.Click, Seat46.Click, Seat45.Click, Seat44.Click, Seat43.Click, Seat42.Click, Seat41.Click, Seat40.Click, Seat4.Click, Seat39.Click, Seat38.Click, Seat37.Click, Seat36.Click, Seat35.Click, Seat34.Click, Seat33.Click, Seat32.Click, Seat31.Click, Seat30.Click, Seat3.Click, Seat29.Click, Seat28.Click, Seat27.Click, Seat26.Click, Seat25.Click, Seat24.Click, Seat23.Click, Seat22.Click, Seat21.Click, Seat20.Click, Seat2.Click, Seat19.Click, Seat18.Click, Seat17.Click, Seat16.Click, Seat15.Click, Seat14.Click, Seat13.Click, Seat12.Click, Seat11.Click, Seat10.Click, Seat1.Click
Dim Seats As Label = CType(sender, Label)
If Seats.BackColor = Color.White Then
Seats.BackColor = Color.Red
Else
Seats.BackColor = Color.White
End If
Okay, I think I understand now.
You have multiple labels which represent seats and when the user clicks on the labels they change from white to red.
You are wanting to create a button that once clicked, it removes all red labels from the screen/form.
You would place the following in the click event of your button (assuming that your form is called frmMain)
For Each c As Control in frmMain.Controls
Dim l As Label = DirectCast(c, Label) ' Cast from control to label
If Not l Is Nothing ' If this control is indeed a label then process
If l.BackColor = Color.Red Then ' If color is red then hide label
l.Visible = False
Else
l.Visible = True
End If
l.Visible = False
End If
Next
I don't have VS pulled up, so syntax may be a little incorrect but I think you get the point.
I have an excel sheet that contains two rectangles and text in other cells.
I need to allow users to only edit the text in the rectangle. They should not be able to change the size of the object.
Applying lock on the rectangle locks the object as well as the text.
Does anyone know how I can achieve this?
Why not create two objects, one being a rectangle that is locked, and one being a text box that is not locked? This is really simplistic, but a possible answer.
Another idea would be to have the rectangle equal a set cell, and let them enter their text in the cell and it would transfer over even when the rectangle is locked.
As far as I am aware Excel does not accommodate Events for shapes and so there is no simple way of detecting a change in a shape size and then resizing the shape.
It is possible to emulate what you are asking for by using a workaround.
Imagine you have two rectangles on your spreadsheet called 'Rectangle 1 and 'Rectangle 2'. When a user finishes updating the text in any given box they must then click the spreadsheet to move out of 'edit' mode for the shape. You can detect this using the Workbook_SheetSelectionChange event.
The following module allows you to set the size of the rectangles as constants and will resize the rectangles accordingly:
Const Rect1Height As Integer = 50
Const Rect1Width As Integer = 200
Const Rect2Height As Integer = 50
Const Rect2Width As Integer = 200
Sub SetRectangleSize()
Dim Rect1 As Shape
Dim Rect2 As Shape
Set Rect1 = ActiveSheet.Shapes("Rectangle 1")
Set Rect2 = ActiveSheet.Shapes("Rectangle 2")
Rect1.Height = Rect1Height
Rect1.Width = Rect1Width
Rect2.Height = Rect1Height
Rect2.Width = Rect1Width
End Sub
Now all you need to do is to call this sub from a workbook level event:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
SetRectangleSize
End Sub
Each time a user updates the text in one of the rectangles they will click back on the spreadsheet and the event is fired, resulting in the rectangles being sized according to the constant height and width parameters that you have defined.