Inserting a panel between 2 controls in Windows form - vb.net

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

Related

FlowLayoutPanel not showing more than one Control after it's cleared

My program automates a process on a website using selenium, HTTP requests, and an HTML parser known as HtmlAgilityPack.
There are parts of this process that require user input that can not be automatically filled out by hard-coded values. Allowing the user control over the website to fill in these inputs would be a mistake. It would be difficult to know when they are done inputting it and they would likely touch something they shouldn't which would cause errors when control of the website is handed back over to the program.
Instead, I download the page source of the website using selenium and parse the inputs out of the HTML with HtmlAgilityPack. Then controls matching those inputs are dynamically generated and added to a FlowLayoutPanel. The FlowLayoutPanel works correctly the first time. It displays all added controls. When the user is done they click on a button and the process continues.
Sometimes the process encounters errors that cause it to loop back to where the user needs to enter input again. The exact code that worked before is then run in the same subroutine again. It all runs without error and as expected. I have manually checked at runtime that the controls are added to the FlowLayoutPanel, that they have a size bigger than 0, 0, and that they are a visible color. Yet only one control ever shows up in the FlowLayoutPanel no matter how many should be visible.
The FlowLayoutPanel has it's AutoScroll property set to true and it has adequate space to add more controls. As I said this works fine the first time around just not the second. I've been stuck on this for a while and would appreciate some help. The code is going to be posted below for you to look at.
The code I am going to post has several FlowLayoutPanels. One that is on the Form permanently that was created in the designer and the others are generated dynamically to group up the controls.
The issue is with the permanent one. It shows one control as I said. This control is the first dynamically generated flow layout panel and all of it's children. It doesn't display any of the other FlowLayoutPanel controls that are added to it. Just whichever one is added first.
For those of you wondering about the line FLPOutOfStock.ImprovedClear() in the code bellow, The .Improved clear is an extension method I wrote. When Clearing the FlowLayoutPanel with the normal method, .Clear(), the controls get removed but they don't get disposed of. My method disposes of all the controls in the panel and then calls the Clear() method on the panel.
I realize that the Clear() method isn't necessary after they've been disposed of it's just a backup since nothing ever seems to work. I wanted to make sure the panel was fully reset.
Here is the code:
Private Sub UIOutOfStockState_Load()
Try
cmdOutOfStockDeleteAll.Enabled = False
cmdOutOfStockContinue.Enabled = False
FLPOutOfStock.ImprovedClear() 'The flowlayoutpanel that does not display all controls
With New WebDriverWait(ChromeDriver, TimeSpan.FromSeconds(20)).Until(Function(driver) CBool((CType(driver, IJavaScriptExecutor)).ExecuteScript("return jQuery.active == 0")))
End With
LBLOutOfStockErrors.Text = "Notice. The following items are out of stock. Please see below for product-specific availability dates. If you have any questions, please contact Customer Service at (800) 843-2020. All direct to patient orders will ship complete. The entire order will ship when out-of-stock product becomes available."
Dim OutOfStockDoc As New HtmlAgilityPack.HtmlDocument
OutOfStockDoc.LoadHtml(ChromeDriver.PageSource)
Dim OutOfStockProductNodes As HtmlAgilityPack.HtmlNodeCollection = OutOfStockDoc.DocumentNode.SelectNodes("//div[contains(#id,'id_detail_item_')]")
FLPOutOfStock.SuspendLayout()
If OutOfStockProductNodes IsNot Nothing Then
For Each OutOfStockProductNode As HtmlAgilityPack.HtmlNode In OutOfStockProductNodes
Dim FLPOutOfStockProduct As New FlowLayoutPanel
FLPOutOfStockProduct.SuspendLayout()
FLPOutOfStockProduct.FlowDirection = FlowDirection.LeftToRight
FLPOutOfStockProduct.AutoSize = False
FLPOutOfStockProduct.Size = New Size(420, 160)
FLPOutOfStock.Controls.Add(FLPOutOfStockProduct)
Dim WBProductText As New WebBrowser
WBProductText.Size = New Size(400, 120)
WBProductText.AllowNavigation = False
WBProductText.AllowWebBrowserDrop = False
WBProductText.IsWebBrowserContextMenuEnabled = False
WBProductText.ScriptErrorsSuppressed = True
WBProductText.ScrollBarsEnabled = True
WBProductText.Margin = New Padding(0, 0, 0, 0)
WBProductText.Padding = New Padding(0, 0, 0, 0)
WBProductText.Navigate("about:blank")
Dim ProductTextHtmlDoc As New HtmlAgilityPack.HtmlDocument
ProductTextHtmlDoc.LoadHtml(OutOfStockProductNode.OuterHtml)
ProductTextHtmlDoc.DocumentNode.SelectSingleNode("/descendant::a[#id='patient-outOfStock-delete-item']").Remove()
WBProductText.DocumentText = ProductTextHtmlDoc.DocumentNode.OuterHtml
FLPOutOfStockProduct.Controls.Add(WBProductText)
FLPOutOfStockProduct.SetFlowBreak(WBProductText, True)
Dim SpaceReducer0 As New Panel
SpaceReducer0.Size = New Size(0, 0)
FLPOutOfStockProduct.Controls.Add(SpaceReducer0)
Dim cmdProductDeleteButton As New Button
cmdProductDeleteButton.Text = "Delete"
cmdProductDeleteButton.BackColor = Color.White
cmdProductDeleteButton.ForeColor = Color.Black
cmdProductDeleteButton.Margin = New Padding(0, 0, 0, 0)
cmdProductDeleteButton.Padding = New Padding(0, 0, 0, 0)
cmdProductDeleteButton.Size = New Size(400, 20)
Dim OnClickAttributeValueWithQuoteEscaping As String = OutOfStockProductNode.SelectSingleNode("/descendant::a[#id='patient-outOfStock-delete-item']").GetAttributeValue("onclick", "")
cmdProductDeleteButton.Tag = "//a[#id='patient-outOfStock-delete-item' and (#onclick=""" & OnClickAttributeValueWithQuoteEscaping & """)]"
AddHandler cmdProductDeleteButton.Click, AddressOf cmdProductDeleteButton_Click
FLPOutOfStockProduct.Controls.Add(cmdProductDeleteButton)
FLPOutOfStockProduct.SetFlowBreak(cmdProductDeleteButton, True)
Dim SpaceReducer1 As New Panel
SpaceReducer1.Size = New Size(0, 0)
FLPOutOfStockProduct.Controls.Add(SpaceReducer1)
FLPOutOfStockProduct.ResumeLayout()
Next
FLPOutOfStock.ResumeLayout()
FLPOutOfStock.Refresh()
End If
cmdOutOfStockDeleteAll.Enabled = True
cmdOutOfStockContinue.Enabled = True
Catch Ex As Exception
cmdOutOfStockDeleteAll.Enabled = True
cmdOutOfStockContinue.Enabled = True
End Try
End Sub
So I’ve been asked to show what is in the improved clear method. Here it is:
<Extension()>
Public Function ImprovedClear(ByRef Control as Control)
For Each controlchild as control in control.controls
Control.Dispose()
Next
Control.Controls.Clear()
Return Nothing
End Function

DataGridView DataBindingComplete doesn't fire until after doing something in the form

In this form, there is a tab control. A new tab page is added for each table in the Data Set. Then to each tab page a Data Grid View is added for displaying the contents of the corresponding table.
This is the form's load event...You can see where I set the Data Source for the data grid view being added:
Public Sub dlgCreateTables_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
bindingComplete = False
For i = 0 To binData.BinnedTables.Tables.Count - 1
Dim tabPage As TabPage = New TabPage
tabPage.Text = binData.BinnedTables.Tables(i).TableName
tabTables.TabPages.Add(tabPage)
dgvData = New DataGridView
With dgvData
.Dock = DockStyle.Fill
.AllowUserToOrderColumns = False
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.DataSource = binData.BinnedTables.Tables(i)
.Columns.Add("Total", "Total")
.Columns("Total").ReadOnly = True
End With
tabPage.Controls.Add(dgvData)
'These following lines have to go after dgvData is added to tabPage because otherwise they don't work
dgvData.AutoResizeColumnHeadersHeight()
For Each col As DataGridViewColumn In dgvData.Columns
col.SortMode = DataGridViewColumnSortMode.NotSortable
Next
'call handler for summing the columns for each row to make Total column once binding is complete
AddHandler dgvData.DataBindingComplete, AddressOf dgvData_Created
'And this to show row headers in the row selectors (but not the triangle when row is selected)
AddHandler dgvData.RowPostPaint, AddressOf MyDGV_RowPostPaint
AddHandler dgvData.KeyDown, AddressOf dgvData_KeyDown
AddHandler dgvData.CellValueChanged, AddressOf dgvData_CellEdit
Next 'DataTable In binData.BinnedTables.Tables
End Sub
For whatever reason, the DataBindingComplete event does not fire until after the form is fully loaded and I do something like click a button or change a value in the grid.
I have a different solution in which I do the same thing and it works in there. I can't for the life of me find anything different, or figure out why dgvData_Created isn't hit until something else happens.
Note that the contents of the tables in the Data Set to which the grids are bound are created via a separate module. Currently I'm just filling everything with a "2" to get this issue working.
UPDATE:
Per Saragis's suggestion, I moved the line to add the Handler for the DataBindingComplete event up to just after instantiating the DataGridView (before "With dgvData"). Now it works in that dgvData_Created does fire when adding dgvData to the tab page, and my table is there and looks great.
However, If there is > 1 table in my data set, the DataBindingComplete event only seems to fire for the first table. After the 1st grid is done, it goes back to adding the 2nd grid but skips DataBindingComplete, and goes to RowPostPaint after the form Load is complete.
As a result, the first grid's row headers are not painted until after I force a repaint (such as by going to a different tab then coming back). And any grids after that do not have the column header row sized right (though they do show the row headers).
How can I at least force a repaint programmatically for that first grid (since that is the one I open the form to)?? I tried .Refresh(), but that didn't work.

Unable To Close A Form Embedded In A Panel

I have an issue that is really starting to do my head in...
My application has a ListView control that is populated with items. When you double click on an item it creates a new instance of a form. It then creates a new panel and adds the form to the panel. However, for the life of me I cannot work out how to close the form inside the panel.
Inside my DoubleClick event:
Dim frm As New frmStorePage(_store.Code, _store.Name)
'Create a new panel with the store page
Dim pnl As New Panel
pnl.Name = _store.Code
pnl.BackColor = SystemColors.Control
pnl.Size = New Size(1522, 892)
pnl.Location = New Point(3, 3)
frm.TopLevel = False
frm.Name = _store.Code
pnl.Controls.Add(frm)
frm.Show()
pnlStores.Controls.Add(pnl)
pnl.BringToFront()
...
Inside my Close event:
Dim panel As Panel = CType(pnlStores.Controls.Find(lsvOpenStoreList.SelectedItems(0).Name, False)(0), Windows.Forms.Panel)
For Each control As Control In panel.Controls
If TypeOf control Is Windows.Forms.Form And control.Name = panel.Name Then
control.Dispose()
End If
Next
pnlStores.Controls.Remove(panel)
panel.Dispose()
pnlStoreList.BringToFront()
...
I have also tried declaring my form as a global variable but still cannot seem to close it.
The form has a number of timer events that get stopped when the form closes, however, even though the panel gets closed, the timer events are still running. It seems that the form is still active in the background.
Any help would be appreciated.
Due to a silly oversight, I failed to see that the code I was using to dispose of all the controls and close the form was not in the FormClosing event, but actually under a Command Button called Close. Therefore, my closing procedures were never getting called and thus the timers remained active.
Thanks

How to apply "Anchor" property

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.

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?