How to scroll down a panel in VB.NET using button without scrollbar - vb.net

I would like to know how to scroll down my Panel that contains a TabControl
in VB.NET using a button without showing a scrollbar. I tried the following
Dim Location As Integer = Panel_Images.Location.Y
If Location + 20 < Panel_Images.VerticalScroll.Maximum Then
Location += 20
Panel_Images.VerticalScroll.Value = Location
Else
'If scroll position is above 280 set the position to 280 (MAX)
Location = Panel_Images.VerticalScroll.Maximum
Panel_Images.AutoScrollPosition = New Point(0, Location)
End If
It only scrolls for once and doesnt work anymore.

So you want to set the location of the Panel inside another control? If you want to scroll the control up, set it's location.Y (or its 'Top') property to minus whatever value you want, it will be clipped by its parent.
To scroll down, set the property positive.

Related

Displaying images from a picturebox list

I am trying to display a line of pictures in my program. But I am having a problem, where it is only showing the first image in the imagelist and only showing one image-box.
Private Cards As New List(Of PictureBox)
Private Sub SetupCards()
For i As Integer = 0 To imglist1.Images.Count - 1
Dim PicCard As PictureBox = New PictureBox()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50
Me.Controls.Add(PicCard)
PicCard.Image = imglist1.Images(i)
Cards.Add(PicCard)
Next i
End Sub
You're placing the picture boxes on top of each other, which is why you only see the last card. You've got to set a different Left property for every picture box you add.
The solution is rather simple. Just add the picture box's width to Left, multiplied by the current index i.
PicCard.Left = 50 + PicCard.Width * i
Don't need to keep the imaging controls in your own list if you add them to a parent container control.
Use ListView or third-party controls, or use custom drawing code if you need to use ListBox (which wraps respective Windows control). See
C# Can I display images in a list box?

Location properties on winforms acting strange

