Control Tip Text on dual monitors? - vba

I have a small user community for an application that contains a lot of user forms and fields.
I have added ControlTipText on most of the fields as helper/reminder.
The problem occurs when the user form is opened/displayed on the secondary monitor, the yellow stuff
(ControlTipText) is popping up on the right edge of the primary monitor.
Not a huge issue, but annoying.
Anyone who has a fix for this?

Same issue, resolved by:
Creating a label with a distinctive background colour positioned over the control that you want to write tip text for
Initialise the UF with the label visible = false, e.g. Userform1.label1.visible = false
use the "mousemove" event to write code to show the label
Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
label1.visible = true
'position label as desired
End Sub
use the click or mousemove event on the label this time to banish the tool tip. You will have lots of labels over controls in the VB editor.
As an alternative I creatie a label with an "Information" icon that is clickable to unhide the tooltip label rather than mousemove event sometimes

As ControlTipText is a property of the Text Box it doesn't have any members which would allow you to adjust it's position.
It seems like the only way the issue can be resolved is by viewing Excel on your primary monitor. If it's useful, you can force Excel to move to the primary monitor with something like this:
With Application
.WindowState = xlNormal
.Left = 0
.Top = 0
.WindowState = xlMaximized
End With

Related

How do you show a panel in VB dot net, if all the panels are created dynamically and sit in the exact same spot?

I am using Visual Studio 2022 and my language of choice is VB. When the program starts it creates Multiple Panels that are located in the same position created from data from a database.
No matter what I do, I can not get any of the panels to show. I can search for the panels and verify the panels exist, I can search the panels and verify that there are buttons in the panels. However I can not get each individual panel to show.
I am going to list all the panels that are created in the order they are created and they are all in the exact same position and size.
panel2
pnlDeptItems01
pnlDeptItems11
pnlDeptItems22
pnlDeptItems23
pnlDeptItems24
pnlDeptItems25
pnlDeptItems26
pnlDeptItems27
pnlDeptItems28
pnlDeptItems29
pnlDeptItems32
pnlDeptItems54
pnlDeptItems82
pnlDeptItems83
pnlDeptItems84
pnlDeptItems85
pnlDeptItems96
pnlDeptItems97
pnlDeptItems98
pnlDeptItems99
Visible and Enabled are set to true for all panels on creation.
Below is the code that I use to search for the panel by name and if I find it then try to make it visible which it already is and then I try to bring it to front. And it does absolutely nothing but keeps the 1st panel visible.
For Each control In Me.Controls.OfType(Of Panel)
If control.Name = btnPanelName Then
control.Controls.Add(oButton)
control.Visible = True
control.BringToFront()
Console.WriteLine(control.Name & " " & oButton.Name)
End If
Next
CODE TO CREATE PANELS
Private Sub CreateNewPanel(ByRef pnlName As String, ByRef pnltopx As Integer, ByRef pnlLefty As Integer, ByRef pnlSizex As Integer, ByRef pnlSizey As Integer)
Dim iPanel As Panel
iPanel = New Panel
iPanel.Name = pnlName
iPanel.Enabled = True
iPanel.Visible = False
iPanel.Top = pnltopx
iPanel.Left = pnlLefty
iPanel.Size = New Size(pnlSizex, pnlSizey)
iPanel.AutoScroll = True
Me.Controls.Add(iPanel)
If iPanel.Name = "pnlDepartments" Or iPanel.Name = "pnlDeptItems26" Then
iPanel.Visible = True
iPanel.BringToFront()
End If
End Sub
I then tried doing this and this does not work either:
For Each control In Me.Controls.OfType(Of Panel)
If control.Name = btnPanelName Then
control.Controls.Add(oButton)
End If
If control.Name = "pnlDeptItems26" Then
control.Visible = True
End If
Next
Code that is called to create the panels
CreateNewPanel("pnlDeptItems" & dataDeptId, 608, 5, 429, 479)
dataDeptId is pulled from a database in a loop.
Can someone explain to me why this is not working or what the work around is? I know it has something to do with all the panels that are created being created in the exact same position.
Thanks,
Shawn Mulligan
I found the solution. After reading my post I took notice that for some reason that my paneltopx and my panellefty were reversed and too large for some reason as well as my panelsizey and panelsizex.
I made the adjustments and made sure that all panels visibility was set to false, and now when I set visibility to true, the correct panel displays with the buttons that I created.
Thanks,
Shawn Mulligan

How do i hide a control in a report based on a field value

I have a checkbox control that i need to hide if a Boolean value is false. I have tried using the Detail_Format event to no avail. i put the field value into another checkbox, then tried setting the visible property from the value in the checkbox, no dice. In the image below, the rightmost checkbox is showing the value of the field that determines if the checkbox to the left of it should be shown. I have the following code
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.chkRequireverify = True Then
Me.chkVerified.Visible = True
Else
Me.chkVerified.Visible = False
End If
'Me.chkVerified.Visible = Me.chkRequireverify
End Sub
when i tried using the Detail_Paint event, it errored out telling me that i cant change the visible property in this event.
Your code looks to be correct and could be shortened to simply:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.chkVerified.Visible = Me.chkRequireverify
End Sub
However, I believe the event handler for the OnFormat event will only be evaluated when viewing the report in Print Preview view, rather than in Report view.
Whilst the OnPaint event fires in Report view, you cannot modify certain properties after the report has been formatted, the Visible property being one such property, along with the Height & Width properties (and so you also cannot shrink the checkboxes to zero size).
It's been a while since I've used Access and VBA, but I believe what you're missing is .Value after the checkbox name in the if clause.
Try this : If Me.chkRequireverify.Value = True

Is it possible to center windows forms with vba?

