Solved

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

Posted on 2014-10-20
7
374 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 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
Comment Utility
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 5

Author Comment

by:Sir Learnalot
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
try the navigation window.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Accepted Solution

by:
Sir Learnalot earned 0 total points
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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 5

Author Comment

by:Sir Learnalot
Comment Utility
@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 5

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now