Archive for the ‘C#’ Category

ASP.NET MVC 2 学习笔记: view和control之间传值

在asp.net2.0的网页开发模式下,我们一般通过直将访问页面控件, 将值写入到页面, 但在Asp.net MVC模式下,已不能在Controller中再去访问页面控件了,要如何才能给View页面传值呢? 在Controller中有两个字典(ViewData和TempData)来实现View之间的值传递, 在ControllerBase类声明中我们就可以看到: namespace System.Web.Mvc { // 摘要: //     Represents the base class for all MVC controllers. public abstract class ControllerBase : MarshalByRefObject, IController

Read the rest of this entry »

Visual Studio 2010 快捷键(VS2010)

Ctrl+E,D —-格式化全部代码 Ctrl+E,F —-格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT + C显示类视图窗口 F4 显示属性窗口 SHIFT + F4显示项目属性窗口 CTRL + SHIFT + E显示资源视图 F12 转到定义 CTRL + F12转到声明 CTRL + ALT + J对象浏览 CTRL + ALT + F1帮助目录 CTRL + F1 [...]

Read the rest of this entry »

WPF里如何屏蔽数字以外的字符,只允许输入数字(或者小数点)

这段代码是为了解决WPF里如何屏蔽数字以外的字符,只允许输入数字(或者小数点)的问题。 在。cs源文件添加如下事件之后,再把事件附加到要应用的Textbox里上。具体就是点击Textbox,在右下角切换到Events这个tab页面里,选择相应的KeyDown事件和TextChanged事件。 #region Added by Will for TextBox: Numbers input only 屏蔽数字以外的字符 private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { TextBox txt = sender as TextBox; //屏蔽非法按键 if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal) { //if (txt.Text.Contains(“.”) && e.Key == Key.Decimal) if (e.Key == Key.Decimal)

Read the rest of this entry »

如何在WPF调整datagrid控件的各项颜色DataGridColumnHeader,DataGridRowHeader

WPF4.0终于支持datagrid控件了,是个多么重要的进步啊,lol,但是怎么调整各项颜色呢? 下面的代码举例了主要的设置方法。包括对列头,行头的设置。最重要的是如何改变选中行的颜色。这个有些麻烦,不能直接通过属性修改,要先继承系统自身的brush,然后定义颜色,如下列代码所示: <DataGrid.Resources> <SolidColorBrush x:Key=”{x:Static SystemColors.HighlightBrushKey}” Color=”#E146474B”/> </DataGrid.Resources>

Read the rest of this entry »

WPF里如何动态调整控件位置

控件的位置往往是需要动态调整的,那么在WPF里如何设置呢? 我们需要用到控件的Margin属性。 首先的大前提是我们要把控件的对齐方式设置好,比如向上向左对齐或者向下向右对齐,否则相对的另一方向的参数就没有作用了。 然后在C#代码里用 Thickness来调整,这里我就用常量举例了: button1.Margin = new Thickness(10,10,0,0); 就这么简单,希望有帮助。

Read the rest of this entry »

C# ASP.NET 里四舍五入并且去掉小数点后面的零

常常遇到四舍五入的问题,下面提供两种方法: 一、通过Round方法 using System.Math; …… Round(3.045,2)// 返回值:3.05 Round(3.044,2)//返回值:3.04 二、通过字符串格式化 Double PI = 3.1415936; …… String strPI = PI.ToString(“F3″);//返回值:3.142 还有下面这种方法 当:decimal sum = 123456.784M; sum = decimal.Round(sum, 2 , MidpointRounding.AwayFromZero); sum 的值为:123456.78 当:decimal sum = 123456.785M; sum = decimal.Round(sum, 2 , MidpointRounding.AwayFromZero); sum 的值为:123456.79

Read the rest of this entry »

解释C#小例子

public class Stack { Entry top; public void Push(object data) {  ///PUSH压入stack的方法,带一个参数 top = new Entry(top, data);    //这个 是实例化下面的一个类,带 两个参数 } public object Pop() {  //POP是取出的方法 if (top == null) throw new InvalidOperationException();//如果TOP为空就抛出一个异常 object result = top.data;  一个OBJECT对象 top = top.next;   //下一个 return result;    //返回RESULT } class Entry   //类 { public Entry next;   //定义next public object data;  //定义data public Entry(Entry next, object [...]

Read the rest of this entry »

组合算法,数组赋值,全组合问题

全组合问题 组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 代表的数被选中,为0则没选中。 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为 “01”组合,同时将其左边的所有“1”全部移动到数组的最左端。 当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得 到了最后一个组合。 例如求5中选3的组合: 1     1     1     0     0     //1,2,3 1     1     0     1     0     //1,2,4 1     0     1     1     0 [...]

Read the rest of this entry »

c# 只显示日期,不显示时间

C#里的日期格式处理起来很烦.因为我的要求是不转成字符串,仍然保留日期类型。 DateTime   dt   =   Convert.ToDateTime(“2005-11-26″); 或者 DateTime   dt   =   DateTime.Parse(“2005-11-26″); 或者 DateTime   dt   =   Convert.ToDateTime(“2005/11/26″); 或者 DateTime   dt   =   DateTime.Parse(“2005/11/26″); 一定要符合日期格式的字符串才能转换成日期型 如果可以转字符串可以这样: DateTime.Now.ToShortDateString() 用datetimepicker控件可以这样: this.dateTimePicker1.Format = DateTimePickerFormat.Custom; //月和日 this.dateTimePicker1.CustomFormat = “yyyy-MM-dd”; DateTime dtstartdatetime = calStartDate.Value; DateTime dtenddatetime = CalEndDate.Value; string[] str = new [...]

Read the rest of this entry »