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

📄 viewer.cs

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

namespace PollsViewer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Viewer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid Options;
		private System.Windows.Forms.Label Question;
		private System.Windows.Forms.Timer TimerRefresh;
		private System.Windows.Forms.Button ButtonRefresh;
		private System.ComponentModel.IContainer components;

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.TimerRefresh = new System.Windows.Forms.Timer(this.components);
			this.Options = new System.Windows.Forms.DataGrid();
			this.Question = new System.Windows.Forms.Label();
			this.ButtonRefresh = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.Options)).BeginInit();
			this.SuspendLayout();
			// 
			// TimerRefresh
			// 
			this.TimerRefresh.Enabled = true;
			this.TimerRefresh.Interval = 5000;
			this.TimerRefresh.Tick += new System.EventHandler(this.RefreshTimer_Tick);
			// 
			// Options
			// 
			this.Options.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.Options.CaptionVisible = false;
			this.Options.DataMember = "";
			this.Options.Location = new System.Drawing.Point(8, 40);
			this.Options.Name = "Options";
			this.Options.ReadOnly = true;
			this.Options.Size = new System.Drawing.Size(456, 184);
			this.Options.TabIndex = 0;
			// 
			// Question
			// 
			this.Question.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.Question.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Question.ForeColor = System.Drawing.Color.Red;
			this.Question.Location = new System.Drawing.Point(8, 8);
			this.Question.Name = "Question";
			this.Question.Size = new System.Drawing.Size(376, 16);
			this.Question.TabIndex = 1;
			this.Question.Text = "label1";
			// 
			// ButtonRefresh
			// 
			this.ButtonRefresh.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.ButtonRefresh.Location = new System.Drawing.Point(392, 8);
			this.ButtonRefresh.Name = "ButtonRefresh";
			this.ButtonRefresh.Size = new System.Drawing.Size(72, 24);
			this.ButtonRefresh.TabIndex = 2;
			this.ButtonRefresh.Text = "&Refresh";
			this.ButtonRefresh.Click += new System.EventHandler(this.ButtonRefresh_Click);
			// 
			// Viewer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(472, 229);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.ButtonRefresh,
																		  this.Question,
																		  this.Options});
			this.Name = "Viewer";
			this.Text = "PollsViewer";
			this.Load += new System.EventHandler(this.Viewer_Load);
			((System.ComponentModel.ISupportInitialize)(this.Options)).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)
		{
			FillOptionsGrid();
		}

		private void FillOptionsGrid()
		{
			// get the dataset through the webservice
			DataSet poll = new ThePhile.Poll().GetCurrent();
			Options.DataSource = poll.Tables["Options"];

			// set the question title
			Question.Text = string.Format("{0}   ( {1} votes )", 
				poll.Tables["Questions"].Rows[0]["QuestionText"].ToString(),
				poll.Tables["Questions"].Rows[0]["TotalVotes"].ToString());

			// remove the current grid columns
			Options.TableStyles.Clear();

			// bind Options table to the Grid, and show the OptionText, TotalVotes, and Percentage
			Options.SetDataBinding(poll, "Options");
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = "Options";
			ts.AlternatingBackColor = Color.LightGray;
          
			// Add the OptionText column
			DataGridColumnStyle OptionCol = new DataGridTextBoxColumn();
			OptionCol.MappingName = "OptionText";
			OptionCol.HeaderText = "Option";
			OptionCol.Width = 300;
			ts.GridColumnStyles.Add(OptionCol);

			// Add the TotalVotes column
			DataGridColumnStyle VotesCol = new DataGridTextBoxColumn();
			VotesCol.MappingName = "TotalVotes";
			VotesCol.HeaderText = "Votes";
			VotesCol.Width = 50;
			VotesCol.Alignment = HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(VotesCol);

			// Add the Percentage column
			DataGridColumnStyle PercCol = new DataGridTextBoxColumn();
			PercCol.MappingName = "Percentage";
			PercCol.HeaderText = "%";
			PercCol.Width = 50;
			PercCol.Alignment = HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(PercCol);

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

		private void RefreshTimer_Tick(object sender, System.EventArgs e)
		{
			// refresh the grid
			FillOptionsGrid();
		}

		private void ButtonRefresh_Click(object sender, System.EventArgs e)
		{
			// refresh the grid
			FillOptionsGrid();
		}

	}
}

⌨️ 快捷键说明

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