FInd a title by searching with only a portion of the text? - vb.net

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

Related

Null Exception while loading my file to listbox.

I have a problem. I'm trying to load data to listbox , but when I click start I get an unhandled exception that states values cannot be null. I've bolded where the issue is.
I'm trying to create a library database, and would really appreciate if someone could help me fix this issue. Thanks!
Public Class frmLibrary
Structure BookData
Dim Title As String
Dim Author As String
Dim ISBN As Integer
Dim YearPublished As Integer
End Structure
Dim bookinfo() As BookData 'Array
Private Sub frmLibrary_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bookfiles() As String = IO.File.ReadAllLines("LibraryDatabase.txt")
Dim n As Integer = bookfiles.Count - 1
ReDim bookinfo(n)
Dim line As String
Dim books() As String 'books are the parts
'use the split methods to assign values to the members of the structure variable.
For i As Integer = 0 To n ---**>Why is this value null???????**
line = bookfiles(i)
books = line.Split(","c) '
bookinfo(i).Title = books(0)
bookinfo(i).Author = books(1)
bookinfo(i).ISBN = books(2)
bookinfo(i).YearPublished = books(3)
Next
Dim query = From book In bookinfo
Select book.Title, book.Author, book.ISBN, book.YearPublished
DGVBookInfo.DataSource = query.ToList
DGVBookInfo.Columns("Title").HeaderText = "Title"
DGVBookInfo.Columns("Author").HeaderText = "Author"
DGVBookInfo.Columns("ISBN").HeaderText = "ISBN"
DGVBookInfo.Columns("Year Published").HeaderText = "Year Published"
DGVBookInfo.AutoResizeColumns()
DGVBookInfo.RowHeadersVisible = False
End Sub

Index was outside the bounds of the array in vb.net while reading a text file

I have a piece of text that has a list of Oscar movies from 1928 - 2014. I'm using an array to find the name of the film and place it into one textbox and the genre in the next. The genre is next to the movies name. I'm trying to access the two separately and place it into two different textboxes.
My Code:
Dim mvs(86) As Movie
Private Sub FrmAcademy_Load() Handles MyBase.Load
Dim mviLine As String
Dim mviData(1) As String
Dim allMovies() As String = IO.File.ReadAllLines("Oscars.txt")
For mviList As Integer = 0 To 86
mviLine = allMovies(mviList)
mviData = mviLine.Split(","c)
mvs(mviList).mviName = mviData(0)
mvs(mviList).gnrName = mviData(1)
Dim mviYear As Integer = 0
Dim mviIndex As Integer = 0
If (IsNumeric(txtAwards.Text)) = True Then
mviYear = CInt(txtAwards.Text)
End If
If (mviYear >= 1928) Or (mviYear <= 2014) Then
mviIndex = 1928 - mviYear
txtFilm.Text = mvs(mviIndex).mviName
txtGenre.Text = mvs(mviIndex).mviName
End If
Next
End Sub
Private Sub btnPicture_Click(sender As Object, e As EventArgs) Handles btnPicture.Click
If (IsNumeric(txtAwards.Text)) = False Then
MessageBox.Show("Please enter a year as a numeric value")
ElseIf CInt(txtAwards.Text) > 2014 Then
MessageBox.Show("Please choose a year in range")
ElseIf CInt(txtAwards.Text) < 1928 Then
MessageBox.Show("Please choose a year in range")
End If
Isn't it supposed to be:
If (mviYear >= 1928) And (mviYear <= 2014) Then
mviIndex = mviYear - 1928
To get better help, at least take the time to format your code properly.
Your mvs array is declared, but where is each element initialized?
On the first line of your For loop, add this code: mvs(mviList) = New Movie()

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

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.

Is it possible to do this code with less redundancy?