My VBA macro is formatting a USB drive before it copies data on it.
The whole process is already working quite good.
So the problem is, that the Windows Format Tool window is opening somewhere on the top left corner of the screen and not like I want it, centered and pretty in the middle of the screen.
So is there a possibility to open them centered or if not, is there anything to center them afterwards?
This is how the window starts:
Private Sub format_drive(selected_volume)
Dim Result&, Drive&
varray = Split(selected_volume, ":")
Drive = Asc(varray(0)) - 65
Result = SHFormatDrive(0, Drive, SHFD_CAPACITY_DEFAULT, SHFD_FORMAT_QUICK)
End Sub
If it is a macro then there must be a parent application, most probably Excel. When a userform is launched from under Excel, the form will be positioned within the active window (that may be maximized). If you want to change the position of your form taylor the following sample to your needs:
Private Sub UserForm_initialize()
Me.Show vbModeless
With Me ' this must come after .Show
.Top = (Abs(Application.Top) + Application.Height - .Height)/2
.Left = (Abs(Application.Left) + ActiveWindow.Width - .Width)/2
End With
Application.Top can be negativ, Abs() is applied to handle it. It's not the most correct yet it's safe. Alternatively you can use ActiveWindows:
With Me
.Top = (Application.ActiveWindow.Height - .Height)/2
.Left = (Application.ActiveWindow.Width - .Width)/2
End With
At an advanced level you can use GetDeviceCaps(HDC hdc, HORZRES) and GetDeviceCaps(HDC hdc, VERTRES) to get the number of pixels of your screen. See this for more: GetDeviceCaps

Remove previous selection highlighting in RichTextBox without scrolling

I have a form with a RichTextBox (RTB) and a listBox.
When the end user selects an item in the listbox, any matched text in the RTB is highlighted (full code removed for brevity).
re = New Regex(txtToFind)
For Each m In re.Matches(rtbMain.Text)
rtbMain.[Select](m.Index, m.Length)
rtbMain.SelectionColor = Color.White
rtbMain.SelectionBackColor = System.Drawing.SystemColors.Highlight
Next
When the user left mouse clicks in the RTB I want the previously highlighted text to be cleared. This is the standard windows behaviour - If you manually select some text in an RTB with the mouse, it is highlighted, click anywhere back in the RTB and the highlighting disappears. My programatically selected text remains selected.
I have some partially working code (below). I can clear all the highlighted text, but it is by process of selecting everything, changing the colour back and then deselecting it again. I know it is not efficient, the RTB flickers and I am sure it is not the correct way to do it. Can I emulate the standard windows behaviour?
Also using my code, it scrolls to the first line when entering the RTB a second time.
I get around this the first time by returning the top visible line index before clearing the text and then selecting that line again afterwards and using ScrollToCaret(). This only works on the first pass. Subsequent MouseDown events select the top row regardless of where the user has clicked so nothing can be manually highlighted in the RTB.
Private Sub rtbMain_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbMain.MouseDown
Dim topIndex As Integer = rtbMain.GetCharIndexFromPosition(New System.Drawing.Point(1, 1))
Dim topLine As Integer = rtbMain.GetLineFromCharIndex(topIndex)
If e.Button = Windows.Forms.MouseButtons.Right Then
'Do nothing (Context Menu)
Else
rtbMain.SelectAll()
rtbMain.SelectionColor = Color.Black
rtbMain.SelectionBackColor = Color.White
rtbMain.DeselectAll()
rtbMain.Select(topIndex, 0)
rtbMain.ScrollToCaret()
End If
End Sub
I need my code to emulate the standard windows behaviour - clear selected text highlighting on MouseDown and leave the mouse cursor where the user has clicked.
Any help anyone can offer is gratefully appreciated.
I think you may be overthinking this one.
In the right click event, try RtbMain.SelectionLength = 0

Opening new form after right click on selected record in continuous form

In my access database before I was using a list box in the form for having list of items and I had below code for opening new form after right click on each selected item in list box.
Private Sub ItemList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
If Button = RIGHTBUTTON Then
DoCmd.OpenForm "frmShortcut_GenerateTask"
DoCmd.MoveSize udtPos.X * mp.TwipsPerPixelX, udtPos.Y * mp.TwipsPerPixelY
End If
End Sub
Now I am using a continuous form instead of list box and I have defined a [isselected) field for selecting each record in continuous form after clicking on that. Now my problem is how I have to write code for right clicking and opening new form.
I used the same code I had used for list box, but it does not work and nothing happened.
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
If Button = RIGHTBUTTON Then
DoCmd.OpenForm "frmShortcut_GenerateTask"
DoCmd.MoveSize udtPos.X * mp.TwipsPerPixelX, udtPos.Y * mp.TwipsPerPixelY
End If
End Sub
Private Sub P_Click()
On Error Resume Next
Me.IsSelected = Not Me.IsSelected
' Save the status
Me.Dirty = False
' Force conditional highlighting
P_ForceHighLight
' Update display in SF_Selected
Me.Parent("SF_Selected").Requery
ActiveControl.SelLength = 0
On Error GoTo 0
End Sub
I recommend either using a DoubleClick event in all of your textboxes and combos, or else putting a small button at one edge of your continuous form that allows the user to open records. Right-click is going to give you problems (so will labels and image controls) because this event doesn't cause (or ensure) that the Form's internal Recordset Bookmark property is actually moved to the record they right-clicked on. In my experience, only buttons, textboxes, and comboboxes will move the Bookmark to the record the user is trying to select.
To Test this, try putting code at the top of your different routines that shows you what record is selected:
MsgBox "RecordID = " & Me!RecordIDField
If you are consistently getting the correct ID, then your problem has to do with how your opening your new form, passing parameters or arguments, etc.