Solved

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

Posted on 2014-10-20
7
396 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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