⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewer.cs

📁 wrox c#高级编程
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace NewsViewer
{

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Viewer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox Categories;
		private System.Windows.Forms.DataGrid Headlines;
		private System.Windows.Forms.Button RefreshButton;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Timer RefreshTimer;

		private DataSet dsHeadlines;
		private DataSet dsCategories;
		private bool categoriesLoaded;

		public Viewer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.Headlines = new System.Windows.Forms.DataGrid();
			this.RefreshTimer = new System.Windows.Forms.Timer(this.components);
			this.Categories = new System.Windows.Forms.ComboBox();
			this.RefreshButton = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.Headlines)).BeginInit();
			this.SuspendLayout();
			// 
			// Headlines
			// 
			this.Headlines.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.Headlines.CaptionVisible = false;
			this.Headlines.DataMember = "";
			this.Headlines.Location = new System.Drawing.Point(8, 40);
			this.Headlines.Name = "Headlines";
			this.Headlines.ReadOnly = true;
			this.Headlines.Size = new System.Drawing.Size(520, 226);
			this.Headlines.TabIndex = 1;
			this.Headlines.DoubleClick += new System.EventHandler(this.NewsGrid_DoubleClick);
			// 
			// RefreshTimer
			// 
			this.RefreshTimer.Enabled = true;
			this.RefreshTimer.Interval = 300000;
			this.RefreshTimer.Tick += new System.EventHandler(this.RefreshTimer_Tick);
			// 
			// Categories
			// 
			this.Categories.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.Categories.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.Categories.DropDownWidth = 256;
			this.Categories.Location = new System.Drawing.Point(8, 8);
			this.Categories.Name = "Categories";
			this.Categories.Size = new System.Drawing.Size(448, 21);
			this.Categories.TabIndex = 2;
			this.Categories.SelectedIndexChanged += new System.EventHandler(this.Categories_SelectedIndexChanged);
			// 
			// RefreshButton
			// 
			this.RefreshButton.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.RefreshButton.Location = new System.Drawing.Point(464, 8);
			this.RefreshButton.Name = "RefreshButton";
			this.RefreshButton.Size = new System.Drawing.Size(64, 23);
			this.RefreshButton.TabIndex = 0;
			this.RefreshButton.Text = "&Refresh";
			this.RefreshButton.Click += new System.EventHandler(this.RefreshButton_Click);
			// 
			// Viewer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(536, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.RefreshButton,
																		  this.Headlines,
																		  this.Categories});
			this.Name = "Viewer";
			this.Text = "News Viewer";
			this.Load += new System.EventHandler(this.Viewer_Load);
			((System.ComponentModel.ISupportInitialize)(this.Headlines)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Viewer());
		}

		private void Viewer_Load(object sender, System.EventArgs e)
		{
			
			// get the categories and the headlines for the first category (if any)
			dsCategories = new ThePhile.Headlines().GetCategories();
			// bind dsCategories to the ComboBox, display the Name and
			// keep the CategoryID as value for each item
			Categories.DataSource = dsCategories.Tables[0].DefaultView;
			Categories.DisplayMember = "Name";
			Categories.ValueMember = "CategoryID";
			categoriesLoaded = true;

			// fill the grid with the headlines
			FillHeadlinesGrid();
		}

		private void FillHeadlinesGrid()
		{
			// if the categories has not been loaded yet,
			// or if there is no category in the ComboBox --> exit now
			if (!categoriesLoaded || Categories.Items.Count == 0) return;

			// get the headlines for the selected category
			dsHeadlines = new ThePhile.Headlines().GetHeadlines(
				(int)Categories.SelectedValue
			);
			
			// remove the current grid columns (if any)
			Headlines.TableStyles.Clear();

			// bind dsHeadlines to the Grid, and show the ReleaseDate and
			// Headline titles in two custom columns
			Headlines.SetDataBinding(dsHeadlines, "Headlines");
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = "Headlines";
			ts.AlternatingBackColor = Color.LightGray;
          
			// Add a the ReleaseDate column
			DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
			TextCol.MappingName = "ReleaseDate";
			TextCol.HeaderText = "Release Date";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);

			// Add a the headline's title column
			DataGridColumnStyle TitleCol = new DataGridTextBoxColumn();
			TitleCol.MappingName = "Title";
			TitleCol.HeaderText = "Headline";
			TitleCol.Width = 400;
			ts.GridColumnStyles.Add(TitleCol);

			// Add a the (hidden) NewsUrl column
			DataGridColumnStyle UrlCol = new DataGridTextBoxColumn();
			UrlCol.MappingName = "NewsUrl";
			UrlCol.Width = 0;
			ts.GridColumnStyles.Add(UrlCol);

			// finally add the table to the grid
			Headlines.TableStyles.Add(ts);
		}

		private void NewsGrid_DoubleClick(object sender, System.EventArgs e)
		{
			// get the current cursor position relative to the Headlines
			Point pt = Headlines.PointToClient(Cursor.Position);
			// navigate to the link only if the user clicked on the row header
			//if (navigateLink)
			if (Headlines.HitTest(pt.X, pt.Y).Type == DataGrid.HitTestType.RowHeader)
			{
				BindingManagerBase bmGrid;
				bmGrid = BindingContext[dsHeadlines, "Headlines"];
				// open the default browser and navigate to the Url
				// of the currently selected row
				System.Diagnostics.Process.Start(
						((DataRowView)bmGrid.Current)["NewsUrl"].ToString()
					);
			}
		}

		private void RefreshTimer_Tick(object sender, System.EventArgs e)
		{
			FillHeadlinesGrid();
		}

		private void Categories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			FillHeadlinesGrid();
		}

		private void RefreshButton_Click(object sender, System.EventArgs e)
		{
			FillHeadlinesGrid();
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -