How can I destroy the previous instance of object and create new one whenever a function is called? - vb.net

Whenever a user selects a cell in a sheet I want a windows form to appear and any previous windows form that was connected to the previously active cell to be dissapear. How can I do this?
I'm thinking a solution like the following would work, but I don't know if it is good practice and I also don't know how to execute the command that I have commented out
Sub CreateNewFormForActiveCellAndDeleteThePreviousOne()
'Remove all previous instances of class Frm
Dim Frm as New Frm
End Sub

Instead of creating a new form instance I'd suggest updating the data on the form instead. Thus, you will save resources required for creating a new form instance. For example, you may define a public field in the Form's class and update it according to the selected cell in Excel.

Related

Create a dynamic button via VBA at runtime

I created a Userform, that shows a few values from the table (within textboxes) that can be edited and a command button to save those changes back into the table.
Since I use the same template for various values I decided to dynamically create the Userform at runtime. But I can't get the command button to work at all. I cross-checked with various examples (on StackOverflow and other sites, e.g. here and here), but I can't find the problem.
This is my (simplified) code. The procedure is being called by a button click event itself (CommandButton1). As a workaround, I created a static (empty) UserForm UserForm1 which I use as a starting point to create a specific configuration of the form dynamically during runtime. Essentially it means that just the form elements are created dynamically. At least for now, but that's fine since I only need one single instance of the form to be displayed at any given time. Eventually, I would like to create the form at runtime too:
Private Sub CommandButton1_Click()
'Set Form
UserForm1.Caption = "Test"
'Create Form-Elements (Commandbutton)
Dim cmdButton01 As MSForms.CommandButton
Set cmdButton01 = UserForm1.Controls.Add("Forms.CommandButton.1", "dynCmdButton01")
cmdButton01.Width = 50
cmdButton01.Caption = "Save"
'Show Form
UserForm1.Show
End Sub
Private Sub dynCmdButton01_click()
MsgBox "Test"
End Sub
You should create a class module and assign the event through it:
dynamically create buttons and assign code for the click event (during runtime)
dynamically assign code to static buttons

Add item to listbox from another form

I'm trying to add items to listbox from another form but the listbox doesn't seem to update.
I have 3 form : frm1, frm2, frm3
this my code in frm1 to open frm2:
Using frm As New frm2
frm.ShowDialog()
End Using
frm2 has a listbox named list_xxx
code in frm3:
Private Sub add_item()
frm2.list_xxx.add("aaaa")
End Sub
i want to add item to frm2 from frm3. but no success the listbox still empty.
how to fix it?
You are almost certainly referring to the default instance of frm2 in that second code snippet but you aren't displaying the default instance in the first code snippet. If you display one form and then add items to a ListBox on another form, there should be no surprise that you can't see those items. Either use the default instance everywhere or not at all. The more correct option is not at all, but that would require other changes too. The easier option is everywhere, which means changing the first code snippet to this:
frm2.ShowDialog()
No Using statement because you're not creating an object.

Send Parameter via new() or pre-set properties before calling the new form?

I would really appreciate your advice on the following:
I'm working on Windows forms using VB.NET (even though the language is irrelevant to the question at hand).
I've got a main form and wish to call out another one, however depending on a given variable I need the text on some of the new form's elements to change as well as disable some of its controls.
There are two ways I see of doing it:
Send a parameter from the main form and have some logic on the second form to deal with everything on load.
Main Form:
dim newform as new frmcalculate(byval type as string)
New Form:
public sub getexplanation(byval type as string)
select type
case "Sum"
lblexplanation.text = "this is a sum"
case "Subtraction"
lblexplanation.text = "this is a subtraction"
End sub
Set exactly what I want on the main form before calling the new form.
i.e:
dim newform as new frmcalculate()
newform.lblexplanation.text = "This is a sum"
I hope I've managed to explain it correctly.
I'm still new at this especially getting the formats right on Stackoverflow.
In the first approach the code is best managed and organized for further editing. So each form has it own code.
It is not best practice to use second approach. (Editing a form designer from another one)

