datagridview not updating to bound datasource - vb.net

I am trying to get the datagridview to update when I update the datasource and I'm having no luck whatsoever.
Here is my binding:
Private _dgbNews As SortableBindingList(Of SalesMessageRecord)
Private Sub SalesMessageScreen_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'_dgbNews.RaiseListChangedEvents = True
_dgbNews = AllNews()
BindingSource1.DataSource = AllNews()
DataGridView1.DataSource = BindingSource1
MassageDemRows()
End Sub
this is AllNews():
Public Function AllNews() As SortableBindingList(Of SalesMessageRecord)
Dim sm = New SortableBindingList(Of SalesMessageRecord)
Dim allnewsitems = News.GetAllNewsItems(Configuration.CompanyID).ToList()
For Each allnewz As News In allnewsitems
Dim smr = New SalesMessageRecord
smr.Body = allnewz.NewsBody
smr.CorporationId = CType(allnewz.CorporationId, Guid)
smr.Expiration = allnewz.Expiration
smr.IsActive = allnewz.IsActive
smr.NewsId = allnewz.NewsId
smr.Title = allnewz.NewsTitle
smr.SortOrder = allnewz.OrderNumber
smr.TokenId = allnewz.TokenId
smr.IsNew = False
sm.Add(smr)
Next
Return sm
End Function
And this is where I'm trying to update it:
Private Sub button_Save_Click(sender As System.Object, e As System.EventArgs) Handles button_Save.Click
If _currentRow < 0 Then
Return
End If
_dgbNews(_currentRow).Expiration = datetimepicker_ExpirationDate.Value
_dgbNews(_currentRow).SortOrder = CInt(numericupdown_SortNumber.Value)
_dgbNews(_currentRow).IsActive = checkbox_Active.Checked
_dgbNews(_currentRow).Body = richtextbox_Body.Text
_dgbNews(_currentRow).Title = textbox_Title.Text
DataGridView1.Refresh()
News.UpdateNewsRecord(_dgbNews(_currentRow).NewsId,
_dgbNews(_currentRow).Expiration,
_dgbNews(_currentRow).SortOrder,
_dgbNews(_currentRow).IsActive,
_dgbNews(_currentRow).Body,
_dgbNews(_currentRow).Title)
End Sub
The database is updating without issue but the datagridview won't update.

