Dynamically create and remove a control from a form, many times - vb.net

The below subroutine, when called using a mouse click, successfully creates and then removes a control. but it doesn't create it a second time. I'm assuming it is because the label is not longer dimensioned as public. ie Dim lblDebug1 As New Label is at the top variable section of the form.
However when I put Dim lblDebug1 As New Label in the subroutine the dispose request doesn't work. Is there someway that I can keep creating and disposing a control?
In the below sub, booleanDebug is used to switch back and forth between creating it and disposing it. Thanks in advance.
Dim lblDebug1 As New Label
booleanDebug = Not booleanDebug
If booleanDebug Then
Me.Controls.Add(lblDebug1)
lblDebug1.BackColor = Color.BlueViolet
Else
lblDebug1.Dispose()
End If

Ensure the label has a global context. Within the form that owns it and that you have all the appropriate size and coordinates information and visibility set.
Here is some sample code that worked for me. First just create a new windows form then add a button control in the middle of the form then use the following code.
Public Class Main
Private labelDemo As Windows.Forms.Label
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.SuspendLayout()
If labelDemo Is Nothing Then
labelDemo = New Windows.Forms.Label
labelDemo.Name = "label"
labelDemo.Text = "You Click the Button"
labelDemo.AutoSize = True
labelDemo.Left = 0
labelDemo.Top = 0
labelDemo.BackColor = Drawing.Color.Violet
Me.Controls.Add(labelDemo)
Else
Me.Controls.Remove(labelDemo)
labelDemo = Nothing
End If
Me.ResumeLayout()
End Sub
End Class

Once you've Disposed a control, you can't use it any more. You have two choices here:
Choice 1: Just Remove the control from the form rather than disposing it:
'Top of the file
Dim lblDebug1 As New Label
'Button click
booleanDebug = Not booleanDebug
If booleanDebug Then
lblDebug1.BackColor = Color.BlueViolet
Me.Controls.Add(lblDebug1)
Else
Me.Controls.Remove(lblDebug1)
End If
Choice 2: Create a new control object each time
'Top of the file
Dim lblDebug1 As Label
' ^ No "New".
'We just want an object reference we can share at this point, no need for an instance yet
'Button click
booleanDebug = Not booleanDebug
If booleanDebug Then
lblDebug1 = New Label()
lblDebug1.BackColor = Color.BlueViolet
Me.Controls.Add(lblDebug1)
Else
lblDebug1.Dispose()
End If

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.

frm.showDialog dispose when openFileDialog close [vb.net]

I have two forms. First form is used to display a set of record and second form is used to edit the particular record. I called the second form using frm.ShowDialog(). Inside that form I got a button to call the OpenFileDialog. When I press OK or Cancel, then the second form dispose together with the OpenFileDialog. I'm pretty should that my code is correct, but it was the ShowDialog() problem. Anyone have idea on this issue?
This is how i called the second form from the first form to display the Information.
Private Sub btnViewOrganizationEdit_Click(sender As Object, e As EventArgs) Handles btnViewOrganizationEdit.Click, dgvOrganization.DoubleClick
Dim selectedOrganization As New Organization
'check permission because double click
If dgvOrganization.RowCount > 0 Then
strOrganizationID = dgvOrganization.SelectedRows.Item(0).Cells(0).Value
selectedOrganization = helperOrganizationCKJ.getOrganizationByID(strOrganizationID)
frmEditOrganizationCKJ.objOrganization = selectedOrganization
frmEditOrganizationCKJ.ShowDialog()
iniGridView()
End If
End Sub
This is how i called the OpenFileDialog.
Private Sub btnEditOrganizationImage_Click(sender As Object, e As EventArgs) Handles btnEditOrganizationImage.Click
dlgImage.Filter = ""
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim sep As String = String.Empty
For Each c As ImageCodecInfo In codecs
Dim codecName As String = c.CodecName.Substring(8).Replace("Codec", "Files").Trim()
dlgImage.Filter = String.Format("{0}{1}{2} ({3})|{3}", dlgImage.Filter, sep, codecName, c.FilenameExtension)
sep = "|"
Next
dlgImage.FilterIndex = 5
If dlgImage.ShowDialog(Me) = DialogResult.OK Then
'Get the image name
Dim img = dlgImage.FileName
picEditOrganizationImage.Image = System.Drawing.Bitmap.FromFile(img)
End If
End Sub
The frmEditOrganizationCKJ just dispose together with the dispose of OpenFileDialog.
Probably you have copy/pasted your btnEditOrganizationImage from a button that has the DialogResult property set to something different than DialogResult.None.
This triggers the closing action for your modal form and the fix is really simple.
Set the property DialogResult for the btnEditOrganizationImage to DialogResult.None
From MSDN on Button.DialogResult
If the DialogResult for this property is set to anything other than
None, and if the parent form was displayed through the ShowDialog
method, clicking the button closes the parent form without your having
to hook up any events. The form's DialogResult property is then set to
the DialogResult of the button when the button is clicked

Deleting controls each time ShowDialog is called