I have the following code:
If moves.Contains("1") Then
lblOnes.Visible = True
End If
If moves.Contains("2") Then
lblTwos.Visible = True
End If
If moves.Contains("3") Then
lblThrees.Visible = True
End If
If moves.Contains("4") Then
lblFours.Visible = True
End If
If moves.Contains("5") Then
lblFives.Visible = True
End If
If moves.Contains("6") Then
lblSixes.Visible = True
End If
I just feel like it is redundant, is there any way to do this without repeating the same statement over and over?
You could e.g. use a look up using a Dictionary:
Dim map = new Dictionary(Of String, Label) From
{
{"2", lblTwos},
{"3", lblThrees},
{"4", lblFours},
{"5", lblFives},
{"6", lblSixes}
}
For Each kvp In map
If moves.Contains(kvp.Key) Then
kvp.value.Visible = True
End If
Next
Other possible ways:
use the Tag property of the controls
name your controls lbl_1, lbl_2 etc. and loop over all elements in moves to find the right control by its name.
Another example:
Dim lbls() As Label = {lblOnes, lblTwos, lblThrees, lblFours, lblFives, lblSixes}
For i As Integer = 0 To lbls.Length - 1
If moves.Contains((i + 1).ToString) Then
lbls(i).Visible = True
Else
' ... possibly do something in here? ...
End If
Next
If you have the luxury of renaming your Labels from lblOnes, lblTwos etc. to lbl1s, lbl2s, then it would simply be:
For i = 1 To 6
Me.Controls("lbl" & i & "s").Visible = moves.Contains(i.ToString())
Next
I propose following idea:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim moves As String
moves = "1"
Dim controlName As String
controlName = "lbl" + moves
CType(Me.Controls("controlName"), Label).Visible = True
End Sub

What is wrong with my subroutines?