Updating a control on another form with the results of a dialog box

I made a windows form which contains a listbox (named VenueList). The listbox is pulling it's values from a list provided by a binding source VenuesBindingSource which is pulling in a ID for the value and a name for the text.
There is a button which is causing a DialogBox to appear which is asking for values to store in the database for a NEW venue. Once the values are filled and insert the database, what's supposed to happen is that the dialog box closes and goes back to the original form which invoked it.
However, instead of updating the list. The list stays the same. But if you close the form and reopen it, you see that a new value was added.
TournamentSettings.VenuesTableAdapter.InsertVenueQuery(Trim(VenueNameTxt.Text), Trim(VenueAddress1Txt.Text), Trim(VenueAddress2Txt.Text), Trim(VenueCityTxt.Text), Trim(VenueProvinceTxt.Text), Trim(VenueZipTxt.Text), Trim(CountryBox.SelectedValue), Trim(VenuePhoneNo.Text), VenueType.SelectedText, VenueWebAddress)
TournamentSettings.VenuesTableAdapter.Fill(TournamentSettings.VenueNameList.Venues)
In the above code, InsertVenueQuery is the name of a query from the designer which is invoked to add the values onto the tableadapter VenuesTableAdapter which is used to fill the combo box on load. I also sent the Fill command to refill the table with the new value.
So the question is, should I go about doing this another way, rather than feeding the Table adapter and sending a fill command on to the datatable? Or is there something that I'm not doing here which I should to force that value into the list. I'm tempted to redo everything outside of the designer but that's a lot of code since I have to essentially run two commands (one to insert the data, and another to get the ##IDENTITY value since this is run on an access database.)
Okay. This one I had to think about for a moment.
Instead of me creating a block of done on the load event, I instead created a sub function called "FillVenueList".
I used the following block of code:
Public Sub FillVenueList()
' Adding values from database to a datatable.
' From there will add to the list box.
Dim VenueConnection As New OleDb.OleDbConnection(DBconnection)
VenueConnection.Open()
Dim VenueConnectionQuery As New OleDb.OleDbCommand("SELECT VenueID, VenueName FROM Venues", VenueConnection)
Dim VenueDataAdapter As New OleDb.OleDbDataAdapter(VenueConnectionQuery)
Dim VenueDataSet As New DataSet
VenueDataAdapter.Fill(VenueDataSet, "Venues")
TrnVenueLst.DataSource = VenueDataSet.Tables("Venues")
TrnVenueLst.DisplayMember = "VenueName"
TrnVenueLst.ValueMember = "VenueID"
VenueConnection.Close()
End Sub
From there I called THIS sub on both the form AND the Add Venue window and I can safely see that this works. SO THAT is how you get a new value onto the form, don't use it as a part of the Load Event but rather call it from the load event block, then call it when you want to add to the list.

Referencing cell values on DataGridView from another form

This is bugging me. I have a "main" form named frmMain with a DataGridView object named objDataGrid.
When a user double clicks on a row or clicks another button, I hide the main form, open a new form and want to reference the values in the row selected by the user but I keep getting an error when I try to access some, but not all, of the datagridview's properties.
For example here is the code that opens the form:
On Error Resume Next
Me.Hide()
frmGenerate.Show()
In the frmGenerate's load event I do the following:
Dim frmMain As frmMain = Nothing
frmMain = New frmMain
Any time I try to grab a value from the grid I get various errors.
frmMain.objDataGrid.Item(1, 2).Value
frmMain.objDataGrid.Rows(2).Cells(4).Value
frmMain.objDataGrid.SelectedRows(0).Index
frmMain.objDataGrid.Item(1, frmMain.objDataGrid.SelectedRows(0).Index).Value
These return index out of range errors even though I know for sure the indexes are correct.
Any ideas?
Thanks.
Why are you creating a new instance of frmMain? You need to refer to the existing one where they've selected a row.
You could pass just the row of the datagrid or PK field values instead of referencing the other form.