winforms: Slow performance on form load - vb.net

I have a VB.NET winforms application. One of its forms, contains a flowlayoutpanel which I populate dynamically (programmatically) on form load event with pairs of label-textbox. My flowlayoutpanel is created in design time with all the properties set to default except below ones:
AutoSize: true
AutoScroll: true
Dock: fill
FlowDirection: TopDown
Then I populate it using below code within the form load event:
Private Sub MyForm_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim lbl As Label
Dim txt As TextBox
Dim flowLayout As FlowLayoutPanel
Dim g As Graphics
For i As Integer = 0 To 253
lbl = New Label
lbl.Text = i.ToString("000") + ":"
lbl.Anchor = AnchorStyles.None
lbl.AutoSize = True
txt = New TextBox
txt.Text = "<" + i.ToString.PadLeft(3, " ") + ">"
txt.MaxLength = 5
txt.Anchor = AnchorStyles.None
txt.ReadOnly = True
g = txt.CreateGraphics
txt.Width = g.MeasureString(txt.Text, txt.Font).Width + 5
g.Dispose()
flowLayout = New FlowLayoutPanel
flowLayout.FlowDirection = FlowDirection.LeftToRight
flowLayout.AutoSize = True
flowLayout.Anchor = AnchorStyles.None
flowLayout.Margin = New Padding(0)
flowLayout.Padding = New Padding(0)
flowLayout.Controls.Add(lbl)
flowLayout.Controls.Add(txt)
Me.FlowLayoutPnl.Controls.Add(flowLayout)
Next
End Sub
As I have stated above FlowLayoutPnl is created on design time and components on this are added following TopDown flow direction.
Above code produces this result.
The problem here is that on opening this form it takes so much time (a few seconds) to open because it is doing all the stuff in form load event. It takes 35 seconds to open!. When all the stuff is done then form becomes visible after 35 seconds...
So I would like to know if there is some kind of method to speed up form load. I have read some posts here saying about implement and show some kind of splash screen while it loads indicating user that it is being loaded or even perform an asynchronous load. What about creating a background thread from form load event in order to do this stuff? Anyone could guide me in the right direction? A very little example will be highly appreciated. How this kind of problems are normally resolved?

Related

How do I display an image from a DataGridView to another PictureBox in a new Windows Form?

What I'm trying yo do is once I clicked on a row in my DataGridView, clicked on the view button, and it opens a new window that displays all the data from the DataGridView to the textboxes. I already did the first part, but I don't know how to do it with the images (I have 2 image columns). Can someone help me out? I'm only starting, sorry. This is my code for the view button that opens up another window. The picture boxes are always blank.
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
If GunaDataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 0 Then
data.txtID.Text = GunaDataGridView1.CurrentRow.Cells(0).Value.ToString
data.txtLN.Text = GunaDataGridView1.CurrentRow.Cells(1).Value.ToString
data.txtFN.Text = GunaDataGridView1.CurrentRow.Cells(2).Value.ToString
data.txtMN.Text = GunaDataGridView1.CurrentRow.Cells(3).Value.ToString
data.txtGen.Text = GunaDataGridView1.CurrentRow.Cells(4).Value.ToString
data.txtNum.Text = GunaDataGridView1.CurrentRow.Cells(5).Value.ToString
data.txtDOB.Text = GunaDataGridView1.CurrentRow.Cells(6).Value.ToString
data.txtAddress.Text = GunaDataGridView1.CurrentRow.Cells(7).Value.ToString
data.txtPlate.Text = GunaDataGridView1.CurrentRow.Cells(8).Value.ToString
data.txtVT.Text = GunaDataGridView1.CurrentRow.Cells(9).Value.ToString
data.txtVB.Text = GunaDataGridView1.CurrentRow.Cells(10).Value.ToString
data.txtVYM.Text = GunaDataGridView1.CurrentRow.Cells(11).Value.ToString
data.txtSP.Text = GunaDataGridView1.CurrentRow.Cells(12).Value.ToString
data.ownerPhoto.Image = GunaDataGridView1.CurrentRow.Cells(13).Value
data.carPhoto.Image = GunaDataGridView1.CurrentRow.Cells(14).Value
data.ShowDialog()
End If
End Sub
I saw another guy asked this question and another guy commented with this code line:
Dim bytes As Byte() = DataGridView1.CurrentRow.Cells(13).Value
Using ms As New MemoryStream(bytes)
ownerPP.Image = Image.FromStream(ms)
End Using
Dim bit As Byte() = DataGridView1.CurrentRow.Cells(14).Value
Using memory As New MemoryStream(bit)
carPhoto.Image = Image.FromStream(memory)
End Using
It works perfectly. I hope this helps someone out too.

