How to apply "Anchor" property - vb.net

I have design a form which contains one tab control and dynamically adding tab pages.
Problem: in each tab page, controls are not placed at correct place as given "LOcation" field while adding "Anchor property to RIGHT OR BOTTOM or eighter of it. If i remove anchor then it works fine.
But i need to use anchor to resolve resize of form should remain consistent for the controls.
Please suggest how i could resolve this issue and also anchor should remain there.
Below is the code:
Dim t As TabPage = New TabPage(titletext)
t.Name = IDValue
Dim w As New WebBrowser()
Dim b As New Button()
b.Text = "&Close"
w.Size = New Size(New Point(tcrViewer.Width - 3, tcrViewer.Height - (b.Height + 30)))
w.Anchor = AnchorStyles.Right Or AnchorStyles.Bottom
w.Location = New Point(0, 0)
w.Navigate(url)
t.Controls.Add(w)
b.Location = New Point(w.Width - (b.Width + 30), w.Size.Height + 3)
b.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
AddHandler b.Click, AddressOf btnReportClose_Click
t.Controls.Add(b)
tcrViewer.TabPages.Add(t)
tcrViewer.SelectedTab = t
tcrViewer.SelectedTab.Focus()
when execute the form, then, WEBEXPLORER CONTROL CUTS by form and it does not appear on "0,0" location and same thing happen for Button also. If i remove "Anchor" then its all fine as follow the location.
Please resolve
Thanks

The issue is resolved.
I have removed "DocK" property and added webexplorer into the panel.
Anchor is applied to "Bottom" and "Right" only and not to top, left. Then its work fine for Bottom,right anchor style.

Related

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.

Inserting a panel between 2 controls in Windows form

I want to insert a Panel (User Control) dynamically between two other control. Let say I have a PictureBox(Control) as the parent are there is an other control on the pictureBox.
I want to remove the other control from the picture box, add a panel(control) to the picture box and then set the older control to the new panel. For now I have this code :
If m_targetDisplay.MainDoubleBufferedPictureBox.HasChildren Then
For Each child As Control In m_targetDisplay.MainDoubleBufferedPictureBox.Controls
If (child.BackColor = Drawing.Color.Transparent) Then
Dim panel As SXFadeWrapperForGPU = New SXFadeWrapperForGPU()
panel.ClientSize = New Size(child.ClientSize.Width, child.ClientSize.Height)
panel.Location = New System.Drawing.Point(child.Location.X, child.Location.Y)
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Add(panel)
panel.Controls.Add(child)
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Remove(child)
AddHandler panel.Paint, AddressOf PanelPaintEvent
End If
Next
End If
My code is adding a background wrapper to the transparent color child in front of it. The thing is even if I remove the child before adding or after adding it back, I can never see it on the screen. Is there any particular thing that maybe the Remove does so the removed Child isn't usable again ?
You should change the child location to (0, 0), else it will still be relative to its position inside MainDoubleBufferedPictureBox, but now it's in the new panel. It could be there, just located outside the panel's boundary
If m_targetDisplay.MainDoubleBufferedPictureBox.HasChildren Then
For Each child As Control In m_targetDisplay.MainDoubleBufferedPictureBox.Controls
If (child.BackColor = Drawing.Color.Transparent) Then
Dim panel As SXFadeWrapperForGPU = New SXFadeWrapperForGPU()
panel.ClientSize = New Size(child.ClientSize.Width, child.ClientSize.Height)
panel.Location = New System.Drawing.Point(child.Location.X, child.Location.Y)
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Add(panel)
panel.Controls.Add(child)
child.Location = New System.Drawing.Point(0, 0) ' <--- this
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Remove(child)
AddHandler panel.Paint, AddressOf PanelPaintEvent
End If
Next
End If

Dynamically add button disappears when anchored

I have a really simple Dynamically add button. It works fine, but when I set the anchor property it does not show anymore.
Dim btn As New Button() With {.Text = "Cerrar", .Location = New Drawing.Point(8, 368)} 'Crea el boto
Me.Controls.Add(btn)
btn.BringToFront()
btn.Show()
btn.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
AddHandler btn.Click, AddressOf Me.Dispose 'Tanca la Pestanya al clicar btn
If I comment the "btn.anchor..." line it shows and works. When debbuging it shows that the property is set fine.
What's happening?
As you have sugested, I added the button to the form after the handler and anchor, nothing changed.
I though that maybe the anchor is moving down my butto, so i tried to locate it at (1,1)... And the anchor seems to be moving it.
Here it is without the anchor The button is the one outlined in red.
Now without comenting the anchor set As you can see the button have been moved down and left.
The only change in the code is that im setting the anchor to bottom on the second one.
There is nothing starting there, it just seems an arbritary position.

Table layout panel inc#