So I've been working on this project for a couple of weeks, as I self teach. I've hit a wall, and the community here has been so helpful I come again with a problem.
Basically, I have an input box where a user inputs a name. The name is then displayed in a listbox. The name is also put into an XML table if it is not there already.
There is a button near the list box that allows the user to remove names from the list box. This amends the XML, not removing the name from the table, but adding an end time to that name's child EndTime.
If the user then adds the same name to the input box, the XML gets appended to add another StartTime rather than create a new element.
All of this functions well enough (My code is probably clunky, but it's been working so far.) The problem comes when I try to validate the text box before passing everything through to XML. What I am trying to accomplish is that if the name exists in the listbox on the form (i.e hasn't been deleted by the user) then nothing happens to the XML, the input box is cleared. This is to prevent false timestamps due to a user accidentally typing the same name twice.
Anyhow, I hope that makes sense, I'm tired as hell. The code I've got is as follows:
Private Sub Button1_Click_2(sender As System.Object, e As System.EventArgs) Handles addPlayerButton.Click
playerTypeCheck()
addPlayerXML()
clearAddBox()
End Sub
Private Sub playerTypeCheck()
If playerTypeCBox.SelectedIndex = 0 Then
addMiner()
ElseIf playerTypeCBox.SelectedIndex = 1 Then
addHauler()
ElseIf playerTypeCBox.SelectedIndex = 2 Then
addForeman()
End If
End Sub
Private Sub addMiner()
If minerAddBox.Text = String.Empty Then
Return
End If
If minerListBox.Items.Contains(UCase(minerAddBox.Text)) = True Then
Return
Else : minerListBox.Items.Add(UCase(minerAddBox.Text))
End If
If ComboBox1.Items.Contains(UCase(minerAddBox.Text)) = True Then
Return
Else : ComboBox1.Items.Add(UCase(minerAddBox.Text))
End If
End Sub
Private Sub addPlayerXML()
If System.IO.File.Exists("Miners.xml") Then
Dim xmlSearch As New XmlDocument()
xmlSearch.Load("Miners.xml")
Dim nod As XmlNode = xmlSearch.DocumentElement()
If minerAddBox.Text = "" Then
Return
Else
If playerTypeCBox.SelectedIndex = 0 Then
nod = xmlSearch.SelectSingleNode("/Mining_Op/Miners/Miner[#Name='" + UCase(minerAddBox.Text) + "']")
ElseIf playerTypeCBox.SelectedIndex = 1 Then
nod = xmlSearch.SelectSingleNode("/Mining_Op/Haulers/Hauler[#Name='" + UCase(minerAddBox.Text) + "']")
ElseIf playerTypeCBox.SelectedIndex = 2 Then
nod = xmlSearch.SelectSingleNode("/Mining_Op/Foremen/Foreman[#Name='" + UCase(minerAddBox.Text) + "']")
End If
If nod IsNot Nothing Then
nodeValidatedXML()
Else
Dim docFrag As XmlDocumentFragment = xmlSearch.CreateDocumentFragment()
Dim cr As String = Environment.NewLine
Dim newPlayer As String = ""
Dim nod2 As XmlNode = xmlSearch.SelectSingleNode("/Mining_Op/Miners")
If playerTypeCBox.SelectedIndex = 0 Then
newMinerXML()
ElseIf playerTypeCBox.SelectedIndex = 1 Then
newHaulerXML()
ElseIf playerTypeCBox.SelectedIndex = 2 Then
newForemanXML()
End If
End If
End If
Else
newXML()
End If
End Sub
Private Sub nodeValidatedXML()
If playerTypeCBox.SelectedIndex = 0 Then
minerValidatedXML()
ElseIf playerTypeCBox.SelectedIndex = 1 Then
haulerValidatedXML()
ElseIf playerTypeCBox.SelectedIndex = 2 Then
foremanValidatedXML()
End If
End Sub
Private Sub minerValidatedXML()
If minerListBox.Items.Contains(UCase(minerAddBox.Text)) = False Then
appendMinerTimeXML()
End If
End Sub
Private Sub appendMinerTimeXML()
Dim xmlSearch As New XmlDocument()
xmlSearch.Load("Miners.xml")
Dim docFrag As XmlDocumentFragment = xmlSearch.CreateDocumentFragment()
Dim cr As String = Environment.NewLine
Dim newStartTime As String = Now & ", "
Dim nod2 As XmlNode = xmlSearch.SelectSingleNode("/Mining_Op/Miners/Miner[#Name='" & UCase(minerAddBox.Text) & "']/StartTime")
docFrag.InnerXml = newStartTime
nod2.AppendChild(docFrag)
xmlSearch.Save("Miners.xml")
End Sub
And lastly, the clearAddBox() subroutine
Private Sub clearAddBox()
minerAddBox.Text = ""
End Sub
So, I should point out, that if I rewrite the nodeValidated() Subroutine to something like:
Private Sub nodeValidatedXML()
If playerTypeCBox.SelectedIndex = 0 Then
appendMinerTimeXML()
ElseIf playerTypeCBox.SelectedIndex = 1 Then
appendHaulerTimeXML()
ElseIf playerTypeCBox.SelectedIndex = 2 Then
appendForemanTimeXML()
End If
End Sub
then all of the XML works, except it adds timestamps on names that already exist in the list, which is what i'm trying to avoid. So if I haven't completely pissed you off yet, what is it about the minerValidated() subroutine that is failing to call appendMinerTimeXML()? I feel the problem is either in the minerValidated() sub, or perhaps clearAddBox() is somehow firing and I'm missing it? Thanks for taking the time to slog through this.
Edit: Clarification. The code as I have it right now is failing to append the XML at all. Everything writes fine the first time, but when I remove a name from the list and then re-add, no timestamp is added to the XML.
You need to prevent the user accidentally typing the name twice.(Not sure if you mean adding it twice)
For this I believe you need to clear the minerAddBox.Text in your addminer() if this line is true.
minerListBox.Items.Contains(UCase(minerAddBox.Text)) = True
minerAddBox.Text = ""
Return
Now it will return back to your addplayerXML which will Return to your clearbox(), since you have this in your addplayerXML()
If minerAddBox.Text = "" Then
Return
Now you get to your clearbox() (Which is not really needed now since you cleared the minerAddBox.Text already)
when I remove a name from the list and then re-add, no timestamp is added to the XML.
your minerValidatedXML() is true, because you are not clearing the textbox when you re-add a name to the list box. Or you may need to remove the existing listbox item if it is the same as the textbox
If minerListBox.Items.Contains(UCase(minerAddBox.Text)) = True Then
minerListBox.Items.remove(UCase(minerAddBox.Text))