www.pudn.com > C sharp和MapObjects实现.rar > frmBus.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
 
namespace MainSystem 
{ 
	///  
	/// Summary description for frmBus. 
	///  
	public class frmBus : System.Windows.Forms.Form 
	{ 
		private frmMain _frmMain; 
		private bool	_bTextBox1 = true; 
		private bool	_bTextBox2 = true; 
 
		private System.Windows.Forms.Label _label_end; 
		private System.Windows.Forms.Label _label_start; 
		private System.Windows.Forms.ListBox _listBox_end; 
		private System.Windows.Forms.ListBox _listBox_start; 
		private System.Windows.Forms.Button _button_search; 
		private System.Windows.Forms.TextBox _textBox_end; 
		private System.Windows.Forms.TextBox _textBox_start; 
		///  
		/// Required designer variable. 
		///  
		private System.ComponentModel.Container components = null; 
 
		public frmBus(frmMain frm) 
		{ 
			_frmMain = frm; 
			InitializeComponent(); 
		} 
 
		///  
		/// Clean up any resources being used. 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if(components != null) 
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows Form Designer generated code 
		///  
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		///  
		private void InitializeComponent() 
		{ 
			this._label_end = new System.Windows.Forms.Label(); 
			this._label_start = new System.Windows.Forms.Label(); 
			this._listBox_end = new System.Windows.Forms.ListBox(); 
			this._listBox_start = new System.Windows.Forms.ListBox(); 
			this._button_search = new System.Windows.Forms.Button(); 
			this._textBox_end = new System.Windows.Forms.TextBox(); 
			this._textBox_start = new System.Windows.Forms.TextBox(); 
			this.SuspendLayout(); 
			//  
			// _label_end 
			//  
			this._label_end.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); 
			this._label_end.Location = new System.Drawing.Point(152, 8); 
			this._label_end.Name = "_label_end"; 
			this._label_end.Size = new System.Drawing.Size(80, 16); 
			this._label_end.TabIndex = 14; 
			this._label_end.Text = "终点站:"; 
			//  
			// _label_start 
			//  
			this._label_start.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); 
			this._label_start.Location = new System.Drawing.Point(8, 8); 
			this._label_start.Name = "_label_start"; 
			this._label_start.Size = new System.Drawing.Size(80, 16); 
			this._label_start.TabIndex = 13; 
			this._label_start.Text = "起点站:"; 
			//  
			// _listBox_end 
			//  
			this._listBox_end.ItemHeight = 12; 
			this._listBox_end.Location = new System.Drawing.Point(152, 56); 
			this._listBox_end.Name = "_listBox_end"; 
			this._listBox_end.Size = new System.Drawing.Size(128, 208); 
			this._listBox_end.TabIndex = 12; 
			this._listBox_end.MouseDown += new System.Windows.Forms.MouseEventHandler(this._listBox_end_MouseDown); 
			this._listBox_end.SelectedIndexChanged += new System.EventHandler(this._listBox_end_SelectedIndexChanged); 
			//  
			// _listBox_start 
			//  
			this._listBox_start.ItemHeight = 12; 
			this._listBox_start.Location = new System.Drawing.Point(8, 56); 
			this._listBox_start.Name = "_listBox_start"; 
			this._listBox_start.Size = new System.Drawing.Size(128, 208); 
			this._listBox_start.TabIndex = 11; 
			this._listBox_start.MouseDown += new System.Windows.Forms.MouseEventHandler(this._listBox_start_MouseDown); 
			this._listBox_start.SelectedIndexChanged += new System.EventHandler(this._listBox_start_SelectedIndexChanged); 
			//  
			// _button_search 
			//  
			this._button_search.Location = new System.Drawing.Point(296, 24); 
			this._button_search.Name = "_button_search"; 
			this._button_search.Size = new System.Drawing.Size(72, 24); 
			this._button_search.TabIndex = 10; 
			this._button_search.Text = "查询"; 
			this._button_search.Click += new System.EventHandler(this._button_search_Click); 
			//  
			// _textBox_end 
			//  
			this._textBox_end.Location = new System.Drawing.Point(152, 24); 
			this._textBox_end.Name = "_textBox_end"; 
			this._textBox_end.Size = new System.Drawing.Size(128, 21); 
			this._textBox_end.TabIndex = 9; 
			this._textBox_end.Text = ""; 
			this._textBox_end.TextChanged += new System.EventHandler(this._textBox_end_TextChanged); 
			//  
			// _textBox_start 
			//  
			this._textBox_start.Location = new System.Drawing.Point(8, 24); 
			this._textBox_start.Name = "_textBox_start"; 
			this._textBox_start.Size = new System.Drawing.Size(128, 21); 
			this._textBox_start.TabIndex = 8; 
			this._textBox_start.Text = ""; 
			this._textBox_start.TextChanged += new System.EventHandler(this._textBox_start_TextChanged); 
			//  
			// frmBus 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(376, 273); 
			this.Controls.AddRange(new System.Windows.Forms.Control[] { 
																		  this._label_end, 
																		  this._label_start, 
																		  this._listBox_end, 
																		  this._listBox_start, 
																		  this._button_search, 
																		  this._textBox_end, 
																		  this._textBox_start}); 
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow; 
			this.Name = "frmBus"; 
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
			this.Text = "选择乘车路线"; 
			this.Load += new System.EventHandler(this.frmBus_Load); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		private void frmBus_Load(object sender, System.EventArgs e) 
		{ 
			LoadListBox(); 
		} 
 