How to re-arrange multiple buttons within a panel when panel/form is resized?

I have a vb.net form that has a panel. Inside the panel, there are 40 buttons dynamically created (see code below). If user wants to resize the form, the panel will resize, but the button layout will not change (i.e. 5 columns of buttons instead of 4 when the form expands horizontally. Is there any way to make the buttons automatically arrange, like when user make the form smaller, there are only 3, or 2, or 1 columns of buttons, and when the form is getting larger, the buttons will arrange to 5 or more columns?
Here is the code of the form, and what I have done so far:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim a, b As Integer
For i = 0 To 40
Dim SampleButton As New Button With {
.Name = "Sample_Button" & "-" & i.ToString,
.Text = "Sample_Button" & "-" & i.ToString,
.Location = New Point(a, b),
.Font = New Font("Segoe UI", 11, FontStyle.Regular),
.Height = 50,
.Width = 180,
.FlatStyle = FlatStyle.Flat,
.TextAlign = ContentAlignment.MiddleCenter,
.Cursor = Cursors.Hand
}
a += SampleButton.Width + 10
If (a + SampleButton.Width) > Panel_MainMenu.Width Then
a = 10
b += SampleButton.Height + 10
End If
SampleButton.BackColor = Color.FromArgb(255, 232, 251, 232)
Panel_MainMenu.Controls.Add(SampleButton)
Next
End Sub
Private Sub Panel_MainMenu_Layout(sender As Object, e As LayoutEventArgs) Handles Panel_MainMenu.Layout
'For Each Controls In Panel_MainMenu.Controls
'Next
End Sub
End Class
What I have done:
Anchor Panel_MainMenu to top, left, right, and bottom (it helps resize the panel according to the size of the form, but not the buttons within it)
Panel_MainMenu.AutoScroll = True
Panel_MainMenu.AutoSize = False and Panel_MainMenu.AutoSizeMode = GrowOnly
I searched on Google on how to use Panel.Layout, but I have not figured out a way to parse the button layout to Panel.Layout to let them adjust accordingly.
I set Panel_MainMenu's minimum size to 776, 426
Any help would be greatly appreciated. Thanks for reading my post!
Simply change from using a Panel to a FlowLayoutPanel:
Represents a panel that dynamically lays out its contents horizontally
or vertically.
You will then not need to specify a Location for each button since this will be taken care of for you.
Anchor/Dock the FlowLayoutPanel so that it resizes with the form, then the buttons will automatically be arranged.

Rows Added to a Datagridview not being captured in Screen Capture

I am running into an issue where I am attempting to add some blank rows to a datagridview before printing the form for use.
I am having an issue trying to understand where I need to put the code to add the rows to the data gridview so they are added and captured before the user hits print. My concern is that I am not adding the rows in the right spot before the screen is captured however the code to add the rows is run before the code to hide the buttons is implemented however the buttons are removed and the rows are not added in the actual printed image.
If that is not clear hopefully these images will make more sense. At this point I am not sure why the buttons being hidden are being captured but the added rows are not when based on the code it looks like the rows are being added before the buttons are hidden.
Thank you.
Here is the Image before the user hits print
Here is the Image after the user hits Print
And here is what actually printed
And here is the code in question that captures the screen
Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
If dgvReceive.Rows.Count < 27 Then
Dim rowstoadd As Integer = 0
rowstoadd = 27 - dgvReceive.Rows.Count
dgvReceive.Rows.Add(rowstoadd)
End If
If MemoryImage IsNot Nothing Then
MemoryImage.Dispose()
End If
MemoryImage = New Bitmap(s.Width, s.Height, myGraphics)
For Each btn As Button In Me.Controls.OfType(Of Button)
btn.Visible = False
Next
dgvReceive.ClearSelection()
lblTitle.Select()
Me.FormBorderStyle = FormBorderStyle.None
Dim memoryGraphics As Graphics = Graphics.FromImage(MemoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
Me.FormBorderStyle = FormBorderStyle.Sizable
For Each btn As Button In Me.Controls.OfType(Of Button)
btn.Visible = True
Next
End Sub

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?

Add tooltip control dynamically

I have a child form that is completely created in code.
I would like to add tool tips to the textbox controls on the form. I know how to set up the tool tips on the fly but can't find a way to add the tooltip control to the form on the fly. All the hits I find on google refer to dragging the control from the designers toolbox.
I would need to do something like:
' Add tool tip control
Dim toolTip1 As New ToolTip()
toolTip1.ShowAlways = True
frm.Controls.Add(toolTip1)
This does not work
I tried adding a sub to handle the from.load event(with a handler that poitn to the sub) at design time but can not get passed the error "Tooltip1 is not declared" when adding the tooltip to the iundividual dynamic controls.
If I call this dynamic form from a parent form and add the tooltip control to the parent form I can use it for the child form. But how would I do this if I was creating the form from a routine that does not live on a parent form?
thanks
Dim frm As New Form
' Add tool tip control
''Dim toolTip1 As New ToolTip()
''toolTip1.ShowAlways = True
'Draw the Form object
'close the dynamic frm if existing already
If frm IsNot Nothing Then
frm.Close()
End If
frm = New Form()
frm.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
frm.Name = "frm_test"
'dimension is irrelevant at the moment
frm.ClientSize = New System.Drawing.Size(10, 10)
'the parent will be the current form
'frm.MdiParent = this;
'splash screen mode form, why not...
frm.ControlBox = True
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
frm.BackColor = System.Drawing.Color.LightGray
For Each item As MYFILE.Class in the Collection
Dim aTextBox As New TextBox()
aTextBox.Font = New System.Drawing.Font(sFont, Single.Parse(sSizeFont), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CByte(0))
aTextBox.BackColor = System.Drawing.Color.Yellow
aTextBox.Location = New System.Drawing.Point(iNewColumnPosition + 5 + intMaxWidthLabel, intVertPos)
aTextBox.Size = New System.Drawing.Size(intWidthTextBox + 10, intGapHeight)
'store the biggest width, so that the textboxes can be vertically aligned
If intWidthTextBox > intMaxWidthText Then
intMaxWidthText = intWidthTextBox
End If
'giving a name to all your object will be the only way
'to retrieve them and use them
'for the purpose of this sample, the name can be the
'same for all textboxes.
aTextBox.Name = item.ParameterName
'giving the maximun size in caracters for the textbox.
aTextBox.MaxLength = Integer.Parse(item.ParameterLength)
toolTip1.SetToolTip(aTextBox, "TIP" & intIndex.ToString)
'tab have to be ordered
aTextBox.TabIndex = intIndex
intIndex += 1
'Vertical position is to be manage according the
'tallest object in the form, in this case the
'textbox it self
intVertPos += intGapHeight
'adding the textbox to the form
frm.SuspendLayout()
aTextBox.SuspendLayout()
frm.Controls.Add(aTextBox)
Next
I left a lot of code out but this should give you an idea as to what I am doing
In vb it is
Dim tooltip As New ToolTip(components)
tooltip.SetToolTip(textBox1, "This is a textbox tooltip")
Sorry this is not in VB.NET but I am pretty sure you can easily convert this. The first line is to set ToolTip control to your form components. The second is how you set a tooltip to a control and give it the related text.
ToolTip tooltip = new ToolTip(components);
tooltip.SetToolTip(textBox1, "This is a textbox tooltip");