Right, I'll take a crack at this. Bindingsources are really useful when used together with a DGV. However, they tend to be aweful in the way that they give no information what so ever why they aren't working.
First of I would say that your binding source has the wrong Datasource.
_dgbNews = AllNews()
BindingSource1.DataSource = AllNews()
DataGridView1.DataSource = BindingSource1
As you can se you have AllNews() as Datasource. But you add stuff to _dgbNews and expect Allnews() to change. Protip, it doesn't. If you were to set the DataSource to this:
BindingSource1.DataSource = _dgbNews
Then atleast you should expect some change when the list is updated. Now this is what you actually do miss. In button save click you add an item to the list, this is now fine if you done the above. But wait, the datasource changed and nothing happened. This is because you don't tell the datagridview to change. Make the Bindingsource tell everything it's connected to to update. With this:
BindingSource1.ResetBindings(True)
This is better than DGV.Refresh (which might work now) since your bindingsource could be attached to other things (I know it isn't, but for future reference).
Try this and well see if it will go better.

Had similiar issue with DataGridView not displaying any data from my datasource, figured out that my binding class properties were regular int or string instead of being declared with get set.
changed from
int RedeemID;
to
public int RedeemID { get; set; }

TRY THIS:
(AFTER EXECUTING THE UPDATE COMMAND FROM THE DATA ADAPTER)
GRIDVIEW_NAME.DATABIND()

Me.TableAdapter1.Fill(Me.DataSet1.Table1)

Related

How would you structure data with different datatypes in VB.NET

Right now I have many locations with this structure. At the moment I have: name as string and x,y,z positions as single. So it's a mix of data types and I might want to add both more data in the future and also other data types. I must be able to easily extract any part of this data.
Example of how I'll work with this data is: When I choose South Wales from a combobox then I want to get its properties, x,y,z populated in a textbox. So they need to be "linked". If I choose London then it'll have its x,y,z properties etc.
My initial idea is just to dim every single data such as in the first example below. This should be the easiest way with 100% control of what's what, and I could easily extract any single data but at the same time might get tedious I assume, or am I wrong? Is it a good way to approach this?
Dim SW_FP As String = "South Wales"
Dim SW_FP_X As Single = "489,1154"
Dim SW_FP_Y As Single = "-8836,795"
Dim SW_FP_Z As Single = "109,6124"
The next example below is something i just googled up. Is this a good method?
Dim dt As DataTable = New DataTable
dt.Columns.Add("South Wales", GetType(String))
dt.Columns.Add("489,1154", GetType(Single))
dt.Columns.Add("-8836,795", GetType(Single))
dt.Columns.Add("109,6124", GetType(Single))
OR should I use something else? Arrays, Objects with properties... and this is where my ideas end. Are there other methods? XML?
I want to do it in a smart way from start instead of risking to rewrite/recreate everything in the future. So my main question is: Which method would you suggest to be the smartest to choose? and also if you could provide a super tiny code example.
You mentioned that when you choose an item you want to get it's properties. This shows that you are looking for objects. If not using a database one example could be to make Location objects and have a List of these to be added or removed from. Then you have a lot of different ways to get the data back from the List. For example:
Class:
Public Class Location
Public Property Name As String
Public Property X As Single
Public Property Y As Single
Public Property Z As Single
End Class
List:
Dim locations As New List(Of Location)
Dim location As New Location With {
.Name = "South Wales",
.X = 1.1,
.Y = 1.2,
.Z = 1.3
}
locations.Add(location)
LINQ to get result:
Dim result = locations.SingleOrDefault(Function(i) i.Name = "South Wales")
This is just an example for use within your program, hope it helps.
Disclaimer: Untested code. It's more to guide you than copy-paste into your project.
First, create a Class that will represent the structured data:
Public Class Location
Public Property Name As String
Public Property PositionX As Single
Public Property PositionY As Single
Public Property PositionZ As Single
Public Sub New()
Me.New (String.Empty, 0, 0, 0)
End Sub
Public Sub New(name As String, x As Single, y As Single, z As Single)
Me.Name = name
Me.PositionX = x
Me.PositionY = y
Me.PositionZ = z
End Sub
Now, you can create a List(Of Location) and use that List to bind to a ComboBox, like this:
Dim list As New List(Of Location) = someOtherClass.ReadLocations ' Returns a List(Of Location) from your database, or file, or whatever.
cboLocations.DataSource = list
cboLocations.DisplayMember = "Name" ' The name of the Location class' Property to display.
cboLocations.ValueMember = "Name" ' Use the same Name Property since you have no ID.
You can also forego the list variable declaration like the following, but I wanted to show the declaration of list above:
cboLocations.DataSource = someOtherClass.ReadLocations
Function someOtherClass.ReadLocations() may populate the List(Of Locations) in a way similar to this. Note I'm not including data access code; this is just an example to show how to add Location objects to the List(Of Location):
Dim list As List(Of Location)
' Some loop construct
For each foo in Bar
Dim item As New Location(foo.Name, foo.X, foo.Y, foo.Z)
list.Add(item)
' End loop
Return list
The "magic" happens when you select an option from the ComboBox. I forget the ComboBox event offhand, so that's homework for you :-) You take the selected Object of the ComboBox and cast it back to the native type, in this case Location:
Dim item As Location = DirectCast(cboLocations.SelectedItem, Location)
txtName.Text = item.Name
txtPosX.Text = item.PositionX.ToString
txtPosY.Text = item.PositionY.ToString
txtPosZ.Text = item.PositionZ.ToString
Here is one way, using a DataTable as you mentioned. This is a stand alone example project just to show code used.
This example loads data from file is found and saves data on exit.
Form1 Image
' Stand alone example
' needs DataGridView1, Label1 and
' ComboBox1 on the Designer
' copy/replace this code with default
Option Strict On
Option Explicit On
Public Class Form1
Dim dt As New DataTable("Freddy")
Dim bs As New BindingSource
'edit path/filename to use as test data path
Dim filepath As String = "C:\Users\lesha\Desktop\TestData.xml"
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
dt.WriteXml(filepath)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With dt
dt.Columns.Add("Country", GetType(String))
dt.Columns.Add("x", GetType(String))
dt.Columns.Add("y", GetType(String))
dt.Columns.Add("z", GetType(String))
' add extra column to hold concatenated
' location (could be a hidden column)
dt.Columns.Add("CombLoc", GetType(String), "'x = ' + x + ' y = ' + y + ' z = ' + z")
If IO.File.Exists(filepath) Then
' saved file found so load it
dt.ReadXml(filepath)
Else
' no saved file so make one test row
.Rows.Add("South Wales", 489.1154, -8836.795, 109.6124)
End If
End With
bs.DataSource = dt
DataGridView1.DataSource = bs
' set any properties for DataGridView1
With DataGridView1
' to hide Combined Location column
.Columns("CombLoc").Visible = False
' dontwant row headers
.RowHeadersVisible = False
End With
set up ComboBox
With ComboBox1
.DataSource = bs
' displayed item
.DisplayMember = "Country"
' returned item
.ValueMember = "CombLoc"
If .Items.Count > 0 Then .SelectedIndex = 0
End With
' default Label text
Label1.Text = "No items found"
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
no items in list so exit sub
If ComboBox1.SelectedIndex < 0 Then Exit Sub
send returneditem to Label
Label1.Text = ComboBox1.SelectedValue.ToString
End Sub
End Class

Pasting new records to a Bound DataGridView

Apologies if this has already been asked. If so, I am unable to find a simple solution. I am trying to allow a user to copy/paste multiple records in a DataGridView (the in memory copy of the data, to be saved later when the user clicks the save button) and cannot find anything that works. It probably is because there is something I do not understand about all of this.
I set up a standard edit form with Visual Studio's drag/table into a form, so it's using a BindingSource control and all the other controls that come with doing that. It works just fine when manually entering something in the new row one by one, so it seems to be set up correctly, but when it comes to adding a record (or multiples) using code, nothing seems to work.
I tried a few things as outline in the code below. Could someone please at least steer me in the right direction? It cannot be that difficult to paste multiple records.
I run this when the user presses Control-V (the clipboard correctly holds the delimited strings):
Private Sub PasteClipboard()
If Clipboard.ContainsText Then
Dim sLines() As String = Clipboard.GetText.Split(vbCrLf)
For Each sLine As String In sLines
Dim Items() As String = sLine.Split(vbTab)
Dim drv As DataRowView = AdjustmentsBindingSource.AddNew()
drv.Item(1) = Items(0)
drv.Item(2) = Items(1)
drv.Item(3) = Items(2)
drv.Item(4) = Items(3)
'Error on next line : Cannot add external objects to this list.
AdjustmentsBindingSource.Add(drv)
Next
End If
End Sub
EDIT
(the bindingsource is bound to a dataadapter, which is bound to a table in an mdb file, if that helps understand)
I adjusted the inner part of the code to this:
If (RowHasData(Items)) Then
Dim drv As DataRowView = AdjustmentsBindingSource.AddNew()
drv.Item("FontName") = Items(0)
drv.Item("FontSize") = Items(1)
drv.Item("LetterCombo") = Items(2)
drv.Item("Adjustment") = Items(3)
drv.Item("HorV") = Items(4)
End If
It kinda works, but it also adds a blank row before the 2 new rows. Not sure where that is coming from, as I have even included your RowHasData() routine...
I would think that “attemp3” SHOULD work, however, it is unclear “what” the AdjustmentsBindingSource’s DataSource is. Is it a List<T> or DataTable?
If I set the BinngSource.DataSource to a DataTable, then attempt 3 appears to work. Below is an example that worked.
Private Sub PasteClipboard2()
If Clipboard.ContainsText Then
Dim sLines() As String = Clipboard.GetText.Split(vbCrLf)
For Each sLine As String In sLines
Dim Items() As String = sLine.Split(vbTab)
If (RowHasData(Items)) Then
Dim drv As DataRowView = AdjustmentsBindingSource.AddNew()
drv.Item("FontName") = Items(0)
drv.Item("FontSize") = Items(1)
drv.Item("LetterCombo") = Items(2)
drv.Item("Adjustment") = Items(3)
drv.Item("HorV") = Items(4)
End If
Next
End If
End Sub
This appears to work in my tests. I added a small function (RowHasData) to avoid malformed strings causing problems. It simply checks the size (at least 5 items) and also checks to make sure a row actually has “some” data. If a row is just empty strings, then it is ignored.
Private Function RowHasData(items As String())
If (items.Count >= 5) Then
For Each item In items
If (item <> "") Then Return True
Next
End If
Return False
End Function
I am guessing it would be just as easy to add the new rows “directly” to the BindingSource’s DataSource. In the example below, the code is adding the row “directly” to the DataTable that is used as a DataSource to the BindingSource. I am confident you could do the same thing with a List<T> by simply adding a new object to the list. Below is a complete example using a BindingSource and a DataTable. This simply adds the rows to the bottom of the table.
Dim gridTable1 As DataTable
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PasteClipboard()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
gridTable1 = GetTable()
FillTable(gridTable1)
AdjustmentsBindingSource.DataSource = gridTable1
AdjustmentsDataGridView.DataSource = AdjustmentsBindingSource
End Sub
Private Function GetTable() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("FontName", GetType(String))
dt.Columns.Add("FontSize", GetType(String))
dt.Columns.Add("LetterCombo", GetType(String))
dt.Columns.Add("Adjustment", GetType(String))
dt.Columns.Add("HorV", GetType(String))
Return dt
End Function
Private Sub FillTable(dt As DataTable)
For index = 1 To 10
dt.Rows.Add("Name_" + index.ToString(), "Size_" + index.ToString(), "Combo_" + index.ToString(), "Adjust_" + index.ToString(), "HorV_" + index.ToString())
Next
End Sub
Private Sub PasteClipboard()
If Clipboard.ContainsText Then
Dim sLines() As String = Clipboard.GetText.Split(vbCrLf)
Try
Dim dataRow As DataRow
For Each sLine As String In sLines
Dim Items() As String = sLine.Split(vbTab)
If (RowHasData(Items)) Then
dataRow = gridTable1.NewRow()
dataRow("FontName") = Items(0)
dataRow("FontSize") = Items(1)
dataRow("LetterCombo") = Items(2)
dataRow("Adjustment") = Items(3)
dataRow("HorV") = Items(4)
gridTable1.Rows.Add(dataRow)
End If
Next
Catch ex As Exception
MessageBox.Show("Error: " + ex.Message)
End Try
End If
End Sub
Private Function RowHasData(items As String())
If (items.Count >= 5) Then
For Each item In items
If (item <> "") Then Return True
Next
End If
Return False
End Function
Hope the code helps…
Last but important, I am only guessing that you may have not “TESTED” the different ways users can “SELECT” data and “how” other applications “copy” that selected data. My previous tests using the WIN-OS “Clipboard” can sometimes give unexpected results. Example, if the user selects multiple items using the ”Ctrl” key to “ADD” to the selection, extra rows appeared in the Clipboard if the selection was not contiguous. My important point is that using the OS clipboard is quirky IMHO. I recommend LOTS of testing on the “different” ways the user can select the data. If this is not an issue then the code above should work.

VB.Net Custom ListBox output issue

Since i needed to disable (grey out) some items inside a ListBox, i'm using a Custom control that can be found here:
Here is my current code:
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtp.Columns.Add("key")
dtp.Columns.Add("value")
PopulateDataTable(dtp, "myTxt")
_dataView = New DataView(dtp)
'Custom ListBox
List1.ValueMember = "key"
List1.DisplayMember = "value"
List1.DataSource = _dataView
'Legacy ListBox
List2.ValueMember = "key"
List2.DisplayMember = "value"
List2.DataSource = _dataView
UpdateLanguageMenu()
End Sub
Private Function PopulateDataTable(dt As DataTable, resTxt As String)
Using sw As New StringReader(My.Resources.ResourceManager.GetObject(resTxt))
Do
Dim line As String = sw.ReadLine
If line Is Nothing OrElse line.Trim = String.Empty Then Exit Do
Dim strArr() As String
strArr = line.Split(",")
Dim row As DataRow = dt.NewRow()
row("key") = strArr(0)
row("value") = strArr(1)
dt.Rows.Add(row)
Loop
sw.Close()
End Using
End Function
List1 is the Custom ListBox and List2 is the ListBox that comes with VS2012E.
I don't need List2, it's only there to test,
and at runtime, in List2 i get all my values loaded correctly, instead in List1 i get System.Data.DataRowView in all rows..
The strange thing is that, my txt i'm loading is like:
00A1,MyValue1
00A2,Myvalue2
00A3,MyValue3
I have also a Label, and when selecting items on the ListBox i have code to change the Label.Text to List.SelectedValue that is the first part before the comma.
And it get displayed in the label. Only items inside the Custom ListBox are not being displayed.
Populating List1 manually, instead using a DataTable, is working.
And since i'm a beginner i can't locate the problem.
I think your problem has to do with this line: string displayValue = GetItemText(item); in the control. This takes for granted that all items are strings. In your case it is a datarowview hence the result (drv.toString would return something like that). You need to convert "item" into a drv and set display value to be drvItem("value" or "key") instead. So it is basically not your code that is the problem, it is the control.
Actually... After reading the code in the control and not on the code project site, I realised that this line:
displayValue = GetItemText(item);
Doesn't even exist. It is exchanged with
item.ToString()
Which pretty much proves my theory.
Right, how to fix.
In:
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
You have this:
object item = this.Items[e.Index];
What you have to do is to convert item into a DataViewRow and assign value to a variable, something like this:
DataViewRow dvrItem = (DataViewRow)item;
String displayText = dvrItem("key"); or String displayText = dvrItem("value");
Then change all these:
e.Graphics.DrawString(item.ToString(), e.Font, SystemBrushes.GrayText, e.Bounds);
Into:
e.Graphics.DrawString(displayText, e.Font, SystemBrushes.GrayText, e.Bounds);

What is the difference between Single and FirstOrDefault? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
LINQ Single vs First
Im new to Linq and want to learn it the best way, I have here 2 working update events for linq, thay do the same, but what way is the best and do i need to add something to make it better !?
Solution 1
Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
Using db As New ThedatabaseconnectionDataContext()
Try
Dim TheUpdateID = DirectCast(FindControl("Textbox5"), TextBox).Text
Dim getEditing As testtable = (From c In db.testtables Where c.test_id = TheUpdateID Select c).FirstOrDefault()
If getEditing IsNot Nothing Then
getEditing.test_cat = DirectCast(FindControl("Textbox1"), TextBox).Text
getEditing.test_info = DirectCast(FindControl("Textbox2"), TextBox).Text
getEditing.test_number = DirectCast(FindControl("Textbox3"), TextBox).Text
getEditing.test_datetime = DirectCast(FindControl("Textbox4"), TextBox).Text
db.SubmitChanges()
'textBox1.Text = "Contact updated."
End If
Catch ex As Exception
'Me.lblMsg.Text = ex.Message
End Try
End Using
End Sub
Solution 2
Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
Using db As New ThedatabaseconnectionDataContext()
Try
Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
Dim TheUpdateID = DirectCast(FindControl("Textbox5"), TextBox).Text
Dim getEditing As testtable = tbltest.Single(Function(c) c.test_id = TheUpdateID)
If getEditing IsNot Nothing Then
getEditing.test_cat = DirectCast(FindControl("Textbox1"), TextBox).Text
getEditing.test_info = DirectCast(FindControl("Textbox2"), TextBox).Text
getEditing.test_number = DirectCast(FindControl("Textbox3"), TextBox).Text
getEditing.test_datetime = DirectCast(FindControl("Textbox4"), TextBox).Text
db.SubmitChanges()
'textBox1.Text = "Contact updated."
End If
Catch ex As Exception
'Me.lblMsg.Text = ex.Message
End Try
End Using
End Sub
First off, you should be able to write your first or default as
Dim getEditing As testtable = tbltest.FirstOrDefault(Function(c) c.test_id = TheUpdateID)
Untested but more to point out that first or default handles lambdas
As for which to use, it depends on your data. To break down what happens
Single - Expects exactly one match, A exception is thrown if no
results are found OR multiple results are found
SingleOrDefault -
Expects 0 or 1 match. A exception is thrown if multiple matches are
found
First - Expects 1 or many match. Exception is thrown if no
matches are found. Any results after the first result are ignored.
FirstOrDefault - handles 0, 1 or multiple matches. Any results after the first result are ignored.
If you are picking based on a ID from a listbox (I.e. it's unique and is definatly in the database) then single is a safe choice.
If the user is entering a ID (again unique) that may or may not be in the DB single or default is safe.
If searching based on a possible duplicate value, like surname, then first or firstordefault is what you should use depending if it is guaranteed to exist in the database or not.
Personally, regardless of the data, I would stick to either first or firstordefault as it handles more scenarios.
Where to even start on this one...
For a start, I know this is likely a test project, so apologies if you're already doing this, but please make sure you're using acceptable data tier hierarchies - your DBML should be in a separate project from your Presentation layer.
But, to the question in hand. My preferred way of doing this is getting the data object and updating it on an object level. Such as (pseudo code / I'm a C# kinda guy!):
private MyObject object;
protected void Page_Load(object sender, EventArgs e)
{
// Select usually be ID
object = DataLayer.GetObject();
if(!IsPostBack)
{
// Load object details for editing into presentation layer
TextboxObjectName.Text = object.Name;
}
}
protected void Button_Click(object sender, EventArgs e)
{
// Button click event - update object and send it to database
object.Name = TextboxObjectName.Text;
DataLayer.UpdateObject(object);
}
This makes use of object tracking, and the Daya Layer can then look like this:
function void UpdateObject(MyObject obj)
{
using (TestDataContext db = new TestDataContext ())
{
db.MyObjects.Attach(obj);
db.Refresh(RefreshMode.KeepCurrentValues, obj);
db.SubmitChanges();
}
}

Selecting the most recently dynamically created object with VB.NET

Below is some code that I'm using to create objects with Visual Basic:
For indexCounter As Integer = 1 To TotalParticipants Step 1
participantClock = New Label
participantClock.Size = New Size(100, 20)
participantClock.Name = "participantClock" & indexCounter
participantClock.Location = New Point(139, (5 + ((indexCounter - 1) * 26)))
participantClock.BorderStyle = BorderStyle.Fixed3D
participantClock.TextAlign = ContentAlignment.MiddleRight
CenterPanel.Controls.Add(participantClock)
participantStop = New Button
participantStop.Size = New Size(58, 20)
participantStop.Location = New Point(245, (5 + ((indexCounter - 1) * 26)))
participantStop.BackColor = Color.Red
participantStop.ForeColor = Color.White
participantStop.Font = New Font(participantStop.Font, FontStyle.Bold)
participantStop.Text = "Stop"
CenterPanel.Controls.Add(participantStop)
participantTimer = New Timer
participantTimer.Start()
participantTimer.Enabled = True
participantTimer.Interval = 1
participantStopwatch = New Stopwatch
participantStopwatch.Start()
Next
I'm creating a label, a button, Timer, and Stopwatch. (Though I have sinking feeling I don't need BOTH a timer and stopwatch since I'm counting time.)
What I would like to do, is create the label and set that label's text to be the value from the stopwatch. The button that will be created will stop THAT stopwatch.
The problem that I'm having is that I cannot call the stopwatch by name since it wasn't created yet and VB throws a hissy fit at me for it. (After all it wasn't really declared.)
So the question becomes, how do you call the most recently dynamically created control and assign events using that control. If it's not possible to do, I do not mind dumping the form and starting over creating 30 stopwatches instead (but I'd like to avoid that, if possible).
Thanks for any help.
I assume that you want the timer to update the label based on the value of the stopwatch. Is that right?
One thing that you might try that is a little hacky is this:
Define a storage class like so:
Public Class StopwatchStorage
Public Property Stopwatch as Stopwatch
Public Property Label as Label
Public Property Timer as Timer
End Class
at the top of your form define a private list:
Private _storage as new List(Of StopwatchStorage)
at the end of your for loop do this
Dim storage As New StopwatchStorage()
storage.Label = participantClock
storage.Timer = participantTimer
storage.Stopwatch = participantStopwatch
_storage.Add(storage)
AddHandler participantTimer.Tick, AddressOf Timer_Tick
The above code would give you access to the three objects that you need in your tick function. You will have to loop through the _storage list to find the right "set" of objects but it should work:
Private Sub Timer_Tick(sender As Object, args As EventArgs)
For Each storage As StopwatchStorage In _storage
If storage.Timer Is sender Then
storage.Label.Text = storage.Stopwatch.Elapsed
Exit Sub
End If
Next
End Sub
I didn't try to compile that code so I'm sure there are a few typos but I think that should give you an idea of how to refer to the object without needing to use the object's name.