How do I get scrollbar on panel - vb.net

I have a Form with a Panel. In this Panel, I want to use the vertical scrollbar when I needed.
How do I do this? I've tried setting autoscroll true and set a min scroll height, but the scrollbar never appears.
I've also tried this:
my_panel.ScrollBars = ScrollBars.Vertical
but then I get the error that scrollbar is not a member of my_panel?
Thanks.

Autoscroll property is actually enough to achieve your need. Basically a panel with autoscroll property true will display the scroll bar only when the contents/components inside that panel exceeds over its bound. In other words, Scroll bar appears with controls which have autoscroll property set to true when the particular control's contents are larger than its visible area. I think your panel is having some minimum amount of contents/controls which fits inside that panel's bound.

I know You asked this question more then year ago, but... ;)
Recently, I had same problem (label inside panel, and I need only vertical scrollbar).
If You want only vertical scrollbar of panel with label inside, use code bellow :
Dim pnl As New Panel
pnl.Size = New Size(300, 200)
pnl.AutoSize = True
Dim lbl As New Label
lbl.Location = New Point(0, 0)
lbl.AutoSize = True
lbl.MaximumSize = New Size(pnl.Width - 18, 0)
'18 is approx. width of scroller, and height must be zero.
'even if Label is set to AutoSize, MaximumSize will not allow him to
'expand more then set width.
'Height of zero px will allow Label to expand as much as he need
pnl.Controls.Add(lbl)
Me.Controls.Add(pnl)
I hope this code help You.
btw. sorry for my weak English, I hope You will understand ;) :)

Related

VB NET What is the good way to find a position of an object inside a scrollable panel?

I have an object inside a scrollable panel, and I want to get the position of the object. Everything is okay until I have create many object inside the panel until the panel needed to be scrolled. Then I notice that the X Position of the object changed after scrolling the panel. Take a look at example image below
X Position inside a scrollable panel
I'm using Position.X to get a X Position of the object, but as you can see in the picture above. It does me no good.
Is there a way to get the actual position even if the panel is scrolled ?
UPDATE:
I have One Panel that have Auto Scroll as True , and on Button inside a Panel, let's just say the button name is Button1 and the Panel is Panel1
The Code i use to get the location of the Button1 is :
Dim msg = Button1.Location.X
msgbox(msg)
You're getting the screen position of the object, I don't know how because you didn't post your code. What you need to do is add the scroll offset, eg.
Position.X + scrollableControl.AutoScrollPosition.X

Not able to set the Backcolor of label as Transparent

Using properties of "label" not able to set the backcolor as transparent ... when i select the color transparent from the option which is showing some color as backcolor, if transparent works properly must show background instead of some colors. please help
If you add a control at design time when setting the background to transparent it 'displays' the background of the form not the control on which it was placed unless that control is a container such as a panel.
2 options:
1 place the label on a panel and the label then displays the panel background (which can be a picture if that is what you are trying to do)
2 place the label programatically i.e.
dim Label1 As New Label
Control.Controls.Add(Label1)
Label1.BackColor = Color.Transparent

How to get scrollbar in Panel in VB.Net?

I am developing a Windows Application in VB.Net. In that, there is one case where there is one form, and in that form there is a Panel, and within the Panel there is a rich text box.
So my requirement is to get a scrollbar in the Panel. So when the user does scroll on the panel, the rich text box can scroll accordingly like MS Office functionality..
Can any one give me an idea how to do it?
Set Panel.AutoScroll = True and scrollbars will automatically appear whenever any controls in the panel fall outside its boundaries.
Set the .Dock property to FILL and the .WordWrap property to FALSE for the richtextbox.
Also set the Panel's .Dock property to FILL.
In order to use panel autoscroll property I do that:
panel.AutoScroll = true
panel.VerticalScroll.Visible = False or panel.HorizontalScroll.Visible = False
In order to know the dimensions of the scroolbars use
SystemInformation.HorizontalScrollBarHeight
SystemInformation.VerticalScrollBarWidth
So you can change the dimension of the panel when the scroolbar is shown.

How do I make a group box's text bold but not the text of controls contained in it?

