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
Related
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
I have a Windows form with a TabControl. In Tab 1 I have a RichTextBox (rtb1) and a Picturebox (pbx). In Tab 2 I have another RichTextBox (rtb2).
When I click a button, text from rtb1 and the current picture in pbx is put into rtb2. Problem is that the picturesize won't always fit. My current code is this:
Dim text = rtb1.Text
rtb2.AppendText(text)
Clipboard.SetImage(pbx.Image)
rtb2.Paste()
Is it possible to have the code resize the image as it's pasted?
It is possible to get the image from a windows path, I just didn't know how to resize this in a RichTextBox either. If this makes it easier?
Edit: This did it!
Dim newWidth = 500
Dim newHeight = 500
Clipboard.SetImage(New Bitmap(pbxDatasheet.Image, newWidth, newHeight))
rtbArmy.Paste()
Edit 2: It still doesn't work.
With the exact same code these things happen:
1. Most of the time the pasted picture(s) are the same size. But not the right size. I can change newWidth and newHeight all I want, they rarely differ in size. Although sometimes the picture is way larger than the others.
2. Sometimes it doesn't even add the picture to the RichTextBox. There's no pattern to when it happens.
The same lines of code can do all the above things. I have no idea what to do here.
I am trying to implement a 'Flag' w/ a menustrip.
I have an image list w/ a red flag and a black flag.
When I click the menu item, i want to toggle the image.
The problem I have is that once I change the image, it wants to do SizeToFit which makes the Icon roughly the height of text. I dont want that. I want the Image to be its actual size.
I have tried putting the statements in different orders, nothing i have tried seems to work.
Currently, I have the button set to the black flag at design time.
This code shows me trying to change to a Red flag.
tsbFlagPatient.Image = ilFlags.Images(1)
tsbFlagPatient.ImageScaling = ToolStripItemImageScaling.None
tsbFlagPatient.DisplayStyle = ToolStripItemDisplayStyle.Image
[If there is some better way to approach toggling the image, im open to that. That seems like a separate question. ]
I was unable to make ImageScrolling work.
Since I dont need text here, I used BackGround Image and set
BackgroundImageLayout = Stretch
This allows the images to be the right size.
You can turn AutoSize = false and then manually set Height, Width to fine tune your menu's size.
I did reconsider the ImageList and changed the images to being stored in the project Resources. This is the code to toggle the image
Public Sub SetFlagImage(flagSet As Boolean)
If flagSet = False Then
btnFlag.BackgroundImage = My.Resources.appbar_flag_wavy_black
btnFlag.BackgroundImage.Tag = "black"
Else
btnFlag.BackgroundImage = My.Resources.appbar_flag_wavy_red
btnFlag.BackgroundImage.Tag = "red"
End If
End Sub
This had no effect on the question posed here. In general, probably better than having an image list because all forms can access it.
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
I am trying to change the location and size of buttons I have contained in a panel on a WinForm (vb.NET).
Initially the buttons have a location and an image (no text), but in execution I want to set a new location and text.
I am able to set the text for each button, and as they are set to Autosize, they increase in width.
But despite I set the location by code correctly, when the buttons are displayed they "come back" to their initial position.
Any help would be appreciated.
Thanks,
IM
The following code will change the location to the co-ordinates you specify:
Button1.Location = New Point(x, y)
You must change the x,y values to the co-ordinates on the form that you want to move the button too.
The next bit of code with change the size of your button:
Button1.Height = 10
Button1.Width = 50