1、对象的初始化器:
Class a = new Class() { id = 1, name = "张三" }
2、窗体间传值
①构造函数
②单例函数
//单例模式:确保一个类只有一个实例,并提供一个全局访问点。 public class Singleton { // 定义一个静态变量来保存类的实例 private static Singleton uniqueInstance; // 定义私有构造函数,使外界不能创建该类实例 private Singleton() { } /// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> public static Singleton GetInstance() { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null || uniqueInstance.isDispose) { uniqueInstance = new Singleton(); } return uniqueInstance; } }
③发布-订阅模式(事件传值)
Action<> //不带返回值 Func<>//带返回值
3、委托的使用及内部构造
class Program { delegate int AddDel(int a,int b); /// <summary> /// 委托的内部构造分析 /// </summary> /// <param name="AddInstanceFunc">方法指针</param> /// <param name="p">目标对象</param> /// <param name="del">所有的方法都在委托数组里</param> static void Main(string[] args) { AddDel del = new AddDel(AddStaticFunc); Program p = new Program(); //多播委托的时候,拿到委托返回值是最后一个委托指向的方法的执行结果。 del += p.AddInstanceFunc; int result = del(3,4); // Console.WriteLine(result); Console.ReadKey(); } static int AddStaticFunc(int a, int b) { return a + b; } //实例方法 public int AddInstanceFunc(int a, int b) { return a + b + 1; } }
4、lambda表达式的演变过程
static void Main(string[] args) { #region 泛型委托 //Func<int, int, int> funDemo = new Func<int, int, int>(AddStaticFunc); //int result = funDemo(3,4); //Console.WriteLine(result); #endregion #region 匿名方法 //Func<int, int, int> funDemo = delegate(int a, int b) { return a + b; }; //int result = funDemo(3, 4); //Console.WriteLine(result); #endregion #region lambda //lambda语句 //Func<int, int, int> funDemo = (int a, int b) => { return a + b; }; //lambda表达式 //Func<int, int, int> funDemo = (int a, int b) => a + b; //进一步演变为 //Func<int, int, int> funDemo = (a, b) => a + b; //int result = funDemo(3, 4); //Console.WriteLine(result); #endregion #region 案例 List<string> strlist = new List<string>() { "3","9","32","7"}; //把集合中字符串小于6的打印出来 var temp = strlist.Where(delegate(string a) { return a.CompareTo("6") < 0; }); foreach (var item in temp) { Console.WriteLine(item); } #endregion Console.ReadKey(); }
5、制作图片表格(10列),手动添加行
string[] imgPath; imagePath = "../../Images"; imgPath = Directory.GetFiles(imagePath); int j = 10; for (int i = 0; i < imgPath.Length; i += 10) { List<string> imgPathList = new List<string>(); imgPathList = imgPath.Take(j).Skip(i).ToList(); j += 10; imagePathListGroup.Add(imgPathList); } for (int i = 0; i < imagePathListGroup.Count; i++) { i = this.dgv_Image.Rows.Add(); DataGridViewRow row = this.dgv_Image.Rows[i]; row.Height = 50; for (int j = 0; j < imagePathListGroup[i].Count; j++) { Image img = Image.FromFile(imagePathListGroup[i][j].ToString()); row.Cells[j].Value = img; } }
6、datagrideview动态添加数据
if (grouplist != null) { for (int i = 0; i < grouplist.Count; i++) { int index = this.dgvGroup.Rows.Add(); this.dgvGroup.Rows[index].Cells[0].Value = grouplist[i].GroupID; this.dgvGroup.Rows[index].Cells[1].Value = grouplist[i].GroupName; } }
单元格点击事件中获得单元格的值:
string groupName = dgvGroup.CurrentCell.Value.ToString(); string groupId = dgvGroup.CurrentRow.Cells[0].Value.ToString();
或者
string groupName = dgvGroup.Rows[dgvGroup.SelectedCells[0].RowIndex].Cells["UserListColumn"].Value.ToString();
或者
string groupName = dgvGroup.SelectedCells[0].Value.ToString();