按钮无法正常工作 - 从C#中的按钮启动网页 [英] Button not working - launching a webpage from a button in C#

查看:137
本文介绍了按钮无法正常工作 - 从C#中的按钮启动网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,我创建了一个表单(WPF User Control),其中包含一个名为Tracking_Num的文本框,一个名为Courier_List的comboBox,以及一个名为Tracking_Button的按钮。想法是用户选择快递,输入跟踪号码,然后点击跟踪以启动默认浏览器并使用提供的快递跟踪包裹并跟踪#。



按钮完全不起作用 - 当我点击它甚至没有错误时没有任何反应。



这是我的XML代码:



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">





这是我的C#代码:



Here is my C# Code:

private void Tracking_Button_Click(object sender, RoutedEventArgs e)
        {

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

                    System.Diagnostics.Process.Start("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;
            }
        }





更多信息:我正在运行SCSM 2012 R2(此表单正在导入的程序我的项目的目标是在Visual Studio 2013中编码的3.5 .Net框架(SCSM要求),并在域管理员帐户下登录时运行该程序。





更新:


所以我很确定现在问题来自交换机没有识别正在进行选择或错误地将其传递给selectedValue。也许是在ToString方法中?将选择从comboBox传递到我的开关的最佳方法是什么?



More Info: I am running SCSM 2012 R2 (the program this form is being imported into), my project is targeted at a 3.5 .Net framework (required by SCSM) being coded in Visual Studio 2013, and running the program while logged on under a Domain Administrator account.


UPDATE:

So I am pretty sure now that the problem is coming from the switch not recognizing a selection is being made or incorrectly passing it to selectedValue. Perhaps it is in the ToString method? What is the best way to pass the selection from comboBox to my switch?

推荐答案

我认为你已经通过调试器逐步完成了它 - 例如在[Tracking_Button_Click]并确保按钮点击进入任何处理程序



你是否在海拔高度下运行?



这里有一个讨论有一些可能有用的东西 - 更多阅读抱歉 http://stackoverflow.com/questions/502199/how-to-open-a-web-page-from-my-application [ ^ ]
I presume you've stepped through it with a debugger - put a breakpoint for example in [Tracking_Button_Click] and made sure the button click is going into any of the handlers

Are you running under elevation ?

There's a discussion here that has a few things that might help - more reading sorry http://stackoverflow.com/questions/502199/how-to-open-a-web-page-from-my-application[^]


我解决了这个问题。我将selectedValue方法更改为selectedIndex,然后我将每个选项更改为与0 - 2的索引值相对应。



提供工作代码,感谢所有试图帮助的人!



I solved the problem. I changed selectedValue method to selectedIndex, then I changed each option to correspond with index values from 0 - 2.

Provided working code, thanks to all who tried to help!

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;
            }
        }


这篇关于按钮无法正常工作 - 从C#中的按钮启动网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