I have to create dynamic table layout panel with some controls with auto sized rows and and fixed columns size.
My problem is that i want to show whole checkbox text .
Any help
My code is
Dim textBox2 As New CheckBox()
textBox2.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
textBox2.AutoSize = True
textBox2.Dock = DockStyle.Top
'' textBox2.Size = New Point(200, 90)
Dim lbl1 As New Label()
lbl1.Location = New Point(10, 10)
lbl1.Text = "Yoer.vb"
lbl1.AutoSize = True
lbl1.Location = New Point(120, 50)
lbl1.Dock = DockStyle.Top
'' dynamicTableLayoutPanel.Padding = New Padding(2, 17, 4, 5)
dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
dynamicTableLayoutPanel.Controls.Add(textBox2, 1, 0)
Me.dynamicTableLayoutPanel.SetColumnSpan(textBox2, 5)
If you mean you want the table to size to the controls within it, then:
dynamicTableLayoutPanel.AutoSize = True
I know this is old, but I stumbled across it and figured I'd throw my 2 cents in in case someone else comes along.
Note: I'm using Visual Studio 2015 with .NET 4.6. Functionality may differ between versions.
The problem is that the really long text is not word-wrapping to fit within the table or form. Instead, it is set to Dock = DockStyle.Top. This will cause it to make a single line that continues on and gets clipped, similar to a single-line textbox.
If you want it to automatically word wrap, you'll need to use Dock = DockStyle.Fill. Now, this doesn't completely resolve the problem if your row or table isn't large enough to display the text. Since all of the rows are set to AutoSize, it will only do the bare minimum to fit the control vertically. It doesn't care if text gets clipped off. The end result, using your example code against a 6-column, 10-row table, is this:
Since there isn't a word wrap property, you'll need to manually fit it. Now, to do this, you'll need to change the row to be Absolute instead of AutoSize. To figure out how big to make it, you can pretty much rely on PreferredSize. This reveals a much wider Width than the existing regular Width. From that, we can determine how many lines it would take if we wrap it.
This is what my code ended up looking like:
Dim h As Single = 0
Dim chk As New CheckBox()
chk.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
chk.AutoSize = True
chk.Dock = DockStyle.Fill
Dim lbl1 As New Label()
lbl1.Text = "Yoer.vb"
lbl1.AutoSize = True
lbl1.Dock = DockStyle.Top
dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
dynamicTableLayoutPanel.Controls.Add(chk, 1, 0)
dynamicTableLayoutPanel.SetColumnSpan(chk, 5)
' Find the preferred width, divide by actual, and round up.
' This will be how many lines it should take.
h = Math.Ceiling(chk.PreferredSize.Width / chk.Width)
' Multiply the number of lines by the current height.
h = (h * chk.PreferredSize.Height)
' Absolute size the parent row to match this new height.
dynamicTableLayoutPanel.RowStyles.Item(0) = New RowStyle(SizeType.Absolute, h)
The changes included delaring a height variable, renaming the CheckBox variable, setting its Dock to Fill, removing the Location from lbl1, and adding in size calculation. The output:
This isn't perfect since the height includes the checkbox itself, and the checkbox takes up padding, so there can be too much or too little height calculated. There are other calculations that may need to be considered. But, this is a starting point.

Order of controls being added to panel, control not showing unless docked

I imagine this is probably an easy to answer question but for some reason I can't get it to work
Sub New(ByVal Sess As AudioSessionControl2)
S_Session = Sess
'Create the panel and position it.
S_Panel.BackColor = Color.AliceBlue
S_Panel.Width = 200
S_Panel.Height = 40
Dim Position As New Point(6, 19)
If G_AppSessions.Count > 0 Then
Position = Point.Add(G_AppSessions.Item(G_AppSessions.Count - 1).SessionPanel.Location, New Point(0, 45))
End If
S_Panel.Location = Position
'Create a label which has the name of the process
Dim S_PName As New Label
S_PName.Text = "Test"
S_PName.Dock = DockStyle.Left
S_Panel.Controls.Add(S_PName)
'Create a button to change volume
Dim S_Save As New Button()
S_Save.Text = "Save"
AddHandler S_Save.Click, AddressOf Save_Click
S_Save.Parent = S_Panel
S_Panel.Controls.Add(S_Save)
S_Volume.Parent = S_Panel
S_PName.Parent = S_Panel
MainForm.Controls.Add(S_Panel)
S_Panel.Parent = MainForm.gb_Applications
End Sub
The problem is that, the label will show because its docked, but the button won't. It will only show if its docked as well, and thats just not what I want. This is part of a class for creating a dynamic UI, where I can create a number of this class to create a bunch of panels for various things.
I don't see anywhere where you are setting the label or button position. You probably have them both at 0,0 and the label is on top of the button, obscuring it. Did you try setting the position of both the controls, making sure they don't overlap?