I have a form that contains lots of stuff, including textboxes, images, panels, and a database. The problem is that when I started running it, the window shows up, but the contents display a few seconds after the window is displayed.
I tried removing this important code in the form's shown event. It is a bit useful, but the program doesn't work properly:
Private Sub Inventory_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Label1.Parent = PictureBox1
Label2.Parent = PictureBox1
OpenConnection()
Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from inventory order by [Item No] asc", conn)
Dim ds As New DataSet
da.Fill(ds, "inventory")
DataGridView1.DataSource = ds.Tables("inventory")
DataGridView1.ClearSelection()
conn.Close()
If DataGridView1.RowCount <> 0 Then
PictureBox1.Visible = False
Label1.Visible = False
Label2.Visible = False
End If
TextBox2.Focus()
MG.Visible = True
btnClear.PerformClick()
txtItemNo.ReadOnly = False
txtItemNo.Text = DataGridView1.RowCount + 1
txtItemNo.ReadOnly = True
btnDelete.Enabled = False
clearExpiryDate.Visible = False
End Sub
How can I fix this delay?
Related
I have this pos system i want to generate All category button and display All product list in the FlowLayoutPanel
Private Sub FrmPos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Connection()
LoadCategory()
LoadMenu()
Me.KeyPreview = True
End Sub
this code for LoadCategory()
Sub LoadCategory()
cn.Open()
cm = New SqlCommand("Select * From TblCategory", cn)
dr = cm.ExecuteReader
While dr.Read
BtnCategory = New Button
BtnCategory.Width = 100
BtnCategory.Height = 35
BtnCategory.Text = dr.Item("Category").ToString
BtnCategory.FlatStyle = FlatStyle.Flat
BtnCategory.BackColor = Color.FromArgb(55, 176, 213)
BtnCategory.ForeColor = Color.White
BtnCategory.Cursor = Cursors.Hand
BtnCategory.TextAlign = ContentAlignment.MiddleLeft
FlowLayoutPanel1.Controls.Add(BtnCategory)
AddHandler BtnCategory.Click, AddressOf filter_click
End While
dr.Close()
cn.Close()
End Sub
this code for filter_click
Public Sub filter_click(sender As Object, e As EventArgs)
If LblTransNo.Text = String.Empty Then
MsgBox("Click New Order first!", vbCritical)
Return
End If
_filter = sender.text.ToString
LoadMenu()
End Sub
You have 2 different things going on in the LoadCategory method. I moved the database code to a separate method.
Connections, Commands, and DataReaders all need to be disposed. These objects need to be declared locally in Using blocks. They provide a Dispose method where they release unmanaged resources. Using...End Using blocks will call Dispose for you and close the object even if there is an error.
You don't want to hold a connection open while you build your buttons and interact with the user interface. It appears that you are only using a single field called Category. Don't pull down all the fields with Select *. Just return the data you need to use.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadFlowPanel()
LoadMenu()
Me.KeyPreview = True
End Sub
Private OPConStr As String = "Your connection string."
Private Function GetCategoryData() As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection(ConStr),
cmd As New SqlCommand("Select Category From TblCategory;", cn)
cn.Open()
Using dr = cmd.ExecuteReader
dt.Load(dr)
End Using
End Using
Return dt
End Function
Private Sub LoadFlowPanel()
Dim dt = GetCategoryData()
Dim BtnAll As New Button
With BtnAll
.Width = 100
.Height = 35
.Text = "All"
.FlatStyle = FlatStyle.Flat
.BackColor = Color.FromArgb(55, 176, 213)
.ForeColor = Color.White
.Cursor = Cursors.Hand
.TextAlign = ContentAlignment.MiddleLeft
End With
FlowLayoutPanel1.Controls.Add(BtnAll)
AddHandler BtnAll.Click, AddressOf filter_click
For Each row As DataRow In dt.Rows
Dim BtnCategory = New Button
With BtnCategory
.Width = 100
.Height = 35
.Text = row("Category").ToString
.FlatStyle = FlatStyle.Flat
.BackColor = Color.FromArgb(55, 176, 213)
.ForeColor = Color.White
.Cursor = Cursors.Hand
.TextAlign = ContentAlignment.MiddleLeft
End With
FlowLayoutPanel1.Controls.Add(BtnCategory)
AddHandler BtnCategory.Click, AddressOf filter_click
Next
End Sub
I have an access databasetable with addresses and a table with addresstypes.
When i add a new relation there's different groupboxes with additional information that should be displayed according to the addresstype.
For example: a patient has only basic address info.
A doctor needs a reference number too, so if i click on relation type "doctor" the address groupbox should appear but also the groupbox with doctor reference textbox.
An instance should also get the instance groupbox + address groupbox etc etc.
Addresses can also be "multiple types" so if an address is A: a doctor, and B an instance, both boxes should appear.
My current code looks like this:
Public Class NewRelationForm
'groupboxen disablen on load
Me.grpKiesUwRelatietype.Visible = True
Me.grpBasisData.Visible = False
Me.grpDataBI.Visible = False
Me.grpFoto_Nota.Visible = False
Me.grpHuisartsData.Visible = False
Me.grpPartijData.Visible = False
Me.grpVerwijzerdata.Visible = False
End Sub
'KNOPPEN en Functies!
Private Sub NewRelationForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Tbl_TypeTableAdapter1.Fill(Me.PatientenDatabaseDataSetX1.tbl_Type)
Me.Tbl_OnderzoeksTypesTableAdapter.Fill(Me.PatientenDatabaseDataSetX1.tbl_OnderzoeksTypes)
Me.Tbl_RelatiesTableAdapter.Fill(Me.PatientenDatabaseDataSetX.tbl_Relaties)
End Sub
Private Sub btnAddRelation_Click(sender As Object, e As EventArgs) Handles btnAddRelation.Click
FormMakeRelationType.Show()
End Sub
Private Sub lboxRelatieTypes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lboxRelatieTypes.SelectedIndexChanged
End Sub
'Private Sub lboxRelatieTypes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lboxRelatieTypes.SelectedIndexChanged
''If ComboBox1.SelectedIndex = 1 Then
'' grpBasisData.Visible = True
'' grpFoto_Nota.Visible = True
'' grpFoto_Nota.Location = New Point(720, 120)
''End If
'End Sub
End Class
I could really use a pointer in how to do this, i've tried if else, i've tried select case, but i can't find "the right way".
I'm a beginner
edit1: This is a pic from the winform: https://imgur.com/a/80pKnjR
and this is the access db (names in the db are changed for privacy purposes
https://www.dropbox.com/sh/4gudk8lwxtjahiq/AACz69ocFWIlU7hiTlQxbviLa?dl=0
I found it...
based on an article in stackoverflow: How to show hidden text boxes when items are selected in a combo box
If someone knows how to make this piece of code "easier to handle" or "more compact" i'm very open to ideas.
Also: i can only select one , the rest of the groupboxes dissappear , so it needs some finetuning but i'm 90% there!
Private Sub lboxRelatieTypes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lboxRelatieTypes.SelectedIndexChanged
If lboxRelatieTypes.Text.Trim.Contains("Patiƫnt") = True Then
Me.grpBasisData.Visible = True
Me.grpDataBI.Visible = False
Me.grpFoto_Nota.Visible = True
Me.grpHuisartsData.Visible = False
Me.grpPartijData.Visible = False
Me.grpVerwijzerdata.Visible = False
ElseIf lboxRelatieTypes.Text.Trim.Contains("Verwijzer") = True Then
Me.grpBasisData.Visible = True
Me.grpDataBI.Visible = False
Me.grpFoto_Nota.Visible = True
Me.grpHuisartsData.Visible = False
Me.grpPartijData.Visible = False
Me.grpVerwijzerdata.Visible = True
ElseIf lboxRelatieTypes.Text.Trim.Contains("Betalende Instantie") = True Then
Me.grpBasisData.Visible = True
Me.grpDataBI.Visible = True
Me.grpFoto_Nota.Visible = True
Me.grpHuisartsData.Visible = False
Me.grpPartijData.Visible = True
Me.grpVerwijzerdata.Visible = False
ElseIf lboxRelatieTypes.Text.Trim.Contains("Huisarts") = True Then
Me.grpBasisData.Visible = True
Me.grpDataBI.Visible = False
Me.grpFoto_Nota.Visible = True
Me.grpHuisartsData.Visible = True
Me.grpPartijData.Visible = False
Me.grpVerwijzerdata.Visible = False
ElseIf lboxRelatieTypes.Text.Trim.Contains("Huisarts" & "Betalende Instantie") = True Then
Me.grpBasisData.Visible = True
Me.grpDataBI.Visible = True
Me.grpFoto_Nota.Visible = True
Me.grpHuisartsData.Visible = True
Me.grpPartijData.Visible = False
Me.grpVerwijzerdata.Visible = False
End If
'Dim curitem As String = lboxRelatieTypes.SelectedItems.ToString()
'Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\GoogleDrive\EINDWERK VBNET\PatientenDatabase.accdb")
'con.Open()
'Dim sql As String = String.Format("select * from tbl_Relaties where Rel_Type='{0}'", curitem)
'Dim command As New OleDbCommand(sql, con)
'Dim reader As OleDbDataReader = command.ExecuteReader()
'If curitem.Contains "Huisarts" Then
' End If
'con.Close()
I make a code example, which show the textbox in the groupbox based on the listbox.
Form_Load event:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\PatientenDatabase.mdb")
con.Open()
Dim sql As String = "select Type_Naam from tbl_type"
Dim command As New OleDbCommand(sql, con)
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
ListBox1.Items.Add(reader.GetString(0))
End While
con.Close()
End Sub
Listboxindexchanged event:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim curItem As String = ListBox1.SelectedItem.ToString()
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\PatientenDatabase.mdb")
con.Open()
Dim sql As String = String.Format("select * from tbl_Relaties where Rel_Type='{0}'", curItem)
Dim command As New OleDbCommand(sql, con)
Dim reader As OleDbDataReader = command.ExecuteReader()
If reader.HasRows Then
While reader.Read()
txtID.Text = reader.GetValue(0).ToString()
txtType.Text = reader.GetValue(1).ToString()
txtName.Text = reader.GetValue(2).ToString()
txtVoo.Text = reader.GetValue(3).ToString()
txtAdress.Text = reader.GetValue(4).ToString()
End While
Else
txtID.Text = String.Empty
txtType.Text = String.Empty
txtName.Text = String.Empty
txtVoo.Text = String.Empty
txtAdress.Text = String.Empty
End If
con.Close()
End Sub
Final result:
Besides, I only make a few column code and you can follow the above code to complete the rest of the code.
I have been searching for days, for any possible reference or suggestions and everything I've come across hasn't worked.
The goal:
User will select options in ComboBox1 that will then determine the available options in ComboBox2, then will populate a list of operations in ListBox1.
When the user selects available operations in ListBox1, I need the output to be the sum of values (total time in minutes in this case) into a label for display.
The data used in stored in a local db. So far everything works with my comboboxes and the listbox.
Im attempting to get the Text value, of all selected items, in ListBox1 to output the numeric value in my table (column 4 "OperationsTime"), into a label that will display the sum of all the selections (Total Time In Minutes).
Some Things I have Tried From Other Posts:
Label9.Text = ListBox1.ValueMember
Label9.Text = ListBox1.ValueMember.ToString
Label9.Text = CType(ListBox1.SelectedItem, DataRowView).Row.Item("OperationsTime").ToString
Attempted using Double:
Dim Total As Double = 0
For Each Time As Integer In ListBox1.SelectedItems
Total += CDbl(Time.ToString.Substring(Time.ToString.LastIndexOf(",") + 1))
Next
Label9.Text = Total.ToString
Screen Shot of the Table:
Operations Data Table
Below is my code:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class MainHome
Private Function GetData(ByVal sql As String) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Me.GetData("SELECT SizeId, SizeName FROM Size")
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ComboBox3.Enabled = False
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox3.DataSource = Nothing
ComboBox2.Enabled = False
ComboBox3.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT DetailLevelId, DetailLevelName FROM DetailLevel WHERE SizeId = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = Me.GetData(sql)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT OperationsId, OperationsName, OperationsTime FROM Operations WHERE DetailLevelId = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = Me.GetData(sql)
ListBox1.ValueMember = "OperationsName"
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
Label9.Text = CType(ListBox1.SelectedValue, Integer).ToString
'Label.Text = CType(cbbank.SelectedItem, DataRowView).Row.Item("Account").ToString
End IF
End Sub
Dim totalOperationsTime As Double
For Each view As DataRowView In ListBox1.SelectedItems
totalOperationsTime += CDbl(view("OperationsTime"))
Next
There's no need to get the DataRow from the DataRowView because you can access the field values directly from the DataRowView. It can and does do many of the same things that the DataRow does.
That's the most conventional way but there are other options too. You could throw some LINQ at it:
Dim totalOperationsTime = ListBox1.SelectedItems.Cast(Of DataRowView)().
Sum(Function(view) CDbl(view("OperationsTime")))
It is somewhat annoying that the ValueMember property only helps you get a value for the SelectedItem. Here's a class I wrote some time ago that adds a GetItemValue method that makes use of the ValueMember much as the GetItemText method does for the DisplayMember:
Public Class ListBoxEx
Inherits ListBox
Public Function GetItemValue(item As Object) As Object
Dim index = Me.Items.IndexOf(item)
If (index <> -1 AndAlso Me.DataManager IsNot Nothing) Then
Return Me.FilterItemOnProperty(Me.DataManager.List(index), Me.ValueMember)
End If
Return Nothing
End Function
End Class
If you use that control instead of a regular ListBox then you can do this:
Dim totalOperationsTime As Double
For Each item In ListBoxEx1.SelectedItems
totalOperationsTime += CDbl(ListBoxEx1.GetItemValue(item))
Next
or this:
Dim totalOperationsTime = ListBox1.SelectedItems.Cast(Of Object)().
Sum(Function(item) CDbl(ListBoxEx1.GetItemValue(item)))
One advantage of using that custom control is that you don't have to know what type the data source or its items are. You only have to know that the ValueMember has been set.
I made a few changes to your code. It works with a ListBox. See comments for details.
' "Please Select" doesn't work well in the ListBox, I added it as an option
Private Shared Function GetData(ByVal sql As String, insertPleaseSelect As Boolean) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If insertPleaseSelect Then
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
End If
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = GetData("SELECT [SizeId], [SizeName] FROM [Size]", True)
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ListBox1.SelectionMode = SelectionMode.MultiSimple ' allow multi-select
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox2.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [DetailLevelId], [DetailLevelName] FROM [DetailLevel] WHERE [SizeId] = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = GetData(sql, True)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [OperationsId], [OperationsName], [OperationsTime] FROM [Operations] WHERE [DetailLevelId] = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = GetData(sql, False)
ListBox1.DisplayMember = "OperationsName" ' changed this from ValueMember to DisplayMember
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
ListBox1.ClearSelected() ' Every time the ListBox is populated, clear it
End If
End Sub
' Added this handler to respond to user input, not programmatic selection changes
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
' Here is the sum
Label9.Text = ListBox1.SelectedItems.OfType(Of DataRowView).Sum(Function(o) CType(o("OperationsTime"), Double))
End Sub
I'm doing a todo list vb.net, I have 2 button name Complete and Execute, when the user press Complete, the interface will let user to choose which task has been executed, and the task is from table name todolist and if the user press Execute on this task then it will do a copy to table name complete. But every time I run the program it appear NullreferenceException, but I already create the table.
I wonder is there my logic thinking or the system? Can anyone tell me where is my mistake?
Here is my Execute button
Private Sub btnexecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexecute.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(daa)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
Dim dsNewRow As DataRow
con.Open()
dsNewRow = dss.Tables("complete").NewRow() `exception occur`
dsNewRow.Item("Task") = txtitem.Text
dsNewRow.Item("description") = txtdescript.Text
dsNewRow.Item("Date of create") = dtptoday.Text
dsNewRow.Item("Date of execute") = dtpcomplete.Text
dss.Tables("complete").Rows.Add(dsNewRow)
MaxRows = MaxRows + 1
da.Update(dss, "complete")
con.Close()
MsgBox("Task has been executed, will be remove here and put in!")
`enable and disable button
btncomplete.Enabled = True
btnSave.Enabled = False
btnadd.Enabled = True
btnUpdate.Enabled = True
btnremove.Enabled = True
inc = 0
`navigate records
NavigateRecords()
End If
End Sub
Here is my complete button
Private Sub btncomplete_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncomplete.Click
btnSave.Enabled = False
btnadd.Enabled = False
btnUpdate.Enabled = False
btnremove.Enabled = False
btnexecute.Enabled = True
`call back the record from todolist table
NavigateRecords()
End Sub
Sorry for my poor English
When the user clicks the Start button, I am trying to display all of the records from the Phone column in the Textbox1 control. I want to see all those records pass into Textbox1 While it is processing the For loop. But it is currently processing very fast so that I only see the last record in Textbox1. What i'm going wrong?
While it is processing the For loop, I change the Start button to a Stop button. So when I click the button and it currently has a Text value equal to "Stop", I want it to skip the For loop and pass the value from TextBox1 to my FirstWin. And then it should change the BtnStart.Text back to "Start"
Here's my code:
Public Class PhoneFortune
Dim CN As OleDbConnection
Dim CM As OleDbCommand
Dim DA As OleDbDataAdapter
Dim DT As New DataTable
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
If BtnStart.Text = "Start" Then
CM = New OleDbCommand("SELECT * FROM TblPhoneNumber", CN)
DA = New OleDbDataAdapter(CM)
DA.Fill(DT)
For i = 0 To DT.Rows.Count - 1
TextBox1.Text = DT.Rows(i)("Phone")
Next
BtnStart.Text = "Stop"
End If
If BtnStart.Text = "Stop" Then
If FirstWin.Text = "" Then
FirstWin.Text = TextBox1.Text
BtnStart.Text = "Start"
ElseIf SecondWin.Text = "" Then
SecondWin.Text = TextBox1.Text
BtnStart.Text = "Start"
ElseIf ThirdWin.Text = "" Then
ThirdWin.Text = TextBox1.Text
BtnStart.Text = "Start"
End If
End If
End Sub
Private Sub PhoneFortune_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\db.accdb;Jet OLEDB:Database Password=12345;")
CN.Open()
End Sub
End Class
I hope we are on the same page but if u want to slow down the process for a bit u use these two lines of code within the FOR loop
System.Threading.Thread.Sleep(1000)
Me.Refresh() Where 1000 is one second(value is represented in milliseconds).
Now what is that FirstWin.Text="" etc used for? Are you trying to start the looping,then change the text to "stop" so that when the user clicks stops it stops at the current record? Go to youtube and look for multithreading videos,not sure how to help u on that one hope the rest helps. If this is useful please acknowledge
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Start" Then
If TextBox1.Text = "" Then
t1 = New Thread(AddressOf Me.PhoneThread)
t1.Start()
Button1.Text = "Stop"
Else
t1.Resume()
Button1.Text = "Stop"
End If
Else 'Click Stop
t1.Suspend()
Button1.Text = "Start"
If FirstWin.Text = "" Then
FirstWin.Text = TextBox1.Text
ElseIf SecondWin.Text = "" Then
SecondWin.Text = TextBox1.Text
ElseIf ThirdWin.Text = "" Then
ThirdWin.Text = TextBox1.Text
End If
End If
End Sub
Sub PhoneThread()
'Dim ThreadsArray As List(Of Threading.Thread) = New List(Of Threading.Thread)
Dim s As Integer
'MsgBox(DT.Rows.Count)
For s = 0 To DT.Rows.Count
If s = DT.Rows.Count Then
s = 0
Else
TextBox1.Text = DT.Rows(s)("Phone")
' ThreadsArray.Add(t1)
Thread.Sleep(19)
'TextBox1.Text = s.ToString()
Me.Refresh()
End If
Next
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
Me.Close()
End Sub
For something simple like this, System.Threading.Thread.Sleep(100) (to block the thread) followed by Application.DoEvents() (to process any button clicks) seems like it would do the trick.
For something more complicated, this approach will result in re-entrancy and difficult-to-reproduce bugs; in that case, you definitely need asynchronous programming.