Why does this ONLY search Google? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
It skips the navigating to valid URL and goes straight to a Google search. If I enter "stackoverflow.com" into textbox it will Google search for "stackoverflow.com".
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text); //navigates to url in textbox
if (!textBox1.Text.StartsWith("http://"))
{
webBrowser1.Navigate("http://www.google.ie/search?q=" + (textBox1.Text));
}
}

That code, in English, basically says "navigate to whatever was entered into the textbox. Then, IF whatever you entered into the textbox does NOT start with 'http://' let's immediately do a Google search for it."
Basically you are navigating to stackoverflow.com but you're then immediately Google searching for it instead. If you want it to act differently, you need to code it differently.
Here's how I'd rework it:
private void button1_Click(object sender, EventArgs e)
{
if (!textBox1.Text.StartsWith("http://"))
{
// didn't start with "http://" so search for it
webBrowser1.Navigate("http://www.google.ie/search?q=" + (textBox1.Text));
}
else
{
// navigate directly to the URL
webBrowser1.Navigate(textBox1.Text);
}
}
In an if-else statement, only one logic path will be chosen based on conditions. It will never be the case that both run.

It searches Google because your logic is saying that 'if the textbox contents doesn't start with http:// let's do a Google search'. You are actually loading stackoverflow.com, but you immediately change the page and load Google instead.
You might want the code below with your code modified to 'filter' out and save some memory by not loading stackoverflow.com first:
private void button1_Click(object sender, EventArgs e)
{
if (!textBox1.Text.StartsWith("http://") | !textBox1.Text.StartsWith("www") || !textBox1.Text.StartsWith("http://www"))
{
webBrowser1.Navigate("http://www.google.ie/search?q=" + (textBox1.Text));
}
else
{
webBrowser1.Navigate(textBox1.Text); //navigates to url in textbox
}
}

Related

No events trigger and buttons are not pressable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
When creating a winforms application I added a pannel as a header so I could make the application borderless, I was going to add some buttons when I realised no events were getting registered by the program. Have I flicked a wrong switch somewhere?
I have already tried clicking the main form and changing the AutoValidation as well as checking that the form, as well as the panel, were both enabled.
public App()
{
InitializeComponent();
}
private void TopBar_MouseHover(object sender, EventArgs e)
{
Application.Exit();
}
private void ExitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
Expect result is that it should just close the application when I hover over the topbar or when I click the ExitButton.
I fixed this by going through the properties of the Forms/Buttons and resetting them as it turns out I had one item disabled.

navigation to the webpage was canceled webbrowser control c# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
i started a create a simple web browser...
when i code it web page is not loading...
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("textBox1.Text");
}
}
}
This is the error when i go to a web page.
When you used the "" literals, you told the Compiler to treat what is between these "" as a literal string and not a valid C# expression. So the expression will not be evaluated. And your WebBrowser control will receive this literal string "textBox1.Text" exactly as it is. And not the Text property of textBox1 Control. By removing these "" literals:
webBrowser1.Navigate(textBox1.Text);
Your WebBrowser control will receive the value of Text property of the textBox1 control.
use webBrowser1.Navigate(textBox1.Text); instead

Fire a comboBox SelectedIndexChanged event programmatically in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
There is a comboBox and a button on a win form. How can I fire the comboBox selectedIndexChanged by clicking on the button.
You should rethink your code design a little. It seems you want to raise the event to trigger some action indirectly. Why not try it like this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// do the things that should happen only if a real change happend
// ...
// then do the special thing you want
DoTheOtherStuff();
}
private void button1_Clicked(object sender, EventArgs e)
{
// do the things to happen when the button is clicked
// ...
// then do the special thing you want
DoTheOtherStuff();
}
private void DoTheOtherStuff()
{
// the special thing you want
}
EDIT: If your legacy code is so awkward as your comment suggests, you can still use this awkward way:
private void button1_Clicked(object sender, EventArgs e)
{
comboBox1_SelectedIndexChanged(comboBox1, EventArgs.Empty);
}

Calculating the average time between button clicks [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm doing this Windows Forms application in C# so when you click this button multiple times, it calculates the average clicks per second you do.
I just yesterday started learning C# and the only language im good at is Lua. In Lua this would be simple, just use a table as they're very dynamic and flexible. I just have no clue how to do this in C#, the MSDN Articles just confuse me.
How would I store the times between clicks? Arrays? I have no clue. This is the button click function I have so far
private void button1_Click(object sender, EventArgs e)
{
//DO STUFF
}
You could store the time of each click, then compute the average of the times between them:
private List<DateTime> clickTimes = new List<DateTime>();
private void button1_Click(object sender, EventArgs e)
{
this.clickTimes.Add(DateTime.Now);
if (this.clickTimes.Count > 2)
{
double averageSeconds = this.clickTimes.Zip(this.clickTimes.Skip(1), (a,b) => (b-a).TotalSeconds)).Average();
// Do something with the average seconds between each click here
}
}

How to close all child windows with a method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Having hard time dealing with this because every time I navigate to another form my project lags or is unresponsive for a few seconds before it shows the content of the window.
How can I close all the child windows with a method?
I have tried this.Close() but that didn't work.
You may use Application Class as a place of storing a links to the child windows. It creates a singleton Current which is accesable from any place
public partial class App : Application
{
private List<Window> childWindows = new List<Window>();
public List<Window> ChildWindows{get{return childWindows;}}
}
To register new ChildWindow add this into Initialized event handler
private void ChildWindow_Initialized(object sender, EventArgs e)
{
((App)Aplication.Current).Windows.Add(this);
}
on ChildWindow Closing you have to remove link from collection
private void ChildWindow_Closed(object sender, EventArgs e)
{
((App)Aplication.Current).Windows.Remove(this) // here
}
if needs you may replace List with Dictionary to have a possibility of searching by key

Categories