'ActiveX Textbox flickers white whenever changing position

I have an ActiveX Textbox on an Excel worksheet. I need to move this textbox around and change its visibility using VBA frequently as you interact with the program. The issue is whenever I change the textbox visibility or position, it briefly flashes white before reacting to the code. Here is a video of what I'm talking about. In this video, the code is designed to move the textbox (which has a grey background), to position itself directly over the active cell whenever the selection changes. You can see when it moves over a cell with a yellow background. Then when I change the selection to leave the yellow cell, you can see the textbox flicker white before moving to the new location and becoming grey again.

https://vimeo.com/709930517

Also heres a screenshot of the instant I click another cell after the textbox was placed over the yellow cell.

enter image description here

Before this image, the single textbox on the Worksheet was grey and placed over the yellow cell. You can see in the image after clicking above the yellow cell, the textbox has flickered white over the yellow cell. This is the white flicker. Also in this image, the textbox appears it has already moved to the new location (where I clicked), but its still visible in the old location as well! There's only 1 textbox on the worksheet!

This is super annoying because I have a lot of background colors and when the textbox flickers white it looks horrible.

This is my code:

WORKSHEET CODE

Private Sub RulesTextbox_KeyDown(ByVal keyCode As MSForms.ReturnInteger, ByVal shift As Integer)

    Call MODTextbox_KeyDown(keyCode, shift, TextboxSheets.rules)
    
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Call MODWorksheet_SelectionChange(Target)
    
End Sub

MODULE CODE

Option Base 0
Option Explicit


Global activeCell As Range

Public Sub MODTextbox_KeyDown(ByVal keyCode As MSForms.ReturnInteger, ByVal shift As Integer)



End Sub
    
Public Sub MODWorksheet_SelectionChange(ByVal Target As Range)

    Application.ScreenUpdating = False
    

    Set activeCell = Target
            
    Dim textbox As OLEObject
    Set textbox = Worksheets(1).OLEObjects("MyTextbox")
            
    Call MODResizeTextboxToMatchRange(textbox, Target)
    With textbox
        .Visible = False
        .Visible = True

        .Object.BackColor = RGB(220, 220, 220) 'light grey
    End With

    textbox.Object.Value = activeCell.Value2
    textbox.Activate

    Application.ScreenUpdating = True
    
End Sub
    
    
    
Public Sub MODResizeTextboxToMatchRange(ByRef textbox As OLEObject, ByRef selectedRange As Range)

    If selectedRange Is Nothing Then
        Exit Sub
    End If

    Dim totalWidth As Double
    Dim totalHeight As Double
    Dim top As Double: top = selectedRange.top
    Dim left As Double: left = selectedRange.left

    If selectedRange.MergeCells And selectedRange.Cells.Count = 1 Then
        totalWidth = selectedRange.MergeArea.Width
        totalHeight = selectedRange.MergeArea.Height
    Else
        totalWidth = selectedRange.Width
        totalHeight = selectedRange.Height
        
    End If

    With textbox
        .top = top + 1
        .left = left + 1
        .Width = totalWidth - 2
        .Height = totalHeight - 2
    End With
    
End Sub

I've tried various work-arounds to try and get this to work:

  1. It doesnt matter if you reposition the textbox or make it invisible, it always fickers white first.
  2. Application.ScreenUpdating = False has no effect. Neither does calling DoEvents immediately after changing the position.
  3. Changing the transparency of the textbox to try and make it dissappear doesnt help either, you get the same white flicker

It's like causing any change to the control makes it flicker. I would love some kind of ScreenUpdating = False equivalent so I could just hide the textbox and set back to true when its repositioned. I don't know much about the internal workings of ActiveX. Is it an issue with my computer and not Excel?

Also using a regular form input will not work, I need ActiveX to be able to style the textbox, particularly increasing the font size for users.

Any help is appreciated thanks



Solution 1:[1]

Well I figured it out. I need to set the textbox to .Visible = False BEFORE it get's moved, then make it visible after the move.

And you also need to set the backstyle of the textbox to transparent, in the VBA properties before you run the program. When the program is running and the textbox is focused it WONT be transparent, but as soon as it loses focuses it becomes transparent which will eliminate the flicker.

And finally, the TEXT of the textbox may still flicker even if the background doesnt. You can overcome this by setting the font size to 0 before you change the position, then when the textbox arrives at the new location set the font size back to 12 via VBA. You will still see a very tiny version of the text in the flicker, so to take it a step further in addition to setting the font size to 0 you can change the color of the font in the textbox via VBA to match the background color of the cell behind it, again switching back to regular font color once the textbox has been moved.

Wow I had no idea it was so simple!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 T.M.