这个是一个基于winform的客户端程序,使用了网络提供的web服务,进行国内机票查询,本文主要讲解一下具体的客户端开发

主界面如下图:

 

使用网络提供的web服务开发航班查询程序

关于如何引用一个web服务,请参见一些网络资料,比如:MSDN

下面,我为了使用多线程,并且向线程中传递参数,新建了一个类库thread.cs,具体代码如下:

 

 WindowsService
{
    class thread
    {
        
string startcity;//定义输出参数,出发城市
        string endcity;
        
string date;//出发时间
        DataSet ds;//返回的数据集
        
        
public string Scity
        {
            
get { return startcity; }
            
set { startcity = value; }
        }
        
public string Ecity
        {
            
get { return endcity; }
            
set { endcity = value; }
        }
        
public string Dt
        {
            
get { return date; }
            
set { date = value; }
        }
        
public DataSet Ds
        {
            
get { return ds; }
        }
        
public DataSet GetTime()
        {
            
try
            {
                airline.DomesticAirline airline 
= new WindowsService.airline.DomesticAirline();//实例化服务
                ds = airline.getDomesticAirlinesTime(Scity, Ecity, Dt, "");//输入参数,得到查询结果
                return ds;//返回结果
            }
            
catch (Exception e)
            {
                
throw new Exception(e.ToString());
            }
        }
    }
}

 

通过这个类库,就完成了向线程中传递参数Scity,Ecity等工作了。下面我们设置窗体代码如下:

 

 WindowsService
{
    public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
            UpdateGridview 
= new GetTimeAir(GetTimes);
        }
        
string date;//预先定义好参数
        string sc;
        
string ec;

        
delegate void GetTimeAir();//定义委托,实现多线程
        GetTimeAir UpdateGridview;
        
private void button1_Click(object sender, EventArgs e)
        {
            
try
            {
                Thread 
get = new Thread(GetTimes);
                
get.IsBackground = true;
                
get.Start();
                statusBar1.Text 
= "正在获取数据,请耐心等待"
            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
finally
            {
                statusBar1.Text 
= "数据收集完成,耐心等待显示"
                
            }
        }
        
private void GetTimes()//主要的方法
        {
            
if (dataGridView1.InvokeRequired)
            {
                
this.Invoke(UpdateGridview);
            }
            
else
            {
                
try
                {
                    thread th 
= new thread();//实例化thread的类th
                    th.Scity = sc;
                    th.Ecity 
= ec;
                    th.Dt 
= date;
                    dataGridView1.DataSource 
= th.GetTime().Tables[0];
                }
                
catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                
finally
                {

                }
            }
        }

        
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            date 
= dateTimePicker1.Value.ToShortDateString();//获取起飞时间
        }
       
       

        
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            sc 
= comboBox1.SelectedItem.ToString();//获取出发城市
        }

        
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ec 
= comboBox2.SelectedItem.ToString();//获取抵达城市
        }
       
    }
}

 

至此,整个过程都好了,调试一下,由于网速,数据量大小问题,请耐心点哦。对了这里提供下载整个项目:点击下载

相关文章:

  • 2021-04-14
  • 2022-02-15
  • 2021-10-01
  • 2021-10-25
  • 2021-07-04
  • 2021-09-28
  • 2021-11-17
  • 2022-01-13
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2021-10-02
  • 2021-07-31
  • 2021-08-03
相关资源
相似解决方案