Exception when adding a new row from the RowLeave event - vb.net

I have an unbound datagridview, set the option to allow user to add row to True, and all I want is that once I "complete" a row, the datagridview automatically should create a new empty row, so I set the method
DataGridView1.Rows.Add()
in the DataGridView1_Leave event, but once the row is finished and exception comes out:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll - Unable to complete this operation in this event handler
What does it mean? It should be plain simple code, but I don't know how to solve
Here is the situation:
That's the recipient DataGridView. When I'm on the first blank row (which is created by the AllowUserToAddRow = true) and I press F3 a modal form is opened with another DataGridView containing products to select
and here's the code for the KeyDown event in the recipient DataGridView
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.F3 Then
If frmZoomArticoli.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim codart As String = frmZoomArticoli.DataGridView1.CurrentCell.Value.ToString()
RicercaxCodiceArticolo(codart)
End If
End If
End Sub
Here the sub RicercaxCodiceArticolo(codart) which fills the recipient DataGridView with the selected product:
Private Sub RicercaxCodiceArticolo(ByVal codiceart As String)
Dim strsql As String
Dim cmd As SqlCommand
Dim source As New BindingSource
'Dichiariamo le variabili che ospitano i dati di riga
Dim codice As String
Dim descrizione As String
Dim unitamisura As String
Dim quantita As Double = 1.0
Dim codiceiva As Double
Dim costobase As Double
Dim prezzobase As Double
Dim costoultimo As Double
Dim giacenza As Double
Dim sconto1 As Double
Dim sconto2 As Double
connection.Open()
strsql = "SELECT CODICEARTICOLO AS 'Codice', DESCRIZIONEARTICOLO AS 'Descrizione', UNITAMISURA AS 'Um', CODICEIVA AS 'Iva' " _
& ", COSTOBASE AS 'Costo', PREZZOBASE AS 'Prezzo', SCONTO1 As 'Sc1', SCONTO2 As 'Sc2', COSTOULTIMO AS 'CostoUlt' " _
& ", BARCODE AS 'Barcode', NOTEARTICOLO AS 'Note' ,CATEGORIAARTICOLO AS 'Categ.Art.', GIACENZA AS 'Giacenza' " _
& ", FORNITOREPREF AS 'Fornit. Pref.' FROM Articoli " _
& " WHERE CODICEARTICOLO = '" & codiceart & "'"
cmd = New SqlCommand()
cmd.CommandText = strsql
cmd.CommandType = CommandType.Text
cmd.Connection = connection
source.DataSource = cmd.ExecuteReader()
'Assegniamo i dati letti nel bindingsource alle variabili
codice = source.Current!Codice
descrizione = source.Current!Descrizione
unitamisura = source.Current!Um
codiceiva = Convert.ToDouble(source.Current!Iva)
costobase = Convert.ToDouble(source.Current!Costo)
prezzobase = Convert.ToDouble(source.Current!Prezzo)
costoultimo = Convert.ToDouble(source.Current!Costoult)
giacenza = Convert.ToDouble(source.Current!Giacenza)
sconto1 = Convert.ToDouble(source.Current!Sc1)
sconto2 = Convert.ToDouble(source.Current!Sc2)
'Riempiamo le celle con i dati estratti dal BindingSource
With DataGridView1.CurrentRow
.Cells("grdCodice").Value = codice
.Cells("grdDescrizione").Value = descrizione
.Cells("grdUM").Value = source.Current!Um
.Cells("grdQuantita").Value = quantita
If TipoMovimento = "Carico" Then
.Cells("grdPrezzoUnitario").Value = source.Current!CostoUlt
Else
.Cells("grdPrezzoUnitario").Value = source.Current!Prezzo
End If
.Cells("grdSconto1").Value = sconto1
.Cells("grdSconto2").Value = sconto2
.Cells("grdSconto3").Value = 0.0
.Cells("grdSconto4").Value = 0.0
'Per calcolare il prezzo totale di riga dobbiamo tenere presente le
'informazioni che abbiamo gia' in anagrafica, ovvero il costo/prezzo
'e gli sconti
'Blocco di controllo sul tipo movimento
If TipoMovimento = "Carico" Then
If sconto1 > 0 Then
.Cells("grdPrezzoTotale").Value = quantita * (costoultimo * (costoultimo * sconto1 / 100))
Else
.Cells("grdPrezzoTotale").Value = quantita * costoultimo
End If
ElseIf TipoMovimento = "Scarico" Then
If sconto1 > 0 Then
.Cells("grdPrezzoTotale").Value = quantita * (prezzobase * (prezzobase * sconto1 / 100))
Else
.Cells("grdPrezzoTotale").Value = quantita * prezzobase
End If
End If
.Cells("grdAliquotaIva").Value = codiceiva
End With
connection.Close()
End Sub
and that's the result in the recipient DataGridView:
now, suppose that I don't want to edit the selected row because I don't need to edit quantity, or price, or whatever, but I just want to insert a new row to keep inserting other products. When I tab until the end of the row I want to create a new row, and that's why I wrote the DataGridView1.Rows.Add() in the RowLeave event of the DataGridView, but that's where the exception occurs:
I'm sorry for all the descriptions are in italian, but if you need any other details feel free to ask.
If there's nothing I can do all that remains is to add a button outside the DataGridView which adds a new row from there. Any other solution is welcomed.

