Dynamic UserControl Not Showing - flowlayoutpanel - vb.net

I've array of items and for each item, creating a new instance of custom user control. The problem is that when the control is created and added to Flowlayoutpanel, it's not showing up.
Code
Sub createList()
Dim len_ids As Integer = account_ids.Length - 1
Dim x As Integer = 1
While x <= len_ids
Dim listItem As New wrapperItems()
listItem.txtName.Text = account_names(x)
listItem.picIcon.BackgroundImage = Image.FromFile(account_icons(x))
listItem.lblPath.Text = account_paths(x)
listItem.Tag = "listItem" & x
listItem.Top = 10 * x
listItem.Left = 10
Me.flowWrapper1.Controls.Add(listItem)
listItem.Show() ' tried this
listItem.Visible = True 'tried this
listItem.BringToFront() ' tried this
x += 1
End While
Me.Refresh()
End Sub
Anyone has idea of why it's not working?

It seems that when the background worker is started on Form.Show instead of Form.Load, the code doesn't work.
For the fix, I added background worker to Form.Load().
The better way was finally to add a refresh list button and then load the background worker.

Related

Adding labels and other controls to a groupbox dynamically in vb.net not working?

So I've been having problems adding labels from a record structure into a groupbox that stack below each other vertically. I could use a Flow Layout Panel but I have multiple labels I need to add from data in a record structure which I want to be able to scroll later with a vertical scrollbar as the autoscroll will only work with one panel only, and not all of them.
For some reason the program puts only one label in, and the others don't seem to be visible even though they have been created (I used a messagebox to check). Could someone please help me to put them in, as I am fairly new to programming and need help.
For context, the program loads 'materials' from an xml file and stores it in a record structure, then this bit of the program creates labels and radio buttons dynamically and puts it in a groupbox, so that it can be arranged manually on a pretty grid that will all become scrollable by using a scrollbar later.. Because the data I am working with is all related, and can have strings of multiple sizes (names and suppliers of materials for example) I didn't want to attempt to add whitespace to make the grid work but with a single label (not that the others would show beneath the first for some reason)
CODE:
'sets up Labels for editing materials
prgFunctions.loadMat()
Dim counter As Integer
Dim newMatIDLabel(numMatFile) As Label
Dim newMatRdb(numMatFile) As RadioButton
Dim lastPos As Integer
'testing to see if materials load GET RID OF LATER
' For counter = 1 To numMatFile
'ListBox1.Items.Add(materials(counter).matName)
' Next
'create the labels with information
For counter = 1 To numMatFile
'ID
newMatIDLabel(counter) = New Label
newMatIDLabel(counter).Name = "lblMatIDNum" & counter
newMatIDLabel(counter).Text = materials(counter).matID
newMatIDLabel(counter).Parent = gbxMaterials
' MsgBox(newMatIDLabel(counter).Name & " " & newMatIDLabel(counter).Text)
Next
'create the checkboxes NOW RADIO BUTTONS
For counter = 1 To numMatFile
newMatRdb(counter) = New RadioButton
newMatRdb(counter).Name = "chkMatSelectNum" & counter
newMatRdb(counter).Text = ""
newMatRdb(counter).Parent = gbxMaterials
Next
'matID locations
lastPos = 10
For counter = 1 To numMatFile
'SOMEHOW MOVE IT INTO THE GROUPBOX INSTEAD, as issues arise everywhere
newMatIDLabel(counter).Location = New Point(7, lastPos + 10)
lastPos = lastPos + 10
Next
Why not just:
lastPos = 10
For counter = 1 To numMatFile
Dim label As New Label
label.Name = "chkMatSelectNum" & counter
label.Text = materials(counter).matID
label.Location = New Point(7, lastPos)
label.Visible = True
gbxMaterials.Controls.Add(label)
lastPos += label.Height
Next
Based on F0r3v3r-A-N00b's answer:
The label has a height of 23, so it seems it is hiding the others.
This works for me:
Dim lastPos As Integer = 20
For counter As Integer = 1 To numMatFile
Dim label As New Label
label.Name = "chkMatSelectNum" & counter
label.Text = materials(counter).matId
label.AutoSize = True
label.Visible = True
label.Location = New Point(7, lastPos)
gbxMaterials.Controls.Add(label)
lastPos += 17
Next

Win32Exception was unhandled when switching tabs, in form VB.NET

