Making a button open a hyperlink - c#

I've just started with Xamarin and C# to do some iOS development. I'm trying to make a button open a web page in Safari. I have added the button in the XCode visual editor and dragged the button into the code view to start using it in Xamarin.
I'm getting the same problem as this although I can't put the code in the view as the answer suggests I should as I need access to UIApplication to open the web page in Safari.
Here is my code:
Login.cs:
partial void webLink (MonoTouch.Foundation.NSObject sender)
{
UIApplication.SharedApplication.OpenUrl(new NSUrl("http://www.google.com/"));
}
And the error:
A partial method implementation is missing a partial method declaration

I suppose you added a UIButton in Xcode and then you also dragged the newly created UIButton to Assistant editor and named your instance e.g. btnOpenGoogle.
Next you need to implement the TouchUpInside event handler in ViewDidLoad:
btnOpenGoogle.TouchUpInside += HandleBtnOpenGoogleTouchUpInside;
And somewhere in your code create the HandleBtnOpenGoogleTouchUpInside method:
private void HandleBtnOpenGoogleTouchUpInside(object sender, EventArgs e)
{
UIApplication.SharedApplication.OpenUrl(new NSUrl("www.google.com"));
}
Hope this helps.

Just use an IBAction,
- (IBAction)ButtonName:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com/"]];
}
Just replace the 'ButtonName' with the name of your existing button, and then connect everything to the files owner in the storyboard.

Shouldn't that be
private/public void webLink (MonoTouch.Foundation.NSObject sender)
instead of
partial void webLink (MonoTouch.Foundation.NSObject sender) ?
I don't think you should have partial.

Related

Create a void method for each button

Hi im hoping someone can assist im still new to programming and this is a noob question but i have created a Visual studio - C# (Windows Form Application) and now the question reads to Create a void method for each of my buttons i created in the form and telling me even what to name the method.
but on my research The void keyword is used in method signatures to declare a method that does not return a value.
LinkToAddresses () will be my void method for address the (button), so my question is do i just put in this void method and its going to do nothing?
im just going to link the full question maybe im just really not understanding this>?
''
The below form will represent the main form from which the user will navigate to the other forms. Meaning each button should be linked to the appropriate form. E.g. If button Manage Addresses is clicked the form managed addresses should be displayed. The Exit button should successfully terminate the program.
Create a void method for each button and name them as follow: LinkToAddresses (), LinkToCustomers (), LinkToDrivers (), LinkToStatus (), and LinkToFreight (). The methods should be called under the appropriate button. For the exit button create a void method named AppExit () this should terminate the program.
''
I would appreciate any help or guidance, thank you in advance.
Visual studio usually handles the button actions easily. Just place the buttons on your form, then rename the buttons to LinkToAddresses, LinkToCustomers, LinkToDrivers, LinkToStatus, LinkToFreight and AppExit. Then simply just double click on the each button and visual studio will create a void method for their click event.
using System;
using System.Windows.Forms;
namespace YourApp
{
public partial class FormMain : Form
{
private FormManagedAddresses formManagedAddresses = null;
public FormMain()
{
InitializeComponent();
}
private void LinkToAddresses_Click(object sender, EventArgs e)
{
if (formManagedAddresses != null)
{
formManagedAddresses.Close();
}
formManagedAddresses = new FormNews();
formManagedAddresses.Show();
}
private void AppExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
The closest thing to a buttons function, is the Click Event Handler. While specific names vary based on Display technology (WinForms, WPF/UWP, ASP.Net), that is the general pattern for Graphical User Interfaces. It is called event driven programming. Even things that have a different programm flow like Games and Web Applications usually try to imitate it.
The signature of a event is given during its definition and must be strictly followed. Usually void NameOfTheEvent(object sender, SampleEventArgs e).
A return type of void is extremely common with events. If there is to be any output, that usually is handeled via a property in the Event Args or by directly doing stuff with the other GUI Elements.
If you want a button to do nothing, you just never give it a event handler. Every single button you ever used, was given a implicit or explicit event handler to do exactly what it did. If you want it to conditionally do nothing, either disable the Button so it can not be clicked, or put a proper if-statement into the event Handler.
A advanced topic would be the command pattern, where there is a bunch of commands in code behind. And each button, menu item and key combination is meerely a way to trigger said command - a representation for hte user to call the command.
You can share a single event across any number of Elements. AS you can see above, the pattern for events includes object sender as argument. This means you can check if it is a specific Button instance that called the event. Or even "unpack" the specific button, do look at stuff like Display String, Tag to get data from it. However, as a general rule retrieving data from the GUI is a bit frowned - ideally the GUI should only represent the data in the backend.

How to show output in webapp directlt at form1 C#

I want to print values at bottom of Form1 directly. It is a web app so if I
write Console.WriteLine("Something"); it doesn't make sense or doesn't print out any thing at Form1. How can I print in Form1 directly?
Here's a pictorial sequence for printing the word "Something" on a web form (on your browser):
Right-click on the blue highlighted WebApplication2.
A webform will appear:
Press F7 key to go to the webform code page.
Then look at your resulting browser page.
UPDATE:
Oh, so you want to write to a winform instead of a webform. Use this code:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "Something";
}
}
}
Then,
Put your mouse on label1, right-click, choose "View Designer".
In the properties box, go to the "Text" property.
To the right of the property, type "Click here" to replace "Label1".
Hit return.
Then run it.
I assume you are using a ASP.NET Web Forms and want to add controls directly to the HTML via code behind.
Look into UserControls for a more proper way moving forward. https://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
But to answer your question directly, the quick and dirty is below.
Within the aspx file create a PlaceHolder
<asp:PlaceHolder ID="phMain" runat="server"></asp:PlaceHolder>
In the code behind you can add HTML directly to the PlaceHolder
phMain.Controls.Add(new LiteralControl("<span>Hello!</span>"));
Update
I see you added a picture of a WinForm app after you said Web App. In that case you should try a ListView https://msdn.microsoft.com/en-us/library/system.windows.forms.listview(v=vs.110).aspx