Is there some reason you do not use a DataSource for the grid? Then you could simply add the row to the DataSource.
If you insist on manually adding the rows, then the problem you currently have is that the code is never “adding” a new row to the grid. The code simply uses the grids CurrentRow to set the values. This may work but the grids “new” row will not get created.
It is questionable to use the grids CurrentRow in this manner... So instead of using the grids CurrentRow for this, I suggest you “add” a new row in the RicercaxCodiceArticolo method. Something like below should add the new row and the “new-new” empty row should be at the bottom…
Dim newRowIndex As Int32
newRowIndex = DataGridView1.Rows.Add()
With DataGridView1.Rows(newRowIndex)
.Cells("grdCodice").Value = codice
.Cells("grdDescrizione").Value = descrizione
'.....
End With

Related

DataGridView moves rows to bottom when cell is updated with Unbound data

I have a DataGridView that is styled with code and is using data from a SQLite Database
The Database is NOT bound to the DataGridView. A number of events are triggered when I click on a row.
First the Database is updated with today's date.
And the cell that contain's that date reflects the change.
I then call a sort on the column based on the cells value. With this code
dgvLinks.Sort(dgvLinks.Columns(3), ListSortDirection.Ascending)
The process works as expected with no issues IF I omit these lines of code from the Sub Routine ViewSearches() that populates the DataGridView.
If rowCount <= 25 Then
maxRowCount = 25 - rowCount
For iA = 1 To maxRowCount
dgvLinks.Rows.Add(" ")
Next
End If
I can use these empty rows if I make a call to repopulate the DataGridView with ViewSearches()
I am trying to avoid this design as it seems like a over use of resource.
The ERROR that is happening is the 4 rows that contain data are moved to the bottom of the DataGridView and above these 4 rows with data are the empty rows. I will post the relevant code below.
My question How can I keep the empty rows and populate DataGridView so the rows with data are at the top of the DataGridView?
Here is a Screen Shot after I selected LID 2. It is updated and bubbled to the bottom of the DGV.
Private Sub ViewSearches()
Dim intID As Integer
Dim strChannelName As String
Dim strLinkAddress As String
Dim strLastVisit As String
Dim strLinkType As String
Dim rowCount As Integer
Dim maxRowCount As Integer
'Dim emptyStr As String = " "
Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
conn.Open()
Using cmd As New SQLiteCommand("", conn)
'cmd.CommandText = "SELECT * FROM LinkTable"
' Line of CODE Above works with If statement in While rdr
'==========================================================
'cmd.CommandText = "SELECT * FROM LinkTable WHERE ytSiteType = 'News'"
cmd.CommandText = "SELECT * FROM LinkTable WHERE ytSiteType = #site"
cmd.Parameters.Add("#site", DbType.String).Value = gvSLT
Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
'dgvLinks.DataSource = rdr
'Statement Above use when DB is bound to dgvLinks
'=================================================
While rdr.Read()
intID = CInt((rdr("LID")))
strChannelName = rdr("ytChannelName").ToString
strLinkAddress = rdr("ytLinkAddress").ToString
strLastVisit = rdr("ytLastVisit").ToString
strLinkType = rdr("ytSiteType").ToString
'If strLinkType = gvSLT Then
dgvLinks.Rows.Add(intID, strChannelName, strLinkAddress, strLastVisit)
rowCount = rowCount + 1
'End If
End While
dgvLinks.Sort(dgvLinks.Columns(3), ListSortDirection.Ascending)
End Using
If rowCount <= 25 Then
maxRowCount = 25 - rowCount
For iA = 1 To maxRowCount
dgvLinks.Rows.Add(" ")
Next
End If
End Using
End Using
'FindEmpty()
End Sub
Click Event with Update to Database
Private Sub dgvLinks_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLinks.CellClick
selRow = e.RowIndex
If e.RowIndex = -1 Then
gvalertType = "4"
frmAlert.ShowDialog()
Exit Sub
End If
'Dim col As DataGridViewColumn = Me.dgvLinks.Columns(e.ColumnIndex)
Dim row As DataGridViewRow = Me.dgvLinks.Rows(e.RowIndex)
If row.Cells(2).Value Is Nothing Then
gvalertType = "5"
frmAlert.ShowDialog()
Return
Exit Sub
ElseIf gvTxType = "View" Then
webPAGE = row.Cells(2).Value.ToString()
siteID = CInt(row.Cells(0).Value.ToString())
UpdateSiteData()
''MsgBox("Stop " & selRow)
'dgvLinks.ClearSelection()
'dgvLinks.Refresh()
'dgvLinks.RefreshEdit()
Process.Start(webPAGE)
'dgvLinks.Columns.Clear()
''dgvLinks.Rows.Clear()
''ViewSearches()
ElseIf gvTxType = "Delete" Or gvTxType = "Update" Then
gvID = CInt(row.Cells(0).Value)
gvSiteName = row.Cells(1).Value.ToString
gvSiteURL = row.Cells(2).Value.ToString
frmADE.Show()
Close()
End If
End Sub
Update Routine
Public Sub UpdateSiteData()
Dim dateToday = Date.Today
dateToday = CDate(CDate(Date.Today).ToString("M-d-yyyy"))
Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;"),
cmd As New SQLiteCommand("UPDATE LinkTable SET ytLastVisit = #ytLastVisit WHERE LID =" & siteID, conn)
conn.Open()
cmd.Parameters.Add("#ytLastVisit", DbType.String).Value = dateToday.ToString("M-d-yyyy")
cmd.ExecuteNonQuery()
dgvLinks.Rows(selRow).Cells(3).Value = dateToday.ToString("M-d-yyyy")
'Line of code above INSERTS value in Last Visit Column at the correct ROW
'NOT needed if you reload data from the database
'=========================================================================
'dgvLinks.Refresh()
'dgvLinks.RefreshEdit()
dgvLinks.Sort(dgvLinks.Columns(3), ListSortDirection.Ascending)
End Using
End Sub
You will see a number of things I have tried commented out.
As I said I can FIX the issue if I make a call to the ViewSearches() Sub Routine.
Private Sub StyleDGV()
'Sets Design of the DataGridView
'===============================
dgvLinks.DefaultCellStyle.Font = New Font("Times New Roman", 13.0F, FontStyle.Bold)
dgvLinks.ColumnCount = 4
dgvLinks.Columns(0).Width = 60 'ID
dgvLinks.Columns(1).Width = 325 'Site Name 325
dgvLinks.Columns(2).Width = 860 'Site Url 860
dgvLinks.Columns(3).Width = 154 'LastVisit 140
'Option with no blank rows increase col count to 5
'OR increase width of col(3) WHY? because the scroll bar is not showing
' TOTAL Width 1450 Height 488
'=============================
'To Set Col Header Size Mode = Enabled
'To Set Col Header Default Cell Styles DO in Properties
'dgvLinks.Columns(6).DefaultCellStyle.Format = "c"
dgvLinks.ColumnHeadersHeight = 10 'Sans Serif 'Tahoma
dgvLinks.ColumnHeadersDefaultCellStyle.Font = New Font("Sans Serif", 12.0F, FontStyle.Bold)
dgvLinks.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue
dgvLinks.DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
'DGV Header Names
dgvLinks.Columns(0).Name = "LID"
dgvLinks.Columns(1).Name = "Site Name"
dgvLinks.Columns(2).Name = "Site URL"
dgvLinks.Columns(3).Name = "Last Visit"
dgvLinks.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
dgvLinks.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
dgvLinks.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
dgvLinks.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
End Sub
Any one following this question the FIX that permitted keeping the empty rows was to just omit the sort command in the Update to Database Sub Routine
As far as getting the data from the SQLite DB into the grid… you almost have it in the ViewSearches method. The command text looks good; however you are using an SQLiteDataReader. This is resorting to reading the data line by line.
I suggest you use the SQLiteDataAdapter instead. With it you can get the DataTable from the DB in one shot. First create and initialize a DataSet, then create a new SQLiteDataAdapter then add your command to the data adapter something like…
DataSet ds = new DataSet();
using (SQLiteDataAdapter sqlDA = new SQLiteDataAdapter()) {
conn.Open();
sqlDA.SelectCommand = cmd.CommanText;
sqlDA.Fill(ds, “tableName”);
if (ds.Tables.Count > 0) {
//return ds.Tables[“tableName”];
dgvLinks.DataSource = ds.Tables[“tableName”];
}
}
This will add a DataTable to the DataSet ds called “tableName” if the query succeeded.
Then simply use that DataTable as a DataSource to the grid… something like…
dgvLinks.DataSource = ds.Tables[“tableName”];

