VB.Net Limiting Checked in the datagridview and get data from it to label - vb.net

Please help me.
I have some problem on my vb code. I have a Datagridview with Checkbox column in it. My system is a library system. i want the user to borrow only 2 books. and my problem is.
1. I want to limit the user to checked only to 2.
2. And when the row was check i want to get some data for example the book title and the author and put it to a label.
Please help me.
Public Sub BorrowBook()
Dim Message As String = String.Empty
Dim Message1 As String = String.Empty
Dim Message2 As String = String.Empty
Dim Message3 As String = String.Empty
Dim Message4 As String = String.Empty
Dim Message5 As String = String.Empty
For Each row As DataGridViewRow In Form5.DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
isSelected += 1
If isSelected = 1 Then
Message = row.Cells("BookID").Value.ToString()
Message1 = row.Cells("CallNumber").Value.ToString()
Message2 = row.Cells("Title").Value.ToString()
ElseIf isSelected = 2 Then
Message3 = row.Cells("BookID").Value.ToString()
Message4 = row.Cells("CallNumber").Value.ToString()
Message5 = row.Cells("Title").Value.ToString()
End If
Next
Form5.Label17.Text = Message
Form5.Label25.Text = Message1
Form5.Label21.Text = Message2
Form5.Label20.Text = Message3
Form5.Label26.Text = Message4
Form5.Label19.Text = Message5
End Sub

Based on your Problem let me answer the second one because I need to know first one, here it is.
Private Sub DataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView.CurrentRow.Index
Form5.Label17.Text = DataGridView.Item(0, i).Value
Form5.Label25.Text= DataGridView.Item(1, i).Value
Form5.Label21.Text = DataGridView.Item(2, i).Value
Form5.Label19.Text = DataGridView.Item(3, i).Value
End Sub
it is only an example you can put this code either in Button Click or DataGridview Click then count your columns starting from 0 and replace the code above with the number of your desired column example.
DataGridView.Item(0, i).Value \\If 0 = BookID

You can try using a List (as Form level variable) and, after checking If List.Contains(RowId) (where RowId is an unique index of the row) Then Remove it from the List OrElse check If List.Count < 2 and Add the RowId to the List.
After that you can check the rows having the Id into the List and uncheck the ones removed from the list.

Related

Find all instances of a word in a string and display them in textbox (vb.net)

I have a string filled with the contents of a textbox (pretty large).
I want to search through it and display all occurances of this word. In addition I need the searchresult to display some charachters in the string before and after the actual searchterm to get the context for the word.
The code below is part of a code that takes keywords from a listbox one by one using For Each. The code displays the first occurance of a word together with the characters in front and after the word - and stop there. It will also display "no Match for: searched word" if not found.
As stated in the subject of this question - I need it to search the whole string and display all matches for a particular word together with the surrounding characters.
Where = InStr(txtScrape.Text, Search)
If Where <> 0 Then
txtScrape.Focus()
txtScrape.SelectionStart = Where - 10
txtScrape.SelectionLength = Where + 50
Result = txtScrape.SelectedText
AllResults = AllResults + Result
Else
AllResults = AllResults + "No Match for: " & item
End If
I recommend that you can split the string into long sentences by special symbols, such as , : ? .
Split(Char[])
You can refer to the following code.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = ""
Dim Index As Integer
Dim longStr() As String
Dim str = TextBox3.Text
longStr = TextBox1.Text.Split(New Char() {CChar(":"), CChar(","), CChar("."), CChar("?"), CChar("!")})
Index = 0
For Each TheStr In longStr
If TheStr.Contains(str) Then
RichTextBox1.AppendText(longStr(Index) & vbCrLf)
End If
Index = Index + 1
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "....."
End Sub
End Class
Result:
Try like this:
Dim ArrStr() As String
Dim Index As Integer
Dim TheStr As String
Dim MatchFound As Boolean
MatchFound = False
ArrStr = Split(txtScrape.text," ")
Index = 1
For Each TheStr In ArrStr
If TheStr = Search Then
Console.WriteLine(Index)
MatchFound = True
End If
Index = Index + 1
Next
Console.WriteLine(MatchFound)
Inside the If statement you will get the index there. And MatchFound is the Boolean value if match found.

how to check checklistbox items using datagridview vb.net?