I have a form with a tabcontrol, the tabscontrol starts with a single tab and more are added dynamically if need be. Each tab holds around 20 seperate panels which are used to represent pages. It can handle 12 tabs fine and continue adding more way past 40. However, whenever you click on a tab to load it up it throws this error.
System.ComponentModel.Win32Exception.
With no location in the code to wear it crashed.
I've tried this on multiple machines and the same thing happens.
This is the code to adding a new tab.
Public Sub CreateAnotherPage5(NoOfPage5 As Integer)
For i = 1 To NoOfPage5
Dim PG5E As New ConditionReportPage5
Dim PREV As ConditionReportPage5
If NoOfPages Mod 20 = 0 Then
Dim NwTabPG As New TabPage
Dim NwPanel As New PanelAutoScroll
NoOfTabs = NoOfTabs + 1
NwTabPG.Name = "tpg_" & NoOfTabs
NwPanel.Name = "pnl_" & NoOfTabs
NwTabPG.Text = NoOfTabs
NwPanel.Dock = DockStyle.Fill
NwPanel.AutoScroll = True
frm_Main.tbc_Main.TabPages.Add(NwTabPG)
NwTabPG.Controls.Add(NwPanel)
PG5E.Location = New Point(frm_Main.FirstPageX, frm_Main.FirstPageY)
NwPanel.Controls.Add(PG5E)
NoOfPages = 1
Else
Dim PrevPanl As PanelAutoScroll
Dim PrevTabPG As TabPage
PrevTabPG = frm_Main.tbc_Main.Controls.Item("tpg_" & NoOfTabs)
PrevPanl = PrevTabPG.Controls.Item("pnl_" & NoOfTabs)
PREV = TryCast(PrevPanl.Controls(NoOfPages - 1), ConditionReportPage5)
PG5E.Location = New Point(frm_Main.FirstPageX, Math.Abs(PREV.Location.Y + 1000))
PrevPanl.Controls.Add(PG5E)
NoOfPages = NoOfPages + 1
End If
ExtensionConfig.DoubleBuffer(PG5E, True)
PG5E.SetNewDef()
PG5E.PageNo = NoOfPages
End sub
Any suggestions?

Sending text to projector in VB. NET

I have a need to send text inside richtextbox to projector screen. I have tried to create a new form and make it fullscreen but I cannot yet figure out how I can communicate the text projector as in EasyWorship or PowerPoint. I will appreciate any suggestion. Thanks
I think you don't have to communicate with projector itself.
You can list the available screens, in order to send that form to the correct one (secondary screen). I mean, something like this:
Dim oYourScreen As Screen = nothing
If Screen.AllScreens.Length > 1 Then
For iAux As Integer = 0 To Screen.AllScreens.Length - 1
If Not Screen.AllScreens(iAux).Primary Then
oYourScreen = Screen.AllScreens(iAux)
Exit For
End If
Next
End If
If oYourScreen Is Nothing Then
oYourScreen = Screen.AllScreens(0)
End If
Me.StartPosition = FormStartPosition.Manual
Dim x, y As Integer
x = oYourScreen.Bounds.Location.X + 100 ' adjust as you want
y = oYourScreen.Bounds.Location.X + 30
Me.Location = oYourScreen.Bounds.Location ' or New Point(x, y)

Space out multiple dynamically created usercontrols

So what im trying to do is create multiple instances of my usercontrol
Though I'm stuck on finding a way for them to be next to eachother like have a 5 pixel space between the next one created.
One way is to have a function that dynamically calculates it. I haven't tested this, but it should work. You just call doGetAppXPosition which loops thru all instances of your control and takes the last one found as the next x-position.
Dim App As New AppTab()
With App
...
.Location = New Point(doGetAppXPosition, 5)
...
End With
Me.Controls.Add(App)
Private Function doGetAppXPosition() as Integer
Dim xpos as Integer = 5
For Each oControl As Control In YourForm.Controls
If TypeOf oControl Is AppTab Then
xpos = oControl.Right + 5
End If
Next
Return xpos
End Function

DataGridView not Refreshing/Updating/Reloading Data. After Child form closes

