Overriding VB.Net TrackBar control Property -- no error, but not working - vb.net

All,
If a TrackBar control on a Windows form is selected by mouse, the keyboard left/right arrow keys can be used to decrement/increment its value respectively. Unfortunately, selecting the control causes an unsightly focus box to appear around it.
I would like to override the control's ShowFocusCues() Property to hide the focus box but retain the ability to use the keyboard keys. The following code does not throw an error, but also does not return the result I wish to achieve. Please can you help?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myTrackBar As CustomTrackBar = New CustomTrackBar
With myTrackBar
.Top = 100
.Left = 100
.Width = 200
.Minimum = 3
.Maximum = 25
.Value = 7
End With
Me.Controls.Add(myTrackBar)
End Sub
End Class
Class CustomTrackBar : Inherits System.Windows.Forms.TrackBar
Protected Overloads Overrides ReadOnly Property ShowFocusCues() As Boolean
Get
Return False
End Get
End Property
End Class

I just would like to preserve keyboard functionality if doing so is straightforward
I don't doubt the way Jimi has suggested in the comments is more correct, but it's really straightforward to put a eg a 300x50 sized trackbar inside an eg 290x40 sized panel, positioned at -5,-5 so the focus box is visually cut off.
Note that I don't guarantee these pixel sizes are exact/correct so they should be regarded as "for illustration of the point purposes only"; I'm on a cellphone and cannot measure the image I created to demo this, but it looks like the focus rect is less than 5 pixels...
In this image the cutoff is more than 5 pixels - I deliberately positioned the trackbar so that part of the slider is cut off to demonstrate that controls inside panels, that overhang the bounds of the panel, have their visual area trimmed:
The form background is blue, the panel grey and the trackbar is yellow
add a panel to the form
set your trackbar background to red temporarily so you can see its bounds
drag the trackbar into the panel then drag it again to the top left corner
use the arrow keys eg 5 times in the up and left directions to push it
size the panel to the bottom right of the trackbar
hold shift and use up/left again to resize the panel down another 5 pixels in each dimension
if you're resizing this trackbar set the anchor properties appropriately on the trackbar and panel
revert the background
If you don't use anchor for resize/don't know how it works, it's simplest to consider it like:
Any edge that is anchored maintains the same number of pixels between that edge and the same edge of the container it is in. Controls try not to resize if they can
This means that anchoring left and right will cause a control to stretch if the window gets wider. Anchoring right only causes the control to slide as the window gets wider

Related

Show Form Above System Tray

I am working on a tool that docks primarily in the system tray. However, if the icon is clicked, the form opens for more options displayed on the form. (Not a context menu) However, I want to position the form directly above the system tray and have no clue how to go about doing this. This application will run on multiple user computers with varying screen resolutions, so hard coding a preset coordinate is not a viable solution.
I did search for FindWindow() for the system tray but could not find anything useful out of Google/Bing.
There is a Screen.WorkingArea property which allows you to get the primary screen "available resolution".
Just put your window top at working area height - window height and window left at working area width - window width (and adjust if necessary to get a bit of margin).
Update: Also, account for the taskbar position and size: How do I get the taskbar's position and size?
You could use this:
First Set The form TopMost
MyForm.TopMost = True
To show the form on top of taskbar on right side, here is example:
Private Sub SetFormPosition()
Dim leftpos As Long
Dim toppos As Long
leftpos = (My.Computer.Screen.WorkingArea.Right - 2) - Me.Width
toppos = (My.Computer.Screen.WorkingArea.Bottom - 2) - Me.Height
Me.Location = New Point(leftpos, toppos)
End Sub
You can play around and change Values. (-2) is close to edge, if you put bigger number you will be away from the edge and so on. You can also change the My.Computer.Screen.WorkingArea where you want to show the Form.
You can use the code on NotifyIcon_MouseClick Event
Private Sub NotifyIcon_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon.MouseClick
If e.Button = Windows.Forms.MouseButtons.Left Then
SetFormPosition()
MyForm.Show()
End If
End Sub

Dynamically Resize forms and controls inspite of change in Display size (Small/Medium/Large) in VB.net

We have developed windows application which is including many forms and controls with default small display size and it is working fine.
but in client systems,displays are not consistent. so when the same code runs with medium/large screen size, the controls are overlapping
It would be great help if anyone gives solution for dynamic re-size/fit the form & controls for any display (small-100%, medium-125%, large-150%)
and also Is there any way to find the Display size (small-100%, medium-125%, large-150%) in vb.net
You can change display size in control-Panel.
You can use the Anchor property to automatically adjust the control size according to its form size. You can anchor any side of the control to that side of the form. For example, if I anchor all four sides of a picturebox to the form, the picturebox will mimic the shape of the form when the form is resized. If you anchor only the top and the bottom of the picturebox, for example, it will change height with the form, but the width and horizontal position will remain constant.
Finally found a solution by doing R&D
call the below function in the Form_Load like ScaleForm(me)
Public Sub ScaleForm(WindowsForm As System.Windows.Forms.Form)
Using g As System.Drawing.Graphics = WindowsForm.CreateGraphics
Dim sngScaleFactor As Single
Dim sngFontFactor As Single = 1
If g.DpiX > 96 Then
sngScaleFactor = g.DpiX / 96
'sngFontFactor = 96 / g.DpiY
End If
If WindowsForm.AutoScaleDimensions = WindowsForm.CurrentAutoScaleDimensions Then
'ucWindowsFormHost.ScaleControl(WindowsForm, sngFontFactor)
WindowsForm.Scale(sngScaleFactor)
End If
End Using
End Sub