		private void LoadListBox() 
		{ 
			_listBox_start.DataSource  = _frmMain._environment.m_dataSet.Tables["公交车站"]; 
			_listBox_start.DisplayMember = "站名"; 
			_listBox_end.DataSource  = _frmMain._environment.m_dataSet.Tables["公交车站2"]; 
			_listBox_end.DisplayMember = "站名"; 
		} 
 
		private void _listBox_start_SelectedIndexChanged(object sender, System.EventArgs e) 
		{ 
			if (!_bTextBox1) 
				this._textBox_start.Text = _listBox_start.Text; 
		} 
 
		private void _listBox_end_SelectedIndexChanged(object sender, System.EventArgs e) 
		{ 
			if (!_bTextBox2) 
				this._textBox_end.Text = _listBox_end.Text; 
		} 
 
		private void _textBox_start_TextChanged(object sender, System.EventArgs e) 
		{ 
			int nIndex = this._listBox_start.FindString(this._textBox_start.Text,-1);  
			if (nIndex != ListBox.NoMatches ) 
			{ 
				_listBox_start.SelectedIndex = nIndex; 
			} 
 
			if ((this._textBox_end.Text != "") && (this._textBox_start.Text != "")) 
				this._button_search.Enabled = true; 
			else 
				this._button_search.Enabled = false;  
			_bTextBox1 = true; 
		} 
 
		private void _textBox_end_TextChanged(object sender, System.EventArgs e) 
		{ 
			int nIndex = this._listBox_end.FindString(this._textBox_end.Text,-1);  
			if (nIndex != ListBox.NoMatches ) 
			{ 
				_listBox_end.SelectedIndex = nIndex; 
			} 
 
			if (this._textBox_end.Text != "" && this._textBox_start.Text != "") 
				this._button_search.Enabled = true; 
			else 
				this._button_search.Enabled = false;  
			_bTextBox2 = true; 
		} 
 
		private void _listBox_start_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
		{ 
			_bTextBox1 = false; 
		} 
 
		private void _listBox_end_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
		{ 
			_bTextBox2 = false; 
		} 
 
		private void _button_search_Click(object sender, System.EventArgs e) 
		{ 
			//判断站名的正确性 
			if (!IsValidStation(this._textBox_start.Text)) 
			{ 
				MessageBox.Show("错误的起始站名。","北京市地理信息公众查询系统",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
			if (!IsValidStation(this._textBox_end.Text)) 
			{ 
				MessageBox.Show("错误的终点站名。","北京市地理信息公众查询系统",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			CEnvironment env = _frmMain._environment; 
			System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;   
			ArrayList array = new ArrayList(); 
			env.m_path.Search(this._textBox_start.Text,_textBox_end.Text,array); 
			System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Arrow;   
   
			if (array.Count ==0) 
			{ 
				MessageBox.Show("没有合适的乘车路线。","北京市地理信息公众查询系统",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
			else 
			{ 
				frmBusResult frm = new frmBusResult(this._frmMain,array); 
				this.Visible = false; 
				frm.ShowDialog(_frmMain);  
				this.Close(); 
			} 
		} 
 
		private bool IsValidStation(string szStation) 
		{ 
			System.Data.DataTable indexTbl = _frmMain._environment.m_dataSet.Tables["公交车站"];		 
			System.Data.DataRow[] rowsType = indexTbl.Select("站名='"+szStation+"'"); 
			if (rowsType.Length > 0) 
				return true; 
			return false; 
		} 
 
 
	} 
}