Passing arguments from a dynamic control to AddHandler in VB.NET - vb.net

I'm trying to figure out how to pass information from one form to another using an AddHandler that I create in a dynamic control.
I have a loop something like
Dim I As Integer
For I = 0 To 10
Dim gbNew As New GroupBox()
Dim pbNew As New PictureBox()
Dim llbNew As New Label()
Dim tlbNew As New Label()
Dim olbNew As New Label()
Dim slbNew As New Label()
Dim wlbNew As New Label()
UserName = dt.Rows(I)("UserName").ToString()
Status = dt.Rows(I)("LastJobType").ToString()
JobType = dt.Rows(I)("LastJobType").ToString()
LLocation = dt.Rows(I)("LastLocation").ToString()
TimeIn = dt.Rows(I)("LogInTime")
TimeOut = dt.Rows(I)("LogOutTime")
FlowLayoutPanel1.Controls.Add(gbNew)
gbNew.Controls.Add(llbNew)
llbNew.Visible = True
llbNew.Text = LLocation
llbNew.Font = New Font(llbNew.Font.FontFamily, 6.5)
llbNew.Location = New System.Drawing.Point(3, 25)
llbNew.BorderStyle = BorderStyle.None
llbNew.TextAlign = ContentAlignment.MiddleLeft
llbNew.Size = New Size(80, 15)
gbNew.Size = New System.Drawing.Size(270, 80)
'gbNew.BackColor = System.Drawing.Color.Silver
gbNew.Visible = True
gbNew.Text = UserName & " " & I + 1
AddHandler gbNew.Click, AddressOf ShowForm
Next
The eventhandler fires off a sub ShowForm:
Private Sub ShowForm()
Details.Show()
End Sub
This in turn pops up a form, but I can't figure out how to pass a few needed bits of information from the dynamic generated control to a static control outside the loop.
I was using a static control in a form:
label1.text = "something"
And I opened the new form, and I could read that into the new form using something like
dim info as string = form1.label.text. But since it is dynamic, I don't have a label1.text. Instead, I have a llbNew.Text which seems to be something I can't call from form2 :(
How can I pass information from form1's dynamic control to form2?
Please keep this to VB.NET, not C#, as I barely understand VB.NET let alone trying to brain convert from C# which I have zero knowledge of.

Here's a direction you could take. I hope it is clear:
For I = 0 To 10
(...)
gbNew.Text = UserName & " " & I + 1
gbNew.Tag = dt.Rows(I) ' Any information that you need here
AddHandler gbNew.Click, AddressOf ShowForm '(No changes here)
Next
' Use the appropriate Event Handler signature # the handler Sub
Private Sub ShowForm(sender as Object, e as EventArgs)
Dim groupBoxClicked as GroupBox = TryCast(sender, GroupBox)
If groupBoxClicked IsNot Nothing
Dim detailsForm as New Details()
detailsForm.ParentInformation = groupBoxClicked.Tag
detailsForm.ShowDialog()
End If
End Sub
(...)
Public Class Details ' Your Details Form
Public Property ParentInformation as DataRow
End Class

Related

Dynamically generated buttons from XML always have the same attributes of the last node in XML file

Using the below function, a series of buttons are generated into a flow layout panel based on an existing XML document.
The function is called at program load, and successfully generates buttons with different attributes, until they are clicked.
When the buttons are clicked, they should output it's attributes to a data grid view panel, but it only enters the attributes of the last node in the XML document.
Function loadMenuItems() As Double
m_xmld = New XmlDocument
m_xmld.Load("Menu.xml")
m_nodelist = m_xmld.GetElementsByTagName("menuItems")
For Each m_node In m_nodelist
Dim newButton As New Button
strID = m_node.Item("ID").InnerText
strName = m_node.Item("Name").InnerText
strPrice = m_node.Item("Price").InnerText
strOptions = m_node.Item("Options").InnerText
newButton.Name = "BTN_" & strID
newButton.Width = 150
newButton.Height = 150
newButton.BackgroundImageLayout = ImageLayout.Zoom
newButton.TextImageRelation = TextImageRelation.TextAboveImage
newButton.ForeColor = Color.White
newButton.Text = strName
AddHandler newButton.Click, Sub()
DGV_Receipt.Rows.Add(strName, strOptions, strPrice)
End Sub
newButton.BackgroundImage = Image.FromFile(".\Resources\Icons\" & strName & ".png")
FLP_Icons.Controls.Add(newButton)
Next
End Function 'end the function definition.
The function being loaded:
Private Sub FORM_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loadMenuItems()
End Sub
I am a beginner at VB, so if I'm missing something obvious, please let me know!
As suggested in the comments, the issue is your event handler. There is some nuance in how Lambda expressions work and you are running afoul of that.
One option would be to store the correct data in the Tag property, which is a general-purpose data field, of each Button, e.g.
newButton.Tag = {strName, strOptions, strPrice}
and then get the data back from the Button in the event handler:
AddHandler newButton.Click, Sub(sender, e)
Dim btn = DirectCast(sender, Button)
Dim data = DirectCast(btn.Tag, String())
DGV_Receipt.Rows.Add(data(0), data(1), data(2))
End Sub
Alternatively, define your own custom class that inherits Button, add dedicated properties for those values and then use them in your code. That custom class will work just like a regular Button but you can set those properties when you create it and then get the data back from them in the event handler.

Wait after showing MDI child form - alternative to DoEvents

A co-worker made some improvements to my app, but at the same time, he changed the whole app to use MDI child forms. So where I used to be able to use ShowDialog() to make the subroutine wait until the new form closed to continue, now I can't. I adapted some old VB6 code to try and replicate the same functionality, but it uses DoEvents() and I've been told to avoid that at all costs.
At this point I can't help but feel like there's just a fundamental flaw in my methodology here, and that there is probably a far simpler way to accomplish the same task.
Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.myResultText = txbNodeClick.Text
}
Frm.Show()
Call WaitOnFormUnload()
txbNodeClick.Text = Frm.myResultText
End Sub
Public Sub WaitOnFormUnload()
Dim bIsLoaded As Boolean = True
Dim FormName As String = Me.MdiParent.ActiveMdiChild.Name ' Form.ActiveForm.Name
Do While bIsLoaded
bIsLoaded = False
For Each f As Form In Application.OpenForms
If f.Name = FormName Then
bIsLoaded = True
Application.DoEvents()
Exit For ' breaks the for loop
End If
Next f
Loop
End Sub
Goes a something like this:
Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.myResultText = txbNodeClick.Text
}
AddHandler Frm.FormClosing, AddressOf RetrieveBlahData
Frm.Show()
End Sub
Private Sub RetrieveBlahData(sender as Object, e as FormClosingEventArgs)
txbNodeClick.Text = DirectCast(sender, frmVariableEditor).myResultText
End Sub
Probably doesn't get more minimal than that, except for maybe sharing a single data object:
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.DataSetToBindTo = Me.DataSetWhatever
}
You'll have to re-code your form so that it takes this passed in dataset (or other data container) and gets its data from it (databinding) .. then as the editor form is changing the data, it's actually changing the original dataset in the main form too, because they're the same thing

