How to get a button to open a web page? - c#

I've looked everywhere, including the MSDN forums, but no one has even mentioned this or ways to do it. The basic idea is that once a Button is dragged from the toolkit, how do you then link that button to a web page, ie I have a 'Facebook' button, how do I then make it so that when the button is clicked, Facebook opens in a new browser window?

Once you've dragged the button onto the designer, you can double-click on it to open up the Button's Click event handler. This is the code that will get run when the user clicks. You can then add the required logic, ie:
private void button1_Click(object sender, EventArgs e)
{
// Launch browser to facebook...
System.Diagnostics.Process.Start("http://www.facebook.com");
}

You can use Process.Start with the desired URL to open the default browser and navigate to the page:
using system.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
Process.Start("http://www.YouTube.com");
}

Since you said open in a new browser window, I was thinking the context was an web application you were developing, so this would apply in that case:
Add an HTML button with the window.open JavaScript method. You don't need or want code-behind here because it's all happening on the client. Here's a snippet, and there are a few other options you can pass to window.open to control behavior.
<input id="Button2" type="button" value="Facebook" onclick="window.open('http://facebook.com')"/></p>

You have to use Process class under System.dll which is be default added to the solution. But you should refer the namespace at the top of your class this way:
using System;
namespace MyApplication
{
public class MyProgram
{
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.facebook.com");
}
}
}

Related

I want to call the appshell with a button but the app crashes XamarinForms

i'm trying to create a back button from a content page to the main page, but the app crashes, i've tried with popasycn, navigation remoe, etc, is there some way to call the app shell from a button?
public void OnRegClicked(object sender, EventArgs e)
{
Navigation.RemovePage(Navigation.NavigationStack[1]);
}

tabPage1_Click event not triggering

I am using a tab in my program to switch between two forms. I put the code required to switch between forms within the tabPage1_Click event, but it doesn't trigger when the tab is clicked.
I attached the code and properties of the tab. Please let me know if any other information is required to know the problem. Thanks.
private void tabPage1_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.ShowDialog();
this.Close();
}
There are 2 things involved here. Tab control and Tab pages. Tab Control is the parent object which has multiple Tab pages in it.
You have event handler for Tab Page which is tabpage1_Click and not for Tab Control.
tabpage1_Click will be triggered when you click on tab page 1(not on the tab page header).
If you need to capture an event when you click on tab page header use Tab Control click event, something like below.
private void tabControl1_Click(object sender, EventArgs e)
{
//Your code goes here
}
To access the properties of the tab page use tabControl1.SelectedTab

WPF: Frame NavigationService GoBack request is not working

In my WPF appln, I have a Dialog window with couple of buttons and For each button click I can Navigate the pages in the MainWindow by "Frame.Navigate(_page);" .But from the page I am unable to go back my previous dialog window. I used "Frame.NavigationService.GoBack();". But it is not going back to the Dialog window.It is not moving out from the MainWindow.
Can Anyone please resolve my problem?
Go back can happen only when navigationService.CanGoBack is true. Ensure the value of this property. You can go back if navigation is done earlier. This actually functions similar to undo redo.
Also I verified with following snippet that works fine for me,
NavigationService service;
public MainWindow()
{
InitializeComponent();
service = mainframe.NavigationService;
service.Navigate("Page2.xaml");
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (service.CanGoBack)
service.GoBack();
}

How can I access to e.handled for a button in silverlight?

I have some buttons in master page (mainpage) and I use the event click of its in other page by map like:
MainPage mp = (MainPage)(((BusinessApplication8.Controls.BusyIndicator)Application.Current.RootVisual).Content);
it's being run more than one and I can not use a e.Handdled to prevent them.
How can I do to control it?
for example we want our code inside below event(ListPish_Emza_Click) runs only once per click :
void ListPish_Emza_Click(object sender, RoutedEventArgs e)
{
if (sender == e.OriginalSource)
return;
Approve_Click(sender, e);
}
but above code runs more than one per click.
Firing an event manually is not recommended approach.
I can't realize your application architecture. But, you should put your code in a method in static class and use it.

c# webbrowser get its url from text box

Ok i am trying to make my webbrowser(webbrowser1) get a url that is read from a textbox (textbox1 ) so the user can pick where the web browser connects from
i have tried:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Url = textBox1.Text();
}
And:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Url = textBox1.Text;
}
and some outer methods but i can not seem to get it to do it is it possible to do this for a web browser ?
(i mean a form web browser in the project not Firefox, chrome and so on ... )
You're handling the DocumentCompleted, which fires when the document has completed loading.
That's probably not what you want; you probably want that code to run when the user wants it.
You shouldn't override DocumentCompleted method. What you want to do is have a button, or in your custom textBox1 override lost focus or one of the action methods. With button override the click.
I guess you could override text changed in textBox1 and detect enter key pressed or some other trigger.
But regardless, you don't want to override DocumentCompleted
webBrowser1.Navigate(new Uri(textBox1.text));
would help you navigate to the URL string.
Here are some real simple examples:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.url.aspx

Categories