Solved

Button not working - launching a webpage from a button in C#

Posted on 2014-10-20
7
400 Views
Last Modified: 2014-10-27
Hello Everyone, I made a form (WPF User Control) with a textbox called Tracking_Num, a comboBox called Courier_List, and a button called Tracking_Button. The idea is the user selects a courier, enters the tracking number, and hits track to launch the default browser and track the package using the provided courier and tracking #.
 
The button is completely not working - nothing happens when I click it not even an error.
 
Here is my XML code:
<ComboBox x:Name="Courier_List" Margin="102,363.711,83,125.874" ItemsSource="{Binding Path=Courier_List, Mode=Default, UpdateSourceTrigger=PropertyChanged}" >
        <ComboBoxItem x:Name="UPS" Content="UPS"/>
        <ComboBoxItem x:Name="FedEX" Content="FedEX"/>
        <ComboBoxItem x:Name="UPS_SCS" Content="UPS SCS"/>
</ComboBox>
 
<TextBox x:Name="Tracking_Num" Margin="102,393.2,83,96.237" Text="{Binding Path=Tracking_Num, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
 
<Button x:Name="Tracking_Button" Content="Track!" HorizontalAlignment="Left" Margin="284.6,393.2,0,0" VerticalAlignment="Top" Width="75" Height="24.8" Click="Tracking_Button_Click">

Open in new window


Here is my C# code:
<ComboBox x:Name="Courier_List" Margin="102,363.711,83,125.874" ItemsSource="{Binding Path=Courier_List, Mode=Default, UpdateSourceTrigger=PropertyChanged}" >
        <ComboBoxItem x:Name="UPS" Content="UPS"/>
        <ComboBoxItem x:Name="FedEX" Content="FedEX"/>
        <ComboBoxItem x:Name="UPS_SCS" Content="UPS SCS"/>
</ComboBox>
 
<TextBox x:Name="Tracking_Num" Margin="102,393.2,83,96.237" Text="{Binding Path=Tracking_Num, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
 
<Button x:Name="Tracking_Button" Content="Track!" HorizontalAlignment="Left" Margin="284.6,393.2,0,0" VerticalAlignment="Top" Width="75" Height="24.8" Click="Tracking_Button_Click">

Open in new window

0
Comment
Question by:Sir Learnalot
  • 4
  • 3
7 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 40393251
the C# code is the same as the XML code.

To launch the browser:
first format your url.

string URL = "https://google.com";
System.Diagnostics.Process.Start(URL);

or you can also do a NavigationWindow:
NavigationWindow nw = new NavigationWindow();
  uri = new Uri("https://google.com", UriKind.Absolute);
  nw.Navigate(uri);
  nw.Show();

Open in new window

0
 
LVL 6

Author Comment

by:Sir Learnalot
ID: 40394634
My mistake. Here is my C# Code.

I was already using the System.Diagnostics.Process.STart(URL) method but it wasn't working so the first option there has IExplore.exe just for debug purposes (not that it helped).


private void Tracking_Button_Click(object sender, RoutedEventArgs e)
        {

            switch (Courier_List.SelectedValue.ToString())
            {
                case "UPS":

                    System.Diagnostics.Process.Start("IExplore.exe", "http://wwwapps.ups.com/ietracking/tracking.cgi?loc=CA_CA^&tracknum^=" + Tracking_Num.Text.ToString());
                    break;

                case "FedEX":
                    System.Diagnostics.Process.Start("https://www.fedex.com/fedextrack/index.html?tracknumbers^=" + Tracking_Num.Text.ToString() + "^&locale=en_CA^&cntry_code=ca_english");
                    break;

                case "UPS_SCS":
                    System.Diagnostics.Process.Start("https://www.upspostsaleslogistics.com/cfw/trackOrder.do?trackNumber^=" + Tracking_Num.Text.ToString());
                    break;

                default:
                    break;
            }
        }

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40394821
try the navigation window.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 6

Accepted Solution

by:
Sir Learnalot earned 0 total points
ID: 40397605
I ended up fixing this on my own:

private void Tracking_Button_Click(object sender, RoutedEventArgs e)
        {
            var SelectedCourier = Courier_List.SelectedIndex;
            switch (SelectedCourier)
            {
                case 0:
                   
                    try
                    {
                        System.Diagnostics.Process.Start("http://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=en_CA&Requester=UPSHome&WBPM_lid=homepage%2Fct1.html_pnl_trk&trackNums=" + Tracking_Num.Text.ToString() + "&track.x=Track");
                    }
                    catch (Exception ex)
                    {
                       MessageBox.Show(ex.Message);
                    }
            
                    break;

                    
                case 1:
                    try
                    {
                        System.Diagnostics.Process.Start("https://www.fedex.com/fedextrack/WTRK/index.html?action=track&action=track&ascend_header=1&clienttype=dotcomreg&mi=n&cntry_code=ca_english&language=english&tracknumbers=" + Tracking_Num.Text.ToString() + "&fdx=1490");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;

                case 2:
                    try
                    {
                    System.Diagnostics.Process.Start("https://www.upspostsaleslogistics.com/cfw/trackOrder.do?trackNumber=" + Tracking_Num.Text.ToString());
                    }
                    catch (Exception ex)
                    {
                       MessageBox.Show(ex.Message);
                    }
                    break;

                //default:
                //    break;
            }
        }

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40397646
Per http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28541134.html#a40393251  the suggestion was to first format the URL and then call the System.Diagnostics.Process.Start.  

Since the code hasn't changed much I can only assume the issue was with how the URL was being formatted.  Had you had used the the https://google.com as an example you would have seen it was an issue with the URL.  I believe full points should be warranted for this.
0
 
LVL 6

Author Comment

by:Sir Learnalot
ID: 40397674
@Kyle

1. The way the points system is setup, I can't recant the points I awarded (as far as I am aware).

2. If you read the posts thoroughly, you would have discovered I had already been using that format for my strings... I even explicitly said it in my post. The problem was not the format of the URL at all actually, had you read my solution you would realize I used SelectedIndex instead of SelectedValue, and changed the case to match the index # instead of looking for a string match...

3. Your answers literally helped me 0%. I actually requested attention on the topic because it was clear to me the answers were not pointing in the right direction... I had a hunch the problem was in the passing of the selection to the button handler... and I was correct. The only reason I gave you points is for effort, because you took the time to reply to my post. 250 points is more than generous.

I am sorry that you are not pleased with how I marked the solution, I hope that my explanation gave you some insight as to why it was awarded the way it was.

Have a great day and enjoy the rest of your week :)
0
 
LVL 6

Author Closing Comment

by:Sir Learnalot
ID: 40405977
I have provided points for effort but my solution was done completely independently. Thanks for all your efforts!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question