I went and created a tab containing a good amount of controls, most of which are contained within what I'll just call the top-level group box. Now I decide I'd like the text of the top-level group box to be bold, but nothing else. When I set the top-level group box's font to bold, however, all of the controls contained within it become bolded as well, which is what I don't want. I can set each individual control's bold property to false, but it seems like there should be an easier way to do this. Any ideas?
I'm probably missing something obvious, like a group box property that is staring me in the face--and apologize if this turns out to be the case.
Thanks in advance for any help.
You could bypass the problem by placing a label over the caption for the GroupBox, but I wouldn't necessarily recommend this.
A better solution emerges once you understand what is happening and why it is happening. The issue is that a control's font (among other things) is an ambient property, meaning that child controls inherit their parent/container control's properties. So if you set the GroupBox to use a bold font, all of its child controls automatically inherit the bold property by default.
The key there is, of course, by default. Ambient properties only apply if you don't explicitly set the properties of the children to something else. If you don't want the child controls to be bold, select them all and turn off bold. The settings of the parent/container will no longer override the new custom settings.
To make things even easier, you can add a Panel control to your GroupBox, dock/anchor it to fill the entire client area of the GroupBox control, and set it to use a standard, non-bold font. Then, the rules of ambient controls stipulate that the child controls you add to the Panel will not be bold by default. This way, you only have to change the font property of one control as opposed to every child control that you add to the GroupBox.
The reason that this is better than trying to add a Label control over the GroupBox caption is because a GroupBox is designed to contain controls. You can take advantage of the docking and anchoring properties to make sure that everything gets arranged correctly, and you won't have to fight the designer when doing so to make sure that your custom Label correctly covers up the default label drawn by the GroupBox control. Additionally, you won't run into Z order issues or have other redrawing problems rear their ugly heads at runtime when, for example, the Label control gets accidentally hidden behind the GroupBox and no one can see it (and a host of other potential snafus).
I came across this old question when searching for the same, and realised it could be solved in code without adding a separate control just to overcome the ambience issue that Code Gray mentions in his answer.
Add an extensions in a module like so:
<Extension()>
Public Sub UnBold(Of T As Control)(cc As Control.ControlCollection)
For Each c As Control In cc
If Not TypeOf c Is T AndAlso c.GetType.GetProperty("Font") IsNot Nothing Then
Dim RegularFont As New Font(c.Font.FontFamily, c.Font.Size, FontStyle.Regular)
c.Font = RegularFont
ElseIf c.HasChildren Then
UnBold(Of T)(c.Controls)
End If
Next
End Sub
Then unbold all the controls in all the GroupBoxes on a form (including any child GroupBoxes) by using as follows in the form's OnLoad event:
Me.Controls.UnBold(Of GroupBox)()
Or for all controls in a single GroupBox (again, including any child GroupBoxes):
MySpecificGroupBox.UnBold(Of GroupBox)()
With the proviso that if you actually want control within the GroupBox to actually stay emboldened you will have to set that in code after calling the extension.
Consider bypassing the problem by placing a label over the GroupBox's text area and make the label's font bold.
I did it once and even used a CheckBox (for enabling/disabling the whole group). Worked like a charm.
Place all of your controls inside of a ContentControl and reset the font parameters
<GroupBox Header="Group" FontSize="16" FontWeight="Bold">
<ContentControl Margin="0" FontSize="12" FontWeight="Regular">
...
...
...
</ContentControl>
</GroupBox>
Programatically you can do it in order. Assume you want to make font style bold in groupbox but not in child controls. First set the font to a new Font in child controls, in this case you can pass groupbox font property. Then change groupbox font style to bold.
var grpBox = new GroupBox()
{
Text = "",
Width = 780,
Height = 70,
Parent = panel1,
Dock = DockStyle.None,
AutoSize = false,
Visible = true,
Location = new Point(20, grpY)
};
var label = new Label()
{
AutoSize = true,
Parent = grpBox,
Enabled = true,
Name = "label" + btnNum++,
Location = new Point(5, 50),
Text = "",
Font = new Font(grpBox.Font, FontStyle.Regular)
};
var txtBox = new TextBox()
{
Width = 550,
Height = 23,
Location = new Point(65, 20),
Name = "txtBox" + btnNum++,
Parent = grpBox,
Enabled = true,
Tag = label,
Font = new Font(grpBox.Font, FontStyle.Regular)
};
grpBox.Font = new Font(grpBox.Font, FontStyle.Bold);

ContextmenuStrip Width

I need to change the contextmenustrip width dynamically, by default the contextmenustrip width depends on the text length of the ToolstripmenuItems.
And BTW I really don't wanna redraw the control again!!!
Thanks in advance.
You need to set the ContextMenuStrip AutoSize property to false. Then you can set the Width property to whatever you want. When AutoSize is set to true, the Width property is ignored and is calculated dynamically.
Example:
Dim menu As New ContextMenuStrip()
menu.AutoSize = False
menu.Width = 100
AutoSize does a VERY poor job of guessing at the "correct" size anyway.
When TRUE, the menu is far wider than any of the text would need it to be.
When you set it manually... you also have to set the HEIGHT to be far less than you would expect... if you want it to display in the correct size menu.