Hi, I am creating a web browser that has a search engine component that links automatically to google news.
it contains a textbox as the search component, and on pressing enter the search will be carried out.
The way I am approaching it is google automatically has a first part of the link that everything has which is " and after this it will say the text so for example if the search was "hello" then this "hello" would be appended to the end of the "=" and if it was "hello world" it would append "hello+world".
My code works when one word is placed in the text box, albeit in a slightly cheating way, if the word is hello it has the link as " so although there is a "+" at the end, it still only searches for the "hello".
However, when I have more than one word it causes this error:
"Index and length must refer to a location within the string.
Parameter name: length"
I have been trying to figure this out for hours and am sure its somethin simple.
I have posted the main bits of my code below, if any1 would be kind enough to read through it and tell me why it is doing this I would be most grateful:
----------------------------------------------------------
My code:
----------------------------------------------------------
public void goToURL(String theURL)
{
try
{
Cursor.Current = Cursors.WaitCursor;
Object o = null;
axWebBrowser2.Navigate(theURL, ref o, ref o, ref o, ref o);
}
finally
{
Cursor.Current = Cursors.Default;
}
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
String googleSearch = "
if(e.KeyCode == Keys.Enter)
{
parse(textBox1.Text);
for (int i = 0; i<listOfWords.Count; i++)
{
googleSearch = googleSearch + listOfWords + "+";
}
goToURL(googleSearch);
listOfWords.Clear();
}
}
private void parse(String textToParse)
{
// If word has a space
if(textToParse.IndexOf(" ") != -1)
{
// Find index of space
int index = textToParse.IndexOf(" ");
// Parse out word before space
String word = textToParse.Substring(0,index);
// Add to listOfWords
listOfWords.Add(word);
// Parse out word after first word
String endWord = textToParse.Substring(index, textToParse.Length);
// Trim any spaces at start or end of word
endWord.Trim();
// Repeat process
parse(endWord);
}
// If indexOf(" ") equals null then this is the last word so its added to list
else
{
listOfWords.Add(textToParse);
}
it contains a textbox as the search component, and on pressing enter the search will be carried out.
The way I am approaching it is google automatically has a first part of the link that everything has which is " and after this it will say the text so for example if the search was "hello" then this "hello" would be appended to the end of the "=" and if it was "hello world" it would append "hello+world".
My code works when one word is placed in the text box, albeit in a slightly cheating way, if the word is hello it has the link as " so although there is a "+" at the end, it still only searches for the "hello".
However, when I have more than one word it causes this error:
"Index and length must refer to a location within the string.
Parameter name: length"
I have been trying to figure this out for hours and am sure its somethin simple.
I have posted the main bits of my code below, if any1 would be kind enough to read through it and tell me why it is doing this I would be most grateful:
----------------------------------------------------------
My code:
----------------------------------------------------------
public void goToURL(String theURL)
{
try
{
Cursor.Current = Cursors.WaitCursor;
Object o = null;
axWebBrowser2.Navigate(theURL, ref o, ref o, ref o, ref o);
}
finally
{
Cursor.Current = Cursors.Default;
}
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
String googleSearch = "
if(e.KeyCode == Keys.Enter)
{
parse(textBox1.Text);
for (int i = 0; i<listOfWords.Count; i++)
{
googleSearch = googleSearch + listOfWords + "+";
}
goToURL(googleSearch);
listOfWords.Clear();
}
}
private void parse(String textToParse)
{
// If word has a space
if(textToParse.IndexOf(" ") != -1)
{
// Find index of space
int index = textToParse.IndexOf(" ");
// Parse out word before space
String word = textToParse.Substring(0,index);
// Add to listOfWords
listOfWords.Add(word);
// Parse out word after first word
String endWord = textToParse.Substring(index, textToParse.Length);
// Trim any spaces at start or end of word
endWord.Trim();
// Repeat process
parse(endWord);
}
// If indexOf(" ") equals null then this is the last word so its added to list
else
{
listOfWords.Add(textToParse);
}