This is a VB.NET, Winforms App. I have a datagridview on "Form1" that uses a databinding.datasource which is an Entity Framework table. I fill the datagridview with the below function on Form1:
Sub PM_UnitViewGrid()
Try
_form1.UnitsBindingSource.DataSource = db.units.Where(Function(f) f.propertyId = _form1.CurrentPropertyId).OrderBy(Function(F) F.unitNumber)
_form1.UnitDataGridView.DataSource = _form1.UnitsBindingSource.DataSource
Dim iCount As Integer = _form1.UnitDataGridView.RowCount
For x As Integer = 0 To iCount - 1
If Not IsNothing(_form1.UnitDataGridView.Rows(x).Cells(4).Value) Then
Dim tid As Integer = _form1.UnitDataGridView.Rows(x).Cells(4).Value
Dim _ten As tenant = db.tenants.Single(Function(f) f.Occupantid = tid)
_form1.UnitDataGridView.Rows(x).Cells(1).Value = _ten.first_name + ", " + _ten.last_name
Else
Dim btnColumn As DataGridViewButtonCell = CType(_form1.UnitDataGridView.Rows(x).Cells(1), DataGridViewButtonCell)
btnColumn.Style.BackColor = Color.Green
_form1.UnitDataGridView.Rows(x).Cells(1).Value = "VACANT"
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return
End Sub
This works great and also assigns the needed values to an unbound column. The problem is that the cells(1) is a button. Which when clicked takes the user to another form as a new dialog window. The function for which is below. However, once the changes are made in that form I need for the datagridview to refresh the data that its using from the database and show the correct data. As it stands right now the values are not updating on the datagridview unless the app is completely exited and restarted. Nothing I have found seems to work and Refresh and Update only redraw the control. I need the underlying datasource to refresh and then the datagridview once the child form is exited.. This has had me stumped for a good 36 hours now and I am lost as to why nothing I am trying is working. ANY and all help would be greatly appreciated.
The sub that loads the child form based on the cells(1) button clicked is as follows:
Private Sub UnitDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles UnitDataGridView.CellContentClick
UnitDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
Dim y As DataGridViewCellEventArgs = e
Dim Tid As Integer = Nothing
If e.ColumnIndex = 1 Then
If Not e.RowIndex = -1 Then
If Not IsNothing(UnitDataGridView.Rows(e.RowIndex).Cells(4).Value) Then
currentTenent = UnitDataGridView.Rows(e.RowIndex).Cells(4).Value
TenentIdentification = currentTenent
If Not IsNothing(e) Then
If Not IsNothing(UnitDataGridView.Rows(e.RowIndex).Cells(4).Value) Then
Tid = UnitDataGridView.Rows(e.RowIndex).Cells(4).Value
Dim _ten As tenant = db.tenants.Single(Function(f) f.Occupantid = Tid) 'tenant is a table entity
TenantViewSubs.tenId = _ten.Occupantid
Dim t As New TenantView
t.tenId = tid
t.ShowDialog()
End If
End If
PropertyManagSubs.PM_UnitViewGrid() 'This is the function that is above that fills the datagridview
Else
Dim uTview As New UnassignedTenants
uTview.selectedProperty = selectedProperty 'selectedProperty is Integer
uTview.ShowDialog()
PropertyManagSubs.PM_UnitViewGrid() 'This is the function that is above that fills the datagridview
End If
End If
End If
End Sub
I tried each of the following code blocks after the t.ShowDialog() line with no change at all.
UnitDataGridView.Refresh()
.
UnitsBindingSource.Dispose()
UnitsBindingSource.DataSource = db.units.Where(Function(f) f.propertyId = selectedProperty).OrderBy(Function(f) f.unitNumber)
UnitDataGridView.DataSource = UnitsBindingSource.DataSource
.
UnitsBindingSource.DataSource = nothing
unitsBindingSource.DataSource = db.units.Where(Function(f) f.propertyId = selectedProperty).OrderBy(Function(f) f.unitNumber)
UnitDataGridView.DataSource = UnitsBindingSource.DataSource
I finally fixed this on my own.. It was in the way I passed my db context to the databinding..
I simply wrote the below sub:
Private Sub UpdateValues()
Dim context As New storageEntities 'storageEntities is an Entity
Dim query = context.units.Where(Function(F) F.propertyId = selectedProperty).OrderBy(Function(f) f.unitNumber)
UnitDataGridView.DataSource = query
End Sub
Then anytime a child form updated data I simply call
UpdateValues()
After the dialog box closes.
This may help someone else with the same problems so that is why I am posting it.