The control or subform control is too large for this location on resize - vba

I have a simple form in Access 2003. The form has a List control and a button. I wanted to make the list to resize with the form (only vertically) while the button remains at the bottom right of the List. I'm using the following code on the form's resize event.
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
This works fine as I resize the form, until the form height gets to a certain height. Then I get this error;
Run-time error '2100':
The control or subform control is too large for this location
This error is occurring at the line where I'm assigning the commandButton.Top. If I remove this line then the list height changes fine. I don't have any subforms in the form.
Does anyone know why this is happening?
Thanks

I think it is because you need to resize the detail section of the form first.
Try changing the code to
Me.Section(0).Height = Me.InsideHeight
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50

Came by here (as many have) with the same problem and then realised my issue. Be mindful when resizing a control to a larger size with regard to the order of setting properties.
I would recommend setting the TOP and LEFT positions before HEIGHT and WIDTH.
Although my final sized control should have fitted OK once resized, I had originally tried setting the WIDTH first which attempted to enlarge the control exceeding the width of the form. My application threw the 2100 error at that point. I really hope this helps someone! Also, don't forget to set dimensions in TWIPS which is set as INCHESS x 1440 (or CM x 566.9291), ie: .Width = 10 * 566.9291 to set a control width to 10cm.

I get this same error if I set the width to greater the 31680.
With a little more research, I notice MS Access only allows a form width to be 22" wide. 22" = 31680 TWIPS.
So my workaround solutions it to add a check:
If newWidth > 31680 Then newWidth = 31680

Related

Problem changing size of a form on secondary display

I am creating a Windows form using VB.net
The form is part of a VSTO Excel Add-in
I move the form to a secondary display using:
Dim b As Rectangle = activeScreen.Bounds
Dim location As Point = Me.Location
Me.StartPosition = FormStartPosition.Manual
location.X = location.X + b.X
location.Y = location.Y + b.Y
Me.Location = location
where activeScreen is of type System.windows.forms.screen.
Before resizing, my form is size {Width = 1139 Height = 308}
I resize my form using
Me.size = newsize
where new size has value {Width = 2278 Height = 308}
However, the value of Me.size somehow jumps to {Width = 5695 Height = 770}
And on the screen, this is in fact what is seen, the form is now 2.5 times bigger than what I expect.
If I change the height of the form in this way, all works fine. Similarly, the above code works fine when I keep my form on the primary display.
The value of Me.DeviceDpi is 96 in both scenarios.
My primary display is scale 250% (recommended) and Display resolution 3200x1800 in the settings, and screen bounds is [Bounds={X=0,Y=0,Width=3200,Height=1800}
The secondary display is scale 100% (recommended) and Display resolution 1360x768 in the settings, and screen bounds is {X=8000,Y=938,Width=3400,Height=1920}
The answer to this question seems to bear no resemblance to the problem.
I was resizing my form as part of a response to Form_PreviewKeyDownevent
Moving the code to Form_KeyUp event, suddenly everything behaved correctly as it had done when running on the primary monitor.
In the same vain, part of my form contains a Label that I change and move dynamically. This form was simply disappearing when I move the form to the secondary display. Debugging size, location, bounds etc showed numbers consistent with all the other controls on the form. Tried calling BringToFront() thinking maybe some subtle scaling had caused the label to be moved behind another control, but this did not help either.
Solution / workaround - every time I change or move the label, remove it from the controls and re-add a newly created label. Once again, with this work around, all once again works like it did on the primary monitor.

How to determine a control size in twips?

I'm developing VB.net application in MS Visual Studio 2019 (community Edition) in which I programatically paste an image from the clipboard into a Richtextbox.
I need to add an option to force that image to completely fill the control's visible area.
Examining the richtext which results from pasting the image, and then again after I've manually dragged the image to resize it to fill the control, it looks like all I need to do is change the picwgoal and pichgoal rtf tags for the image to the dimensions of the control.
The problem is that picwgoal and pichgoal are in twips.
So my question is: Is there a reliable way to dynamically calculate a control's dimensions in twips from it's pixel width and height, such that it will work when the app runs in various resolutions/scaling ?
I understand that the TwipsPerPixelX method is obsolete and not compatable with 64 bit processes.
Thanks in advance.
..Think I've worked it out myself
..Think I've worked it out myself
assuming a point is always 1/72 inches and a point is 20 twips..
Dim g = Me.CreateGraphics()
Dim RTBxTwips = (RTB.Width * 72 / g.DpiX) * 20
Dim RTByTwips = (RTB.Height * 72 / g.DpiY) * 20
g.Dispose()
..seems to give results equal to the picwgoal and pichgoal tags for an image resized to the controls visible area

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

If I move a WinForms form using VB.NET code... its height also changes. But why oh why?

In the VB.NET VS2005 IDE I size this form's height to 180.
When I run this code it displays A150 B150 C180. (Not sure where the 150 comes from... or why it changes later.)
The form is still correctly displayed as 180 tall.
If I move the form's left or top position... this will change to A150 B150 C150
The form is incorrectly displayed as 150 tall
Debug.Print("A " & frmMy.Height)
frmMy.Left = 11 ' Changes to LEFT or TOP decreases the form's height by 30???
'frmMy.Top = 2
Debug.Print("B " & frmMy.Height)
Call frmMy.ShowDialog()
Debug.Print("C " & frmMy.Height)
Do I have something set in the VS2005 IDE that says "if you reposition this form... change its height too???
I was able to replicate similar behavior by setting AutoSize to True and AutoSizeMode to GrowAndShrink. Check if these settings were turned on inadvertently.

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