connecting datatable with datagrid asp.net

so i was trying to bind my datagrid to a datatable and i came up with this code
now everything seems to be working but it does not shows nothing on my gridview it doesnt even shows the datagrid at all when i execute anyone know what is the problem because i cant figure it out everything should be working fine but the gridview does not load .
Dim mrDB As New Odbc.OdbcConnection(myDAC.OBDC)
Dim dgrMR As New DataTable
Dim dr As DataRow
dgrMR.Columns.Add("Data")
dgrMR.Columns.Add("Utente")
dgrMR.Columns.Add("Nome")
dgrMR.Columns.Add("Série")
dgrMR.Columns.Add("Tipo")
dgrMR.Columns.Add("Estado")
dgvMarcacoes.Columns.Clear()
Dim sqlStr As String = "select distinct data,idno,idnome, u_tratam as serie, u_tipomr, Max(estado) as estado " &
"from [marca].[mr]"
sqlStr += queryFilter
sqlStr += "group by idno,idnome, u_tratam, data, u_tipomr "
'this msgbox shows a correct string (that works on sql) so the problem is not on this part
'MsgBox(sqlStr, MsgBoxStyle.OkOnly, "controlo")
mrDB.Open()
Dim mrComm As New Odbc.OdbcCommand(sqlStr, mrDB)
Dim mrReader = mrComm.ExecuteReader
Dim b = New StringBuilder()
If Not (mrReader.HasRows) Then
Me.dgvMarcacoes.Visible = False
MsgBox("Não há marcações para o dia indicado", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ATENÇÃO...")
Else
'Me.dgvMarcacoes.Visible = True
While mrReader.Read
dr = dgrMR.NewRow()
dr(0) = Trim(mrReader(0))
dr(1) = Trim(mrReader(1))
dr(2) = Trim(mrReader(2))
dr(3) = Trim(mrReader(3))
dr(4) = Trim(mrReader(4))
dr(5) = Trim(mrReader(5))
dgrMR.Rows.Add(dr)
'dr.Item(5).Value = Trim(estado(mrReader(5)))
End While
End If
dgvMarcacoes.DataSource = dgrMR
dgvMarcacoes.DataBind()
mrDB.Close()
'so i tried this to see if the data was passing correctly to my datatable and it is :\
For i = 0 To dgrMR.Rows.Count - 1
For u = 0 To dgrMR.Columns.Count - 1
MsgBox(dgrMR.Rows(i)(u).ToString)
Next
Next
You have a tag as asp.net but you have MsgBox in your code indicating that is a WinForm application. Check if the grid columns are AutogenerateColumns=True and change it to false

how to update specific cell in data grid view using SQL and VB.net

I am trying to update a specific cell in datagridview using vb.net and sql ( or any other method)
I am using this code to split somme strings and save it into database.
Private Sub SalveazaData()
Dim list As String() = rtbComData.Text.Split(Environment.NewLine.ToCharArray())
SmdDataDataSet1.SearchAdrese.Rows.Clear()
SmdTableTableAdapter.ClearSearchAdrese()
Me.SearchAdreseTableAdapter.Update(Me.SmdDataDataSet1.SearchAdrese)
For Each Row As String In list
If Not (Row = "AT+DSCAN" Or Row = "OK" Or Row = "" Or Row = "AT+RSSI") Then
Dim s As String() = Split(Row, "|")
Dim aRow As smdDataDataSet1.smdTableRow = SmdDataDataSet1.smdTable.NewsmdTableRow()
aRow.Model = s(0)
aRow.AdresaUnica = s(1)
aRow.StatusModul = "ACTIVE"
Try
SmdDataDataSet1.SearchAdrese.Rows.Add(s(1))
SmdDataDataSet1.smdTable.Rows.Add(aRow)
Catch ex As Exception
Dim u As String
u = SmdTableTableAdapter.UpdateInactivActiv()
End Try
End If
Next
'selectzr()
salveaza()
status()
DataGridView1.Refresh()
End Sub
The table has the columns
Model | Adresa Unica | Status | time | InstAddress |
ZR 123456 active
ZR 654321 active
What i want to do is update the row with the address "123456" or "654321" and write in their specific cell , in this case the cell from the column | time | a value .
Can you please help me .?
I have made some digging and i have fund an answer.
please see below the code i've used in my application
Private Sub GroupCommandRtime()
Dim list As String() = rtbComData.Text.Split(Environment.NewLine.ToCharArray())`enter code here`
Dim ListaAdrese As List(Of String) = GetAdreseID()
For Each Row As String In list
For Each _Adresa As String In ListaAdrese
If Not (Row = "at+remote=" Or Row = "AT+RSSI" Or Row = "OK") Then
Try
Dim s As String() = Split(Row, " ")
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database\smdData.mdf;Integrated Security=True;User Instance=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE smdTable SET datetime='" + s(3) + "' WHERE AdresaUnica = '" + s(0) + "'"
cmd.ExecuteNonQuery()
Me.SmdTableTableAdapter.Fill(Me.SmdDataDataSet1.smdTable)
DataGridView1.Refresh()
Catch ex As Exception
End Try
End If
Next
Next
End Sub

Click column header

I want to order the items in listview by the time or payment or name I'm use now the combobox but i want to do it from the listview header when i clicked on header column i want the listview sorted by it , i'm use this code with the combo box please help me.
Private Sub OrderBy()
Try
Dim OB As String
If ComboBox1.Text = "ID" Then
OB = "ID"
ElseIf ComboBox1.Text = "Name" Then
OB = "StudentName"
ElseIf ComboBox1.Text = "Payment" Then
OB = "Payment"
ElseIf ComboBox1.Text = "Time" Then
OB = "LessonTime"
ElseIf ComboBox1.Text = "Date" Then
OB = "LessonDate"
End If
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("select * from Tend order by " & OB & "", con)
da.Fill(dt)
Dim myrow As DataRow
For Each myrow In dt.Rows
ListView1.Items.Add(myrow.Item(0))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(1))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(3))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(4))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(5))
Next
Explaining better, use the ColumnClick event and use e.Column to know the index of column that was clicked
EDIT: This isn't the best way to do it, but it is the easiest way for you now.
Private Sub OrderBy(Col as Integer)
Try
Dim OB As String
Select Case Col
'Depending on the order of columns in your table
Case 0
OB = "ID"
Case 1
OB = "StudentName"
Case 2
OB = "Payment"
Case 3
OB = "LessonTime"
Case 4
OB = "LessonDate"
End Select
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("select * from Tend order by " & OB & "", con)
da.Fill(dt)
ListView1.Items.Clear
For Each myrow As DataRow In dt.Rows
Dim item = ListView1.Items.Add(myrow.Item(0))
item.SubItems.Add(myrow.Item(1))
item.SubItems.Add(myrow.Item(2))
item.SubItems.Add(myrow.Item(3))
item.SubItems.Add(myrow.Item(4))
item.SubItems.Add(myrow.Item(5))
Next
End Try
End Sub
Private Sub ColumnClick(ByVal o As Object, ByVal e As ColumnClickEventArgs) Handles ListView1.ColumnClick
OrderBy(e.Column)
End Sub
I experienced the same problem, I found a Microsoft tutorial in c# and converted to VB.NET a while back.
You can check out my converted open source project on: http://www.sourcecodester.com/visual-basic-net/5415/how-sort-listview-control-column-header-vbnet.html
It shows how to add new items, separated into columns, and you are able to sort the list view, by clicking on the column header. You can also set the default sort method.