Three panels, one in the center, other two resize

So,
I am working on a VB.NET Wordpad program. I have three panels in the middle of my program and a RichTextBox in the middle panel. Here's an image:
Example: || || The middle is the actual page [A4] [I have the size accurate]. It runs great when I debug the Wordpad program though when I resize/maximise the window, the side panels all keep the same size, though the middle panel resizes but the TextBox stays to the left of the panel which should happen. The panel shouldn't be visible as the TextBox is covering it all, but when its resized the TextBox stays the same size and the middle panel comes visible as it resized itself. The panel on the right is the same as panel #1 and stays where its meant to be and same size.
Though I'm wondering how I can make the middle panel stay in the center [when resizing] and instead of resizing the middle panel, both of the side panels resize.
So instead of || || its now |#1 |#2|#3 |
Sorry for the terrible examples but hope someone will understand. Thanks
Sorry, I misunderstood what you were initially asking and anchor won't really work for what you are trying to do.
This is the best I can come up with and uses the form resize event. It assumes the width of panels 1 and 3 are equal, the width being derived from the form width minus the width of panel 2.
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
' set panel 1 width
Panel1.Width = (ClientSize.Width - Panel2.Width) / 2
' position panel 2 next to panel 1
Panel2.Left = Panel1.Left + Panel1.Width
' position panel 3 next to panel 2
Panel3.Left = Panel2.Left + Panel2.Width
' set panel 3 width
Panel3.Width = (ClientSize.Width - Panel2.Width) / 2
End Sub
You can easily play with this if you want panel 1 and panel 3 sized differently, or if you want spacing between the panels and/or form boundaries, but it will give you a good starting point.

Decrease space between controls in FlowLayoutPanel

How can I decrease the space between the controls even further?
I've set all the margins and padding to 0 but there is still space between my controlers.
this is the space I am getting with all margins and padding set to 0. I even set the margin and padding on each controller to 0.
and for the sake of consistency here is the code that is adding the PictureBoxes
Dim newPic As PictureBox = New PictureBox()
newPic.Image = p.Image
newPic.Size = New System.Drawing.Size(New Point(p.Size.Width * 2,
p.Size.Height * 2))
newPic.SizeMode = p.SizeMode
laytt.SetToolTip(newPic, ttstring)
AddHandler newPic.Click, AddressOf LayoutComponent_Clicked
LayoutFlowLayout.Controls.Add(newPic)
You are not setting the Margin property on the picture boxes you add. The default is 3,3,3,3. Add this line of code to fix the problem:
newPic.Margin = New Padding(0)
Every control handles margins differently, even with standard controls. Have a look at this example:
Notice that a Button reserves some space around it, while a TextBox takes everything. You may ask why 2 pixels in between them which you can clearly see. For that - please copy/paste into Paint and zoom in. Those 2 pixels are in fact the border, this is how a control is drawn. I am sure Buttons also have a border, but it's harder to justify visually, even when zoomed in.
If you want to change that, you would need to create a custom control and override how it's drawn, i.e. manually cut borders from it or similar. But I would not recommend doing it, to keep UI consistent.

Scrollable image in userform

I have a word doc with a bunch of ActiveX Control buttons or whatever on it, and each time a button is clicked, a corresponding image needs to be displayed in a popup box.
I have a userform called ImageForm, and this is what I'm doing right now:
Sub Button_Clicked()
ImageForm.Picture = LoadPicture("appropriate_image_path")
ImageForm.Show
End Sub
Each of these images has a width of 8.5 inches, but their heights can vary anywhere from like 3 to 20 inches (they're snippets of a pdf). So I've set the width of the userform to a little more than 8.5 inches, and that looks fine. But I need to be able to scroll vertically through the image in the userform, since some of the images could be taller than a user's monitor.
I'm completely stuck on this. What I've tried so far is adding a frame to the form, then adding an image control inside the form, and setting the "ScrollBars" property of the frame to vertical. Then instead of using "ImageForm.Picture = ..." I use "ImageForm.ImageControl.Picture = ..." But it doesn't work.
Any insight here would be greatly appreciated. Hopefully this question is clear enough, I've only been using VBA for a month or so now. (I miss Java so, so much)
Thanks!
Here is a neat little trick based on one of my posts
The idea is to ensure that the image control is in frame control and the image control doesn't have a border. Also the image control's PictureSizeMode is set to fmPictureSizeModeClip so that we can scroll the image
SNAPSHOT (DESIGN TIME)
SNAPSHOT (RUN TIME)
CODE
Private Sub UserForm_Initialize()
With Frame1
'~~> This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'~~> Change the values of 2 as Per your requirements
.ScrollHeight = .InsideHeight * 2
.ScrollWidth = .InsideWidth * 9
End With
With Image1
.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
.BorderStyle = fmBorderStyleNone
.PictureSizeMode = fmPictureSizeModeClip
End With
End Sub