/*I am reading many files and getting data through File.ReadAllLines. Now I want to search in these files for a specific string written in a textbox. Whenever I put some text in the textbox it must return lines of text containing that word. I am coding in textchanged property but it is not successful as it gives me a result even when I press backspace or add any other word. */
I have successfully made it to work. I was clearing the listbox every time it runs else statement. Now I just want you people to tel me what should I do to make it work fast.
if you don't want to get the result right away when you type something in Textbox then put Button on your form and try this code:
string[] lines=File.ReadAllLines(path);
var result = lines.Where(l => l.Contains("text")).ToList();
I hope this helps
Related
I'm creating a Desktop App in Visual Studio With C#.
I've a String array called Tag_Word_Box. In a textbox if I type something, then it will be show suggest words from Tag_Word_Box array.
In briefly, assume that- I've 5 words respectively [aabc] [abc] [abd] [abcg] [bcd] If I type in textbox only 'a' then it will be show all of words without 'bcd'.
If I select 'aabc' or press enter one of suggesting words, then- it will be assign whole word in textbox, that means textbox value will be changed with selecting word by 'a'.
BTW, I know that- it will be solved by trie algorithm to find out words. But I want to know that how to do that operation in visual studio respect of C# that-
1. to show suggesting words when I type something
2. and how do I change textbox value by selecting from those?
Thanks :)
All textboxs have an 'AutoCompleteSource' property. Set it to CustomSource from the Properties toolbar. Then set the 'AutoCompleteMode' property to SuggestAppend. Now, in the code, add this to the TextChanged Event of the textbox:
var autocomplete = new AutoCompleteStringCollection();
autocomplete.AddRange(Tag_Word_Box);
textBoxName.AutoCompleteCustomSource = autocomplete;
To do the complete thing in code, add this to the TextChanged event of your textbox:
textBoxName.AutoCompleteSource=AutoCompleteSource.CustomSource;
textBoxName.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
var autocomplete = new AutoCompleteStringCollection();
autocomplete.AddRange(Tag_Word_Box);
textBoxName.AutoCompleteCustomSource = autocomplete;
Remember to replace textBoxName by the name of your textbox before using this code.
I am working on a wpf app, c#, in visual studio. My aim is to take the selected item text from a listbox on button press and add it to a list whilst also appending it to a text block. My code thus far:
bill.BillItems.Add(lstbxVeg.SelectedItem.ToString());
txtblkBill.Text = String.Join(Environment.NewLine, bill.BillItems);
Where bill is the class name, BillItems the List name. I can see that items are indeed added to the text block, yet instead of the desired string relating to the listbox item selected, it reads System.Data.DataRowView. Where am I going wrong?
EDIT: This is not the correct answer.
lstbxVeg.SelectedItem.Text.ToString()
I think the actual problem is this line:
txtblkBill.Text = String.Join(Environment.NewLine, bill.BillItems);
You mentioned you want to append the value, this line is not accomplishing that.
I think you want to do this:
txtblkBill.Text += lstbxVeg.SelectedItem.ToString();
If that isn't the answer you should look up String.Join() as it takes a String[] not a List.
I am working on a project for school. I have to create and SQL statement and then use a while loop to input information into a label. Every thing is working correctly except when you search for a new item, the label still displays the result from the last search. Code Picture Second Result Picture
Any way to fix this?
You should clear the label's text before you start looping.
you need to clear the label text first then show the result on it.
label1.text="";
You need to clear before start a new loop.
Try this:
Nameofyourlabel.Text = "";
Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"
How do you get the values of their textboxes and transfer them to richtextbox?
I created a windows application form that contains a button1, a richtextbox, and a webbrowser(set to facebook.)
I want this button1, that if clicked, it will then copy the values that were typed in the registration forms of facebook.com and then pastes it to the richtextbox.
These are the IDs of their text boxes.
"firstname"
"lastname"
"reg_email__"
"reg_email_confirmation__"
"reg_passwd__"
"sex"
"birthday_month"
"birthday_day"
"birthday_year"
How do you programmatically get the values that were typed in the facebook textboxes and then transfer them to richtextbox?
Are you asking for someone to write the program for you? Doubt that will happen.
So, ignoring the obviously question of "why?", I'll give a few pointers.
First, look at the View Source of the page you are intereted in. It can sometimes be easier (with fixed formats - especially with textboxes that have fixed and unique names) to scan through the html source as text and just substring out the tags with the names you want. If the textboxes are not populated at build time (i.e. the values are not in the view source), then you will probabably have to go through the DOM and access the controls individually.
Depending how and when you are accessing this page, you may well find these fields are not populated at all by FB (for the obvious security reasons) and only act as input from the user (hence my "why?" question earlier).
Assuming that you are working with WinForms you can do use the WebBrowser control's Document property. It returns an object of type HtmlDocument. HtmlDocument in turn has a method GetElementById returning an object of type HtmlElement. The OuterHtml property finally contains the information about the textbox. Using a Regex expression you can extract the required info
HtmlElement tb = webBrowser1.Document.GetElementById("firstname");
string outerHtml = tb.OuterHtml;
// Yields a text which looks like this
// <INPUT id=firstname class=inputtext value=SomeValue type=text name=firstname>
string text = Regex.Match(outerHtml, #"value=(.*) type=text").Groups[1].Value;
// text => "SomeValue"
I would prefer to be able to access the HTML-DOM object model; however it seems to be hidden from c#.
EDIT:
The ComboBoxes yield a different kind of information. Note that I use the InnerHtml here.
HtmlElement cb = webBrowser1.Document.GetElementById("sex");
string innerHtml = cb.InnerHtml;
// Yields a text which looks like this where the selected option is marked with "selected"
// <OPTION value=0>Select Sex:</OPTION><OPTION value=1>Female</OPTION><OPTION selected value=2>Male</OPTION>
Match match = Regex.Match(innerHtml, #"<OPTION selected value=(\d+)>(.*?)</OPTION>");
string optionValue = match.Groups[1].Value;
string optionText = match.Groups[2].Value;