如何在用户按Enter键时转换星期几中的日期 [英] how to convert date in dayof week as user press enter key

查看:91
本文介绍了如何在用户按Enter键时转换星期几中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码...
我有一个数据网格视图,其中包含两列第一天和第二天(星期天星期一..ans等)我的问题是,随着用户在第一单元格中输入日期并按Enter键,光标应在下一个波纹管单元上移动,并且日期应增加到7天和日期列填充的是相关日期值(星期日星期一...)


日期日期
2005年12月5日,星期一
2005年5月19日星期一
2005年5月26日星期一
2005年03月6日,星期一
2005年10月6日,星期一
2005年6月17日,星期一,这个日期不应大于2006年12月31日.

请帮助我的朋友

感谢和问候
lakhan

here is my code ...
i have a data grid view which contain two column 1st date and 2nd day(sunday Monday..ans so on)my problem is that as user enter date in fist cell and press enter key cursor should move on next bellow cell and date should increases by 7 day and date column fill with is assosiate date value (Sunday Monday...)


date day
12/5/2005 Monday
19/5/2005 Monday
26/5/2005 Monday
03/6/2005 Monday
10/6/2005 Monday
17/6/2005 Monday this date should not greater than 31/12/2006

please help me friends

thanks and regards
lakhan

推荐答案

yourDate.ToString("dddd")将日期返回为星期日,星期一...
yourDate.ToString("dddd") returns the day as Sunday, Monday...


使用此代码

如果Asc(e.KeyChar)= 13那么

将dim dt设置为新日期
dt ="26-12-2011"
将a变暗为整数
a = dt.dayofweek()
暗淡的str作为字符串
如果a = 1则
str ="Monday"
elseif a = 2则
str =星期二"
最多
elseif a = 7则
str ="sunday"
如果
结束

msgbox str


如果
use this code

If Asc(e.KeyChar) = 13 Then

dim dt as new Date
dt="26-12-2011"
dim a as integer
a=dt.dayofweek()
dim str as string
if a=1 then
str="Monday"
elseif a=2 then
str="Tuesday"
upto
elseif a=7 then
str="sunday"
end if


msgbox str


end if


你好,
我编写了简单的Windows窗体应用程序,
它由一个包含两个带有两列的DataGridView1控件的窗体组成,
当您在第一列的单元格中输入日期并按Enter时 同一行的第二列的单元格的值变为
根据输入的日期是星期几,
然后焦点将移至下一行第一列中的单元格.
我使用过:
DataGridViewCellEndEdit
当前单元格的编辑模式结束时发生的事件
再按Enter键,或在DataGridView控件中单击其他单元格.

Program.cs的代码

Hello,
I wrote simple Windows form application,
which consists of one Form containing DataGridView1 control with two columns,
when you enter date into cell in the first column and press enter the
value of the cell in the seckond column of the same row becomes the
day of the week based on the entered date,
and the focus moves to cell in the first column of the next row.
I have used the:
DataGridViewCellEndEdit
event that occurs when edit mode for the current cell ends
afther you press enter, or click on other cell in DataGridView control.

Code for Program.cs

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 27.12.2011
 * Time: 14:50
 * 
 */
using System;
using System.Windows.Forms;
namespace Date_and_days
{
	/// <summary>
	/// Class with program entry point.
	/// </summary>
	internal sealed class Program
	{
		/// <summary>
		/// Program entry point.
		/// </summary>
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
		
	}
}



MainForm.cs的代码:



Code for MainForm.cs :

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 27.12.2011
 * Time: 14:50
 * 
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Date_and_days
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
		}
		
		void DataGridView1CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			
			//
			// Event occurs when edit mode for the current cell ends,
			// afther you press enter, or click on other cell i DataGridView control
			//
			
			// 
			// Local variables
			//
			DateTime Date;
			int row = 0;
			int	column = 0;
			bool IsDate = false;
			row = dataGridView1.CurrentCell.RowIndex;
			column = dataGridView1.CurrentCell.ColumnIndex;
			
			//
			// If  edit mode ends for the cell in the first column,Date column.
			//
			if (column == 0)
			{
				//
				// Checks if you have entered Date in first column, Date column.
				//
				// IsDate is true if it is Date entered into Date column,
				// the value of the entered date is set out to Date variable,
				// afther that program write the day of the week from entered date into
				// seckond column Day of the week column.
				//
				// else program sets the value of the seckond column to empty string
				//
				IsDate = DateTime.TryParse(dataGridView1.CurrentCell.Value.ToString(),out Date);
				
				if (IsDate)
				{
					dataGridView1[column+1,row].Value = Date.DayOfWeek.ToString();
				}
				
				else
				{
					dataGridView1[column+1,row].Value = String.Empty;
				}
				
			}
		}
	}
}



MainForm.Designer.cs的代码



Code for MainForm.Designer.cs

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 27.12.2011
 * Time: 14:50
 * 
 */
namespace Date_and_days
{
	partial class MainForm
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		
		/// <summary>
		/// Disposes resources used by the form.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
			this.dataGridView1 = new System.Windows.Forms.DataGridView();
			this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGridView1
			// 
			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
									this.Column1,
									this.Column2});
			this.dataGridView1.Location = new System.Drawing.Point(12, 12);
			this.dataGridView1.Name = "dataGridView1";
			this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.dataGridView1.Size = new System.Drawing.Size(540, 230);
			this.dataGridView1.TabIndex = 0;
			this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGridView1CellEndEdit);
			// 
			// Column1
			// 
			dataGridViewCellStyle1.Format = "D";
			dataGridViewCellStyle1.NullValue = null;
			this.Column1.DefaultCellStyle = dataGridViewCellStyle1;
			this.Column1.HeaderText = "Date";
			this.Column1.MaxInputLength = 100;
			this.Column1.Name = "Column1";
			this.Column1.Width = 200;
			// 
			// Column2
			// 
			this.Column2.HeaderText = "Day of the week";
			this.Column2.Name = "Column2";
			this.Column2.Width = 300;
			// 
			// MainForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(562, 262);
			this.Controls.Add(this.dataGridView1);
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Date and days";
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
			this.ResumeLayout(false);
		}
		private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
		private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
		public System.Windows.Forms.DataGridView dataGridView1;
	}
}



祝一切顺利,
PerićŽeljko



All the best,
Perić Željko


这篇关于如何在用户按Enter键时转换星期几中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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