-
Jan 4th, 2014, 11:11 AM
#1
Thread Starter
New Member
trying to create a button
I'm posting this new thread because i finally fixed my code and all thats left for me to do is to create a search button. In my last post nobody really gave me any advice, so i figured i would try to explain this in the most simplest way: I'm just trying to create a search button and i need help doing it. I am a beginner. I just want the program to ask the user what he would like to search for, when the search button is clicked on. when the user types in the word he wants to search for.....i want the program to look in the text file to see if that word exists and if the word exists i want that word to appear in my listbox. I am studying trimester. so i have not been going to class for 4 months i have been going for almost 2 we have not discussed findstring or findstringexact. I looked up info on them but i dont know how to use them correctly with streamreader i think i have to create a new string but i get confused because i am a beginner. if you could be able to help me i would be extremely grateful. If not thank you for taking the time to look at my post. Here is my code so far, if you see anything wrong with what i have written so far feel free to give me some advice to make it better. Thank you. (I have not created a search button):
Code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim archive As StreamWriter = New StreamWriter("stuff.txt", True)
ListBox1.Items.Add(TextBox1.Text)
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
archive.WriteLine(TextBox1.Text)
archive.Close()
MsgBox("ok")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim archive As StreamReader = New StreamReader("stuff.txt", True)
ListBox1.Items.Clear()
While archive.Peek() > -1
ListBox1.Items.Add(archive.ReadLine)
End While
archive.Close()
End Sub
Public Sub DeleteLine(ByRef fileaddress As String, ByRef listbox1_selecteditem As String)
Dim thefilelines As New List(Of String)
thefilelines.AddRange(File.ReadAllLines(fileaddress))
thefilelines.Remove(listbox1_selecteditem)
File.WriteAllLines(fileaddress, thefilelines.ToArray)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
DeleteLine("product.txt", ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
MsgBox("Item Deleted")
End Sub
End Class
-
Jan 4th, 2014, 11:32 AM
#2
Re: trying to create a button
when the user types in the word he wants to search for.....i want the program to look in the text file to see if that word exists and if the word exists i want that word to appear in my listbox.
I'm afraid this is the point at which I simply get lost. What's the point? If this is all the search does then why not simply give a message that says 'Yes it's there' or 'No it's not'? What further use is there for this word that warrants putting it in a listbox (or indeed searching for it in the first place)?
In broader terms why are you using streams for simple text file commands in one place then a different method altogether in another? If you're going to be searching a text for anything it's always going to be more efficient to do it in memory than through file in any case.
I'm inclined to dispute that "In my last post nobody really gave me any advice". The point at issue was always whether there was anything to be gained in giving you a way to do something that really had no obvious value. That remains the case. We're still frankly baffled by what the whole program is actually for.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jan 4th, 2014, 11:48 AM
#3
Thread Starter
New Member
Re: trying to create a button
Originally Posted by dunfiddlin
I'm inclined to dispute that "In my last post nobody really gave me any advice". The point at issue was always whether there was anything to be gained in giving you a way to do something that really had no obvious value. That remains the case. We're still frankly baffled by what the whole program is actually for.
once again i must mention that i am STUDYING. this is an assignment that i am doing for my UNIVERSITY. I am not asking you to do my assignment for me. I am just asking if you can help me figure out how to create a search button for this kind of code for the sole purpose of practicing. I am just practicing how to do certain things when i write code. there are many ways to write code and even if you will never use them in your daily lives its just good to know. so you dont have to be baffled by what this program is because its sole purpose is for me to practice code.
-
Jan 4th, 2014, 12:38 PM
#4
Re: trying to create a button
How can the ListBox.FindString Method or ListBox.FindStringExact Method help in the slightest with searching a text file? A textfile is a document that resides on your computer, a listbox is a control on your form. The have no relationship with each other. Take ListBox.FindStringExact for example because you have not read the documentation for it (this is how you learn) clearly states Finds the first item in the ListBox that exactly matches the specified string. At not One part that it states it can search a text file.
So you want to read a textfile, so use the File.ReadAllText Method. I'm not going to wish to hard that you read this but it's there in case you decide to.
vb Code:
Imports System.IO
Public Class Form1
Private Function ReadAllText(ByVal filePath As String) As String
Dim fileText As String = Nothing
If File.Exists(filePath) Then
fileText = File.ReadAllText(filePath)
End If
Return fileText
End Function
End Class
Next we need to search for the specified word. There are a few different ways, i'm a regex fan so going to use regex.
vb Code:
Public Class Form1
Private Function WordExists(ByVal fileText As String) As Boolean
Return New System.Text.RegularExpressions.Regex(String.Format("(?i)\b{0}\b", Me.TextBox1.Text)).Match(fileText).Success
End Function
End Class
-
Jan 4th, 2014, 12:45 PM
#5
Re: trying to create a button
Originally Posted by AA123
once again i must mention that i am STUDYING. this is an assignment that i am doing for my UNIVERSITY. I am not asking you to do my assignment for me. I am just asking if you can help me figure out how to create a search button for this kind of code for the sole purpose of practicing. I am just practicing how to do certain things when i write code. there are many ways to write code and even if you will never use them in your daily lives its just good to know. so you dont have to be baffled by what this program is because its sole purpose is for me to practice code.
I agree with dunfiddlin, members did offer the little advice they could manage with the written explanation of what you was doing. It was and still is extremely hard to follow. Your code is even harder. Where are you learning this, are you just googling code and pasting it? Members will help, i myself have now provided Three MSDN links for you to read and learn.
-
Jan 4th, 2014, 03:04 PM
#6
Re: trying to create a button
I just want the program to ask the user what he would like to search for, when the search button is clicked on. when the user types in the word he wants to search for.....i want the program to look in the text file to see if that word exists and if the word exists i want that word to appear in my listbox.
To do that read the entire file into a string using IO.File.ReadAllText(filename), then using the string.contains method. If it returns true your word is in the file and you would put it in the textbox.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jan 4th, 2014, 04:33 PM
#7
Re: trying to create a button
once again i must mention that i am STUDYING. this is an assignment that i am doing for my UNIVERSITY.
Well here's an idea. How about giving us the text of the assignment so that we can have some idea of where you're headed (or at least where you think you're headed)? I couldn't care less whether this is a University assignment or a mission for the CIA. If you don't tell us what you are aiming at then we are completely unable to determine whether you're going about it the right way or not (though, unless this is a really poor University course, I'm more than prepared to hazard a guess!) You act as though it should be blindingly obvious what is required despite the fact that every one of the people that has responded to your posts has said that the exact opposite is true. At some point you're going to have to accept that you are not doing us a favour by deigning to condescend to asking for help here! All of us have far better things to do with our time than try to work out what an assignment we have never seen asks for from your incomplete and often nonsensical hints.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|