Fire Panel's Click event

I was looking for how to automatically fire a Click event for a Panel. I found code to do this for a Button and it is not the same.
My problem is: I am making an auto gallery generator and I want fire the event when you click the parent block which contains the id for the user. The problem is when I click the event the image and the label are before block, then if I click image or label I want that block is clicked.
Here is my code:
Public Class users
Private Sub users_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' COMPROVE CONNECTION
Try
' ESTABLISH CONNECTION TO SERVER
conn = New DataBase(DbServer, DbName, DbUser, Password)
' PICK UP DATA FROM USERS
Dim DataTable As New DataTable
DataTable = conn.ConsultSql("SELECT `id_user`,`full_name`,`img` FROM `user` WHERE `user_type` = 1")
' PRINT PHARMACIST
Dim b As Integer = 0
While b < DataTable.Rows.Count
Dim RowData As DataRow = DataTable.Rows(b)
PrintUsers(RowData)
b += 1
End While
Catch ex As Exception
MessageBox.Show("OcurriĆ³ el siguiente error: " & ex.Message, "Error al loguearse", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub PrintUsers(ByVal RowData As DataRow)
Dim iNumber As String
Console.WriteLine(RowData("full_name"))
iNumber = RowData("full_name")
' GENERATE ID
Dim oPaneldId As New Panel
oPaneldId.Name = RowData("id_user")
' GENERATE TEXT NAME
Dim oTextField As New Label
oTextField.Name = "name" & iNumber
oTextField.Text = iNumber
' GENEREATE PHOTO
Dim oPictureBox As New PictureBox
oPictureBox.Name = "img" & iNumber
Dim MyWebClient As New System.Net.WebClient
'BYTE ARRAY HOLDS THE DATA
Dim ImageInBytes() As Byte = MyWebClient.DownloadData(RowData("img"))
'CREATE A MEMORY STREAM USING THE BYTES
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
'CREATE A BITMAP FROM THE MEMORY STREAM
oPictureBox.Image = New System.Drawing.Bitmap(ImageStream)
oPictureBox.Size = New Size(100, 130)
' ADDING TO THE BODY
oPaneldId.Controls.Add(oTextField)
oPaneldId.Controls.Add(oPictureBox)
AddHandler oPaneldId.Click, AddressOf Me.SelectUser
AddHandler oPictureBox.Click, AddressOf Me.ClickFather
AddHandler oTextField.Click, AddressOf Me.ClickFather
body.Controls.Add(oPaneldId)
End Sub
Public Sub SelectUser()
Console.WriteLine("Funciona")
End Sub
Public Sub ClickFather(sender As Object, e As EventArgs)
Console.WriteLine(sender.GetType.Name)
Console.WriteLine(sender.parent.Name)
sender.parent.click
End Sub
End Class
Add the click event handlers for the other controls in the panel to the same event handler you use for the panel`s click event. So your code would change to
AddHandler oPaneldId.Click, AddressOf Me.SelectUser
AddHandler oPictureBox.Click, AddressOf Me.SelectUser
AddHandler oTextField.Click, AddressOf Me.SelectUser

Modification of the UltraDateTimeEditor background

I need to set the background color of Infragistics.Win.UltraWinEditors.UltraDateTimeEditor to yellow when the time is not today, for example. Also, I need to show a warning message when I move the cursor over the editor. I know XAML has such property I can use. How about in winform?
A quick example on how you could accomplish your goal. I am pretty sure that there are other ways to get this done, but this works
' Globals controls and Forms
Dim f As Form
Dim dt As Infragistics.Win.UltraWinEditors.UltraDateTimeEditor
Dim tt As Infragistics.Win.ToolTip
' This Main has been built using LinqPAD, you could have problems
' running it, as is in a WinForms project, but the concepts are the same
Sub Main()
dt = New UltraDateTimeEditor()
dt.Dock = DockStyle.Top
' Add the event handlers of interest to the UltraDateTimeEditor
AddHandler dt.Validating, AddressOf onValidating
AddHandler dt.Enter, AddressOf onEnter
tt = New Infragistics.Win.ToolTip(dt)
' Just another control to trigger the OnEnter and Validating events
Dim b = New Button()
b.Dock = DockStyle.Bottom
f = New Form()
f.Size = New Size(500, 500)
f.Controls.Add(dt)
f.Controls.Add(b)
f.Show()
End Sub
Sub onValidating(sender As Object , e As EventArgs)
' Some condtions to check and then set the backcolor as you wish
dt.BackColor = Color.Yellow
End Sub
Sub onEnter(sender As Object, e As EventArgs)
' Set the background back
dt.BackColor = Color.White
' Some condition to check to display the tooltip
If dt.DateTime <> DateTime.Today Then
tt.ToolTipText = "Invalid date"
' Time to leave the message visible
tt.AutoPopDelay = 2000
tt.DisplayStyle = ToolTipDisplayStyle.BalloonTip
' Calculation to set the tooltip in the middle of the editor
Dim p = New Point(dt.Left + 50, dt.Top + (dt.Height \ 2))
p = dt.PointToScreen(p)
' Show the message....
tt.Show(p)
End If
End Sub

Remove LineShape from Windows Form, LineShape and ShapeContainer Arrays

I am using the code below to add multiple LineShape controls to a Windows Form. Note the globally declared mLineShapes() and mShapeContainter() arrays (at bottom of code) which store each new LineShape object once it's created.
At present, I have been unsuccessful at removing a given LineShape control from the form (even if I know its array index), and also cannot removing an array element without causing a Nothing for the removed element. Obviously, once I remove the element from these arrays, it requires that all the remaining elements with greater indices are copied to lower values to fill in the Nothing voided element. Given these circumstances, can lists be used instead of the mLineShapes() and mShapeContainer() arrays?
enter code here' create new ShapeContainer
Dim sSCTemp As New ShapeContainer
' add ShapeContainer to Form
sSCTemp.Parent = Me
' create new LineShape
Dim sLSTemp As New LineShape
sLSTemp.BorderColor = Color.Black
sLSTemp.BorderWidth = 2
sLSTemp.Cursor = Cursors.Cross
' add LineShape to ShapeContainer
sLSTemp.Parent = sSCTemp
' set starting and ending coordinates for the line
sLSTemp.StartPoint = New System.Drawing.Point(siSCCount * 20, 60 + siSCCount * 60)
sLSTemp.EndPoint = New System.Drawing.Point(100 + siSCCount * 20, 110 + siSCCount * 60)
' set new LineShape to top of z-order
sLSTemp.BringToFront()
sSCTemp.BringToFront()
' connect ContextMenuStrip to LineShape
sLSTemp.ContextMenuStrip = mLsCtm1
' add new LineShape to arrays
ReDim Preserve mLineShapes(siSCCount)
ReDim Preserve mShapeContainer(siSCCount)
mLineShapes(siSCCount) = sLSTemp
mLineShapes(siSCCount).Name = "LineShape" & siSCCount
mShapeContainer(siSCCount) = sSCTemp
mShapeContainer(siSCCount).Name = "ShapeContainer" & siSCCount
In addition to the above, the endpoints of each
LineShape are selected from the arrays so that they can be moved. An example is below:
Dim siSCId As Integer
Dim myShapeContainer As ShapeContainer
myShapeContainer = CType(sender, ShapeContainer)
Dim myLineShape As LineShape
' get index of the actual ShapeContainer in ShapeContainer array
siSCId = Array.IndexOf(mShapeContainer, sender)
If siSCId > -1 Then
myLineShape = mLineShapes(siSCId)
If MouseIsNearBy(myLineShape.EndPoint) Then
myLineShape.BorderColor = Color.Red
NearLineEndPoint = True
End If
If MouseIsNearBy(myLineShape.EndPoint) = False Then
myLineShape.BorderColor = Color.Black
NearLineEndPoint = False
End If
If (dragStartPoint) Then
myLineShape.StartPoint = New Point(oldStartPoint.X + e.X - oldMouseX, oldStartPoint.Y + e.Y - oldMouseY)
End If
End If
Therefore, If I simply add a new LineShape to the form controls without using the mLineShapes() ans mShapeControl() arrays, how can I modify the above code (which finds the LineShape in the storage arrays) so that the line can be modified? I think that if I click on a LineShape, I can get its name using .sourcecontrol or .parent?
UPDATE 5/9/2019
After right clicking on a control on Form1 and selecting the "Link" command from a ContextMenuStrip, the following method (ctmsconnect) is fired to draw a new LineShape control that the user then drags and drops on to the next control in the workflow. Question is, is the list of LineShapes ("Lines") not needed?
(in Form1 class declarations):
Dim SC As New ShapeContainer
Dim Lines As New List(Of LineShape)
Private Sub ctmsconnect_Click(sender As System.Object, e As System.EventArgs) Handles ctmsconnect.Click
mLineWidth = 1
Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)
Dim x As Integer = cms.SourceControl.Right - 2
Dim y As Integer = cms.SourceControl.Top + (cms.SourceControl.Height / 2 - 12)
Dim LS As New LineShape
NumLineShapes += 1
LS.Name = "LineShape" & NumLineShapes
LS.BorderColor = Color.Black
LS.BorderWidth = 2
'Set starting and ending coordinates for the line
LS.StartPoint = New Point(x, y)
LS.EndPoint = New Point(x + 80, y - 5)
'Set new LineShape to top of z-order
LS.BringToFront()
Dim nxgContextMenuStrip As New ContextMenuStrip
LS.ContextMenuStrip = nxgContextMenuStrip
LS.Tag = "LineShape" & NumLineShapes & "_Delete"
'Attach an event handler for the ContextMenuStrip control's Opening event.
AddHandler nxgContextMenuStrip.Opening, AddressOf cms_Opening
numconnectedlineendpoints += 1
Dim myValues As New List(Of String)
myValues.Add(cms.SourceControl.Name)
DropLineOriginalObjectName = cms.SourceControl.Name
OrigDropControl = cms.SourceControl
myValues.Add(LS.Name)
myValues.Add("linestart")
dicGUIControls.Add(numconnectedlineendpoints, myValues)
Lines.Add(LS)
SC.Shapes.Add(LS)
Me.Refresh()
End Sub
You shouldn't need the arrays, just use the controls collection. Instead of setting the parent of the controls, you should probably add them to the collection:
'sSCTemp.Parent = Me
Me.Controls.Add(sSCTemp)
To remove them, you can reference them by the name property:
If Me.Controls.ContainsKey("ShapeContainer1") Then
Me.Controls.RemoveByKey("ShapeContainer1")
End If
The shape controls inside the ShapeContainer have to be accessed through the Shape collection:
If Me.Controls.ContainsKey("ShapeContainer1") Then
Dim sc As ShapeContainer = DirectCast(Me.Controls("ShapeContainer1"), ShapeContainer)
If sc.Shapes.ContainsKey("LineShape2") Then
sc.Shapes.RemoveAt(sc.Shapes.IndexOfKey("LineShape2"))
End If
End If
Example of reading the StartPoint and EndPoint properties:
Dim sb As New StringBuilder
For Each ls As LineShape In Me.ShapeContainer1.Shapes
sb.AppendLine(ls.StartPoint.ToString & " - " & ls.EndPoint.ToString)
Next
MessageBox.Show(sb.ToString)
Note: ShapeContainer Class makes a special note:
Be careful that you do not create more than one ShapeContainer for each form or container; doing this may introduce unexpected behavior. If you add a design-time line or shape control to a form or container after you write code to create one programmatically, you should modify that code to use the ShapeContainer created by the designer.
Public Class Form1
Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
' Set the form as the parent of the ShapeContainer.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
canvas.Parent = Me
' Set the ShapeContainer as the parent of the LineShape.
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If RadioButton1.Checked = True Then
Dim line1 As New Microsoft.VisualBasic.PowerPacks.LineShape
line1.Parent = canvas
' Set the starting and ending coordinates for the line.
line1.StartPoint = New System.Drawing.Point(Me.Width / 2, 0)
line1.EndPoint = New System.Drawing.Point(e.X, e.Y)
TextBox1.Text = canvas.Shapes.Count.ToString
line1.Name = "MyShape"
canvas.Shapes.Add(line1)
AddHandler line1.Click, AddressOf LineClick
End If
End Sub
Private Sub LineClick(sender As Object, e As EventArgs)
' Here is where we take the object that is sender from the arguments and cast it to its specific control
If RadioButton2.Checked = True Then
' I could just as easily use
CType(sender, PowerPacks.LineShape).Dispose()
TextBox1.Text = canvas.Shapes.Count
End If
End Sub
End Class