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.
Related
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.
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
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.
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