I have a form where I allow the user to generate multiple panels with some various contents in the panels by pressing an "add" button. Depending on what the user does in the panel, the panel grows and shrinks to fit the contents. Because of this change is size, I have created a sub that formats the panels on the form.
Private Sub formatPanels(frm As Form)
Dim count As Integer = 0
Dim startPoint As Point = New Point(12, 80)
Dim endPoint As Point = New Point(0, 0)
Dim maxY As Integer = 0
For Each pnl As Control In frm.Controls
If TypeOf pnl Is Panel Then
ReDim Preserve _arr_Panels(count)
_arr_Panels(count) = pnl
count += 1
pnl.Location = startPoint
startPoint.Y += pnl.Size.Height + 30
End If
Next
End Sub
So as you can see, we loop through every panel and the first always begins at the location (12,80) and then increments with the size of the panel and some spacing.
HERE IS THE ISSUE. This ONLY happens when i am SCROLLED DOWN the form. The panels spacing all of a sudden screws up and decides to put the first panel hundreds of pixels down the form. Is the location property based on what you're looking at? So if I were scrolled down the form location(0,0) would be the top left of the current view? There must be some weird property to location that I am not aware of.
Thanks
This behavior is not related to a panel, but to any control on a form with AutoScroll = True and Anchor including Top. (Note: if Anchor didn't also include Left I had some strange positioning on the first call of the function.
The solution is described here which is to use AutoScrollPosition. If you change your startPoint to this it will adjust for the scroll position.
Dim startPoint As Point = New Point(12, Me.AutoScrollPosition.Y + 80)
And the documentation for AutoScrollPosition states this:
When adding controls programmatically to a form, use the AutoScrollPosition property to position the control either inside or outside of the current viewable scroll area.

Determine if button is inside contour

I have a PictureBox on which a contour is drawn. Let's say this contour is in form of a circle. Now when I click a button a lot of buttons are created over the PictureBox row by row until the PictureBox is full:
For n As Integer = 1 To buttonNumberY
For m As Integer = 1 To buttonNumerX
Dim Btn As New Button
Btn.Width = elementsize
Btn.Height = elementsize
Btn.Flatstyle = Flatstyle:Flat
Btn.FlatAppearance.BorderSize = 1
Btn.FlatAppearance.BorderColor = Color.Gray
Btn.Location = new Point(elementsize*(m-1)+BORDER, maxContourHeight * scaley + BORDER - elementsize * n)
Btn.BackColor = Color.Transparent
Btn.Name = "Btn" & m & n
PictureBox1.Controls.Add(Btn)
Next
Next
As true transparency doesn't exist in WinForms I used some code so that the contour will be visible through the small buttons lying over the PictureBox (thanks for the help with this problem :)).
Now I have to determine whether a button is inside the contour or outside. I already have an idea of how to do this:
In a For Next loop every button should be checked if it has been painted.
If yes, it is partially inside the contour.
If no I have to check whether one of all the buttons under this one (in y direction) is painted.
If none of the buttons below this one is painted then it is not inside the contour.
If only one button below is painted then it is inside the contour and if two buttons are painted then it is outside of the contour again.
The problem I have is the following:
How can I check if a button is painted or not?
All the small buttons are generated through the code. Can I actually write code to check these buttons by referring to their name when they don't even exist yet in the code?
Essentially it sounds like you want to check if a point is within a circle, mathematically speaking this would be the algorithm:
(point.x - center.x) ^ 2 + (point.y - center.y) ^ 2 < radius ^ 2
In terms of your two questions, the button would be painted after you create the control. Probably not immediately, but you can force the control to be repainted dynamically by refreshing the parent.
Since you're setting the Button's name, then you could use the Form.Controls.Find method and set the searchAllChildren parameter to True.

VBA: Dynamic Adapting Scrollbar

I have a user form containg a frame. During runtime, the user can add controls to this frame. Ultimately, the added controls can exceed the size of the frame. In this case I would like to increase the insideheight of the frame. So my vertical scrollbar does cover the whole area.
frm.ScrollHeight = .InsideHeight
How do I do this?
The .InsideHeight is not responsible for the scrollbar. However, the .ScrollHeight is. While adding new controls, this height must be increased.
When adding a new set of controls execute this:
Exchange 50 with the height of your control.
Dim scrHeight As Integer
With frm
scrHeight = .ScrollHeight
.ScrollHeight = scrHeight + 50
End With

VB.NET panel scrolling without showing scrollbars

I am trying to create a custom UI element: a panel, derived from the original panel, which is scrollable with special scrollbars (not the integrated ones) and has some other special abilities.
The actual problem is the scrolling. When I change the value of the custom scrollbar (e.g. scrolling), the panel-integrated scrollbars show up suddenly, although autoScroll = false.
Leading to the unintended state where both scrollbars are visible, the integrated and my custom one.
Private Sub ScrollB_EvValueChanged(NewVal As Integer) Handles ScrollB.EvValueChanged
Me.CleanPanel1.VerticalScroll.Value = NewVal
End Sub
How can I assign the new scrolling position (the new offset), determined by the custom scrollbar, to the panel without showing up panel-integrated scrollbars?
Sadly a panel (or usercontrol) with another panel on it and playing with the .Top and .Left properties of the inner panel to simulate scrolling is not an appropriate solution in my case.
Thank you for all your hints!
'I've been looking for methods of doing this all over, most of it is way more complicated than it needs to be, or you gotta write a whole dang program just to remove the bars to scroll. Anyhow, here's a quick, effective, neat method of doing this (since I was having trouble finding anything, I'll post it.
'e.delta detects a mouse wheel tick, if greater than 0, scroll up, less than 0 scroll down.
' the -91 part deducts some of the panel overhang (adjust as needed)
'I have buttons in this project that are 50 tall, so this scrolls perfectly for me.
'the nested if statements where there is no code (just else), tells the program to stop 'do nothing if a border is near by. The bottom of my panel, while scrolling, doesn't 'stop when the bottom of the panel (which is hanging off the bottom of the form quite 'some ways) reaches the bottom of the form, this can be adjusted by altering the 700 constant.
Private Sub DaddyPanel_MouseWheel(sender As Object, e As MouseEventArgs) Handles DaddyPanel.MouseWheel
If e.Delta < 0 Then
If (-DaddyPanel.Height - 91) > (DaddyPanel.Location.Y - 700) Then
Else
TextBox1.Text = DaddyPanel.Height & " " & DaddyPanel.Location.Y
DaddyPanel.Location = DaddyPanel.Location + New Point(0, -50)
End If
Else
If DaddyPanel.Location = New Point(0, 0) Then
Else
DaddyPanel.Location = DaddyPanel.Location + New Point(0, 50)
End If
End If
End Sub