I'm just a beginner for coding and I want to programmatically check items in checklistbox using datagridview.
Data grid view values are seperated with commas like this jhon,Metilda,saman,.
Checklistbox name as chklistinput and please help me to solve this ?
'Full coding is here..............................
Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles TextBox10.TextChanged
'this is ok and searching as I want
Dim SearchV As String = TextBox10.Text
SearchV = "%" + TextBox10.Text + "%"
Me.PassIssuingRecordTableAdapter.FillBy(Me.Database4DataSet.PassIssuingRecord, SearchV)
'But the problem bigins here
Dim areasback As String = DataGridView1.Rows(0).Cells(6).Value.ToString
Dim areasback1 As String() = areasback.Split(",")
For Each x In areasback1
For i = 0 To areasback.Count - 1
If chklistInput.Items(i).ToString() = x.ToString() Then
chklistInput.SetItemChecked(i, False)
End If
Next
Next
End Sub
You have to loop over chklistInput.Items.Count - 1 instead of areasback.Count - 1
use the following code:
Dim areasback As String = DataGridView1.Rows(0).Cells(6).Value.ToString
Dim areasback1 As String() = areasback.Split(",")
Dim intCount as integer = 0
For each str as string in areasback1
For intCount = 0 To chklistInput.Items.Count - 1
If chklistInput.Items(intCount).ToString() = str Then
chklistInput.SetItemChecked(intCount , True)
End If
Next
Next
chklistInput.Refresh()
Note: comparing is case sensitive

FInd a title by searching with only a portion of the text?

Title says it all, I need help making my search input box find the title and display it when the user only types part of the movie title before searching. This is what I have now and it works great, but you must type in the complete title. Any help would be appreciated! Thanks
Private Sub btnSearch_Click(sender As Object, e As EventArgs)Handles btnSearch.Click
'Searches for movie in listbox
Dim strDVDtitle As String
strDVDtitle = InputBox("Enter Movie Title:")
Dim X As Integer = 0
Dim bolDVDFound As Boolean = False
For X = 0 To count - 1
If DVDS(X).DVDtitle = strDVDtitle Then
txtDVDyear.Text = DVDS(X).DVDyear
txtDVDtitle.Text = DVDS(X).DVDtitle
txtDVDyear.Text = DVDS(X).DVDyear
txtDVDruntime.Text = DVDS(X).DVDruntime
txtDVDrating.Text = DVDS(X).DVDrating
bolDVDFound = True
End If
Next
If bolDVDFound = False Then
MessageBox.Show("Movie not found")
End If
End Sub
You can use the contains string method, like this:
Dim actualTitle = "The Martian"
If actualTitle.ToLower().Contains(strDVDtitle.ToLower()) Then
MsgBox("Match!")
End If

Send text from textboxes to datagrid

I have for textboxes on my form and I would like the text there to be send to a datagrid. I wrote the following:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim itemName As String = txtItem.Text
Dim qty As Double = CDbl(txtQTY.Text)
Dim price As Double = CDbl(txtPrice.Text)
Dim Total As Double = price * qty
txtTotal.Text = Convert.ToString(Total)
Dim row As Integer = grdNewInvoice.Rows.Count
Dim data As TextBox() = New TextBox() {txtItem, txtQTY, txtPrice, txtTotal}
grdNewInvoice.Rows.Add()
For i As Integer = 0 To data.Length - 1
grdNewInvoice(i, row).Value = data(0)
Next
End Sub
But I get the following on my datagrid row: System.Windows.Forms.TextBox, Text: [textbox string]
I tried the following code as well to make sure there was nothing wrong with my settings on my datagrid:
'grdNewInvoice.Rows(0).Cells(0).Value = itemName
'grdNewInvoice.Rows(0).Cells(1).Value = qty
'grdNewInvoice.Rows(0).Cells(2).Value = price
'grdNewInvoice.Rows(0).Cells(3).Value = Total
That worked fine and the text went in as expected but since I will be writing to multiple lines on the datagrid, I will need to use a loop.
What am I doing wrong here?
One way to do this is to use a list of string array.
Dim row As Integer = grdNewInvoice.Rows.Count
Dim data As New List(Of String())
data.Add({txtItem.Text, txtQTY.Text, txtPrice.Text, txtTotal.Text})
data.Add({"Item2", "qtr2", "price2", "total2"})
data.Add({"Item3", "qty3", "price3", "total3"})
For i As Integer = 0 To data.Count - 1
grdNewInvoice.Rows.Add(data(i))
Next

gettin the all the column values of datagridview in vb.net in variables on enter key event

Private Sub dgcustomerlist_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgcustomerlist.KeyPress
Dim custcode As String
Dim custname As String
Dim custmobile As String
'Dim rows As DataGridViewRow = dgcustomerlist.SelectedRows(0)
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
custcode = dgcustomerlist.SelectedCells(0).Value
custname = dgcustomerlist.SelectedCells(1).Value
custmobile = dgcustomerlist.SelectedCells(2).Value
MsgBox("the selected value is", custcode)
End If
heres the above code,the situation is that i want the column value of the selected row in gridview in the particular variables.but i am getting error in custname as it throws index out of range.kindly tell help me what i am doing wrong.
This error appears because CustName cell is not selected when the code is running (maybe because pressing ENTER deselect it).
You can set DataGridView.SelectionMode = DataGridViewSelectionMode.FullColumnSelect or you can use .CurrentRow.Cells instead of .SelectedCells; for example:
custcode = dgcustomerlist.CurrentRow.Cells(0).Value
custname = dgcustomerlist.CurrentRow.Cells(1).Value
custmobile = dgcustomerlist.CurrentRow.Cells(2).Value