I have a form that I display exclusively with the ShowDialog method. In the Form_Shown event, I dynamically create a set of labels and text boxes based on a public variable set in the form making the call.
I understand that the form is not closed or destroyed but simply hidden between calls, and as such I added code at the top of my Form_Shown event to clear out any controls from a previous call, but the controls aren't being removed. I have tried ctrl.Dispose (as in the code below) and Me.Controls.Remove(ctrl). Neither produces an error, but the Textboxes are not removed and new ones are created over them. (For some reason,
This is the first time I've dynamically created/removed controls in .NET, so it's possible my yearning for VB6's control arrays have something to do with the error.
The form builds itself based on the calling form's public ListView variable. The calling form makes certain this variable is not nothing and that items are selected if and only if the user is editing an existing row.
Public Class frmTableEdit
Private isNew As Boolean
Private inputText() As TextBox
Private Sub FormTableEdit_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is TextBox Or TypeOf (ctrl) Is Label Then
ctrl.Dispose()
End If
Next
With frmKioskData.TBLobj
Dim fieldCount As Integer = .Columns.Count - 1
isNew = .SelectedIndices.Count = 0
'(code setting size of form and location of OK/Cancel buttons is here)
ReDim inputText(fieldCount)
For i As Integer = 0 To fieldCount
Dim lbl As New Label, txt As New TextBox
inputText(i) = txt
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
'(code setting size and and position of lbl & txt is here)
'lbl.Tag = i (I commented these lines out because I never used the
'txt.Tag = i Tag property, but can if a solution calls for it.)
lbl.Text = .Columns(i).Text
If isNew Then
txt.Text = ""
Else
txt.Text = .Items(.SelectedIndices(0)).SubItems(i).Text
End If
Next
End With
End Sub
End Class
If #Plutonix's suggestion in the comment above does not quite work for you, I think it would be easiest and make more sense to dispose of the form after calling ShowDialog, and then when you need to show that form you create a new instance with a parameter that tells it what dynamic controls to load.
So you'd have a New method in frmTableEdit that uses this parameter:
Public Sub New(ByVal fieldCount As Integer)
InitializeComponent()
fieldCount = fieldCount 'Where fieldCount is a class variable
End Sub
And when you call this form from frmKioskData, you can do so like this:
Dim newTableEdit As New frmTableEdit(Me.TBLobj.Columns.Count - 1)
newTableEdit.ShowDialog()
newTableEdit.Dispose()
Then the code in your frmEditTable's Shown event just simply has to add the controls accordingly, without the need to remove old ones:
ReDim inputText(fieldCount)
For i As Integer = 0 To fieldCount
Dim lbl As New Label, txt As New TextBox
inputText(i) = txt
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
'(code setting size and and position of lbl & txt is here)
'lbl.Tag = i (I commented these lines out because I never used the
'txt.Tag = i Tag property, but can if a solution calls for it.)
lbl.Text = .Columns(i).Text
If isNew Then
txt.Text = ""
Else
txt.Text = .Items(.SelectedIndices(0)).SubItems(i).Text
End If
Next
How you get the isNew value is up to you - you can add that as a second parameter to the form's New, or get it the same way you are now...w/e. If it were me I'd normally add that as another New parameter.

Visual Basic Dynamically Create and Erase Controls

I use Visual Basic in Visual Studio 2015 and i am trying when i click on a StripMenu to appear me some TextBoxes and Buttons.
After another click in Stripmenu i want to erase them and add new one.
My problem is in Erase (delete or clear my buttons and textboxes) controls from my surface.
I try to do it use Button.Visible =True (or False) but it's not seems to be really helpful in a big amount of controls.
Private Sub ClassAToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClassAToolStripMenuItem.Click
Label1.Text = "Sum A class Students: "
Dim btnA As Button = New Button
btnA.Location = New Point(420, 180)
btnA.Name = "Btn1"
btnA.Text = "OK"
btnA.Visible = True
Me.Controls.Add(btnA)
AddHandler btnA.Click, AddressOf button
End Sub
Private Sub button()
'What my Button does.
End Sub
I create dynamically through this code my Button but if i want to go in another Menu option i want to erase this button to add again my new controls (such us new buttons labels etc).
Your declaration is out of scope since you declared it in the menu's click method. You would have to use the Find method to get back the reference to the control you created:
Dim btn = Me.Controls.Find("Btn1", True).FirstOrDefault()
If btn IsNot Nothing Then
btn.Dispose()
End If
If you are trying to replace the contents of a panel with a new "screen" on your menu click, you can try code like this:
While Panel1.Controls.Count > 0
Panel1.Controls(0).Dispose()
End While
Dim newControl As New UserControl1
newControl.Dock = DockStyle.Fill
Panel1.Controls.Add(newControl)

How to change only one tooltip?

I am populating a FlowLayout with Pictureboxes. As I populate i give each of them a tooltip. I have a seperate function to change the pictures how can I change the tooltip as well?
dim laytt as tooltip = new tooltip
For i = 1 To count
Dim newPic As PictureBox = New PictureBox()
newPic.Image = p.Image
newPic.Size = p.Size
newPic.SizeMode = p.SizeMode
laytt.SetToolTip(newPic, ttstring)
AddHandler newPic.Click, AddressOf LayoutComponent_Clicked
sys.Add(a_component)
LayoutFlowLayout.Controls.Add(newPic)
Next
later i have a function to change the pics in it I want to be able to change the tool tip
Private Sub LayoutComponent_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = LayoutFlowLayout.Controls.IndexOf(sender)
If deleteModeOn Then
sys.components.RemoveAt(i)
LayoutFlowLayout.Controls.RemoveAt(i)
Exit Sub
End If
'get index in sys from layout?
If (sys.components.Item(i).GetType() = GetType(Transpositor)) Then
Form2.ShowDialog(Me)
sys.components.Item(i).divert = tempTranspositorDivert
'here I want to do something like this
laytt.RemoveAt(i) <--- THIS DOESN'T EXIST
End If
End Sub
TL;DR I want to remove/change only one tooltip text at a specific index
Since the sender parameter is the picture box control that was clicked, you can use that variable to specify which control you want to alter. For instance, this will remove the tool tip:
laytt.SetToolTip(sender, Nothing)
This will change it:
laytt.SetToolTip(sender, "new value")