vb.net use string as a previously created object instance

Not sure exactly what I need to do to make this work, so my description may be lacking at first. Essentially I am writing a program launcher that recreates itself each time on load. It pulls the data regarding the tabs and buttons from an SQLite database and builds itself dynamically at run time. I get my problem when I pass the tab name through to the function that creates the buttons. I need the name to pull the right set of buttons from the database and I then tried to use the name to place the buttons on the right tab when I create them, but the debugger calls it a null reference because it doesn't point correctly to the tabpage that I'm trying to make it point to (at least that is what I'm guessing). Any ideas on how to make this work right?
Private Sub CreateTabs()
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT title FROM tabs"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
Dim Tabs(25) As String
Dim c As Integer = 1
While SQLreader.Read()
Tabs(c) = SQLreader(0)
c = c + 1
End While
SQLcommand.Dispose()
SQLconnect.Close()
For i = 1 To UBound(Tabs)
If Tabs(i) <> "" Then
Launcher.TabPages.Add(Tabs(i))
CreateButtons(Tabs(i))
End If
Next
End Sub
Private Sub CreateButtons(ByVal tab)
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT id,name,path FROM buttons WHERE tab = '" & tab & "'"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
While SQLreader.Read()
For i = 1 To 9
Dim NewButton(i) As Button
If Not SQLreader(2) Is System.DBNull.Value Then
Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(SQLreader(2))
End If
Dim toolTip1 As ToolTip = New System.Windows.Forms.ToolTip(Me.components)
Me.Controls(tab).tabpages.add(NewButton(i)) '<--this causes my problem
'NewButton(i).Width = 32
'NewButton(i).Height = 32
'NewButton(i).Text = i
'NewButton(i).Image = myIcon.ToBitmap
'If Not SQLreader(1) Is System.DBNull.Value Then
'toolTip1.SetToolTip(NewButton(i), SQLreader(1))
'toolTip1.Active = True
'End If
Next
End While
SQLcommand.Dispose()
SQLconnect.Close()
End Sub
I think you've got two problems here:
You're trying to find the tab control by the tab name. You should instead find the tab control itself (or pass it in) and find the name of the tab inside that control. Otherwise you're trying to add a button to a collection of tab pages, instead of to a specific tab.
You're never actually creating a new button. You're creating an array of buttons for every iteration of the loop, but not a new actual Button object. Admittedly that doesn't jibe with your suspicion that it's due to tab pages, but it's certainly a problem...
I suspect you want this as your loop (where tabControl is the TabControl passed in):
While SQLreader.Read()
For i = 1 To 9
If Not SQLreader(2) Is System.DBNull.Value Then
Dim myIcon As Icon = Icon.ExtractAssociatedIcon(SQLreader(2))
End If
Dim toolTip1 As ToolTip = New ToolTip(Me.components)
Dim NewButton As Button = New Button
NewButton.Width = 32
NewButton.Height = 32
NewButton.Text = i
NewButton.Image = myIcon.ToBitmap
tabControl.TabPages(tab).Controls.Add(NewButton)
If Not SQLreader(1) Is System.DBNull.Value Then
toolTip1.SetToolTip(NewButton, SQLreader(1))
toolTip1.Active = True
End If
Next
End While
Hopefully that's right - my VB.NET isn't great...
I'm surprised your original code compiled though - do you have Option Strict on?
You should also use a Using statement for your command and connection so they get disposed even when an exception occurs.
Thanks for the help, but I found my answer. Apparently I just needed to reference the index of the tab like so:
Private Sub CreateTabs()
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT title FROM tabs"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
Dim Tabs(25) As String
Dim c As Integer = 1
While SQLreader.Read()
Tabs(c) = SQLreader(0)
c = c + 1
End While
SQLcommand.Dispose()
SQLconnect.Close()
For i = 1 To UBound(Tabs)
If Tabs(i) <> "" Then
Launcher.TabPages.Add(Tabs(i))
CreateButtons(Tabs(i), i - 1)
End If
Next
End Sub
Private Sub CreateButtons(ByVal tab, ByVal TabIndex)
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT id,name,path FROM buttons WHERE tab = '" & tab & "'"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
While SQLreader.Read()
For i = 1 To 9
Dim NewButton As New Button
Launcher.TabPages.Item(TabIndex).Controls.add(NewButton)
NewButton.Width = 32
NewButton.Height = 32
NewButton.Location = New Point(10 + (SQLreader(0) * 32) + 10, 10)
NewButton.Text = SQLreader(0)
If Not SQLreader(2) Is System.DBNull.Value Then
Dim toolTip1 As ToolTip = New System.Windows.Forms.ToolTip(Me.components)
Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(SQLreader(2))
NewButton.Image = myIcon.ToBitmap
toolTip1.SetToolTip(NewButton, SQLreader(1))
toolTip1.Active = True
End If
Next
End While
SQLcommand.Dispose()
SQLconnect.Close()
End Sub