Why do I have to set VerticalScroll.Value twice? - vb.net

I've got a Windows form that contains a picturebox that holds images of differing size - width is always the width of the form, height is calculated to keep the aspect ratio steady.
The up & down arrows (and home, end keys) are configured to scroll the window - but I'm finding I have to make the following call twice to 'properly' scroll:
Me.VerticalScroll.Value += 100
Me.VerticalScroll.Value += 100
The first call changes the scroll position (but not the scrollbar position), then the second changes the scrollbar position.
Why is this? Am I setting the wrong option here?

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 do I move something downwards without it dissapearing off the form? VB.Net

This is really quite ridiculous, but I have a picture box called picWeather, and it covers the whole form and looks like rain, and I want it to look like it's falling. This code does it perfectly, but goes up instead of down?!?!?!?!!?!
picWeather.Top = picWeather.Top - 50 'Moves rain up by 50
picWeather.Height = picWeather.Height + 50 'Adds 50 to the height of the rain
If picWeather.Top < -10000 Then 'If the rain is 10000 above the form then
picWeather.Top = Me.Top 'Set the position of the top of the rain to the top of the form
picWeather.Height = Me.Height 'Set the height of the rain to the height of the form
End If 'End the if statement
It's so simple, but because moving something by the bottom is read only and when you add height to something it adds it to the bottom, it's confusing... Anyone able to get there head around this?
Y coordinates are measured down from the top of the form. If you want to move something downward then you need to INCREASE the Top value, not DESCREASE it.
You should only be setting Left, Top, Width and Height if you only want to set one of them. If you want to set multiple then do one of the following:
Set Location to set Left and Top.
Set Size to set Width and Height.
Set Bounds to set Location and Size.
Call SetBounds to set any combination of Left, Top, Width and Height.
In your case, you want to set Top and Height so you should be calling SetBounds. That said, is there actually any point changing the Height? If you want the rain to initially cover the whole form and appear to be moving down then you need the PictureBox to have an initial Height that is greater then the form and a Top that is less than zero. As you move the PictureBox down, the Top approaches zero and you can reset when it gets there, e.g.
If picWeather.Top = 0 Then
'Move the rain back up to its starting position.
picWeather.Top = -10000
Else
'Move the rain down 50 pixels.
picWeather.Top += 50
End If
As a physical analogy, let's say that you are looking through a window at a picture that I'm holding. The picture is much taller than the window. I start by positioning the picture such that its bottom edge lines up with the bottom edge of the window. I slowly move it down and you see "rain falling". When the top of the picture gets level with the top of the window, I quickly move picture back up to its starting position. That's exactly what that code does: it moves the control down by increasing the Top value and then, when the top edge lines up with the top edge of the form, it immediately the control back to its starting position with its top edge well above that of the form.

Change location of buttons at execution VB.NET

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

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.

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

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