Passing values from one page to another on a Windows phone project

I am trying to work out how to transfer string value from one windows phone app page to the next, following this article but I get an error that reads "The name 'NavigationService' does not exist in the current context".
There is a textbox and a button on page1. There is only a TEXTBLOCK on page 2. I wish to enter a string in the textbox of page 1, and when the button is clicked, the page 2 shows up with the text from the textbox (of page 1) in the textblock of page 2.
For the button click even in page 1, I entered this:
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page2.xaml?msg=" + Texbox_page1.Text, UriKind.Relative));
}
In the OnNavigatedTo method, in page 2, I entered
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
{
Textblock_nickname_display.Text = msg;
}
}
What am I missing? If there is n easier way to do this, please share!
I've spend almost 4 hours trying to figure this out, watching YouTube videos and what not!
Thanks in advance!
Make sure you have
using System.Windows.Navigation;
Also, make sure your pages inherit from the proper base class.
public class SecondPage : PhoneApplicationPage
{
...
}
EDIT:
Right click your project in the solution explorer and click Add > Reference and search for PresentationFramework. Adding that reference should give you the namespace you need.
Your project is Windows Phone 8.1 (Universal), So NavigationService isn't exist any longer.
You can use Frame.Navigate method in WP 8.1.
And below is my answer about Page Navigation in Windows Phone 8.1, please read it:
Windows Phone 8.1 - Page Navigation

C# Dragdrop events not working

i created a little form in C# and registered some drag drop events to it. But i cant get it to work properly!
Goal: Dragging files from explorer.exe into the form, and handeling it, and photos dragged into the form. I test the code by dragging a .txt file from my desktop into the form. It shows the (/) cursor when doing so
Code:
public partial class Viewer : Form
{
public Viewer()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Viewer_DragEnter);
this.DragDrop += new DragEventHandler(Viewer_DragDrop);
}
public void Viewer_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragEnter");// Line has breakpoint
}
public void Viewer_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragDrop: "+e.Data.GetFormats());// Line has breakpoint
}
}
What i have tried:
Try-catch over the constructor => nothing
Running succesfull build outside of visualstudio(2010)
Running explorer.exe as administrator when dragging items into the form
Thanks for reading my question
EDIT:
when testing the files were on the desktop(%userprofile%\Desktop). but it shouldnt matter where they are stored. the images are supposed to be dragged from the browser or a word document into the form
ANOTHER EDIT:
When trying to run it without visual studio, i get an InvalidOperationException, that occurs on the AllowDrop = true; but i dont get a reaction when try-catching it
SOLUTION:
OMG... so.... i kinda moved the public static Main() into the viewer.cs file.... and i forgot to add the STAThread attribute.
I found this when executing the build outside of visual studio again, and in the exception form it showed that the thread needed to be a STAThread but that part of the message was hidden and hard to find
anyway: ALWAYS USE STATHREAD :P

How to open a window then load my XAML file to it? (XAML and C#)

I am new to WPF and C# and i need some help - sorry about the newbie question!
Anyway I have a control panel 'window' as the file thats loaded when i run my project, and i then have a button on this control panel, that when clicked triggers an event. Inside this 'event function' I am trying to load a new window that has its own XAML code behind, how do i go about doing this? I have googled but to no avail.
Please explain in laymens terms, I am still getting the hang of this all.
Thanks!
private void btnCustomers_clicked(object sender, RoutedEventArgs e)
{
//load in Customers.xaml file here - in a new window
}
You need to declare an instance of the class that is your other window then call Show() on it. So if your other window is call MySecondWindow you write the following in your event handler.
MySecondWindow otherWindow = new MySecondWindow();
otherWindow.Show();
A basic explination of how windows work can be found on the MSDN Site.

Categories

Resources