zoukankan      html  css  js  c++  java
  • C#开发小技巧

    001.判断一个Form是否已关闭并释放,需要从引用和对象两方面来判断,判断引用是否为null:mainfm==null
    判断引用的对象是否已释放:mainfm.IsDisposed
    MainFormmainfm;
    ......
    privatevoidbutton_ok_Click(objectsender,EventArgse)
    {
    if(mainfm==null||mainfm.IsDisposed)
    {
    mainfm=newMainForm();
    mainfm.Show();
    }
    else
    {
    mainfm.Show();
    }
    }

    002.设置Form_MDIMDI主窗口,设置Form_Child为子窗体
    Form_MDI的属性IsMdiContainer设置为True

    Form_Child的属性isMdiContainer设置为false,然后在Form_Childload事件中加载如下代码:

    ChildForm2chldfm2=newChildForm2();

    chldfm2.MdiParent=this;

    chldfm2.Show();

    003.工具栏按钮同时显示图像与文字
    设置DisplayStyle属性(ImageAndText为显示图像与文字)
    设置TextImageRelation属性(ImageAboveText图像在上文字在下)

    004.只运行一个程序实例
    usingSystem.Diagnostics;
    staticclassProgram
    {
    ///<summary>
    ///
    应用程序的主入口点。
    ///</summary>
    [STAThread]
    staticvoidMain()
    {
    if(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length>1)
    {
    return;
    }
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(newForm1());
    }
    }

    005.根据字符串名称,实例化指定的Form.
    在开发中,一般需要在TreeView中打开相应的FormTreeViewForm名称保存在数据库中,双击TreeView需要根据字符串名称,实例化指定的Form.
    格式:Activator.CreateInstance(Type.GetType("命名空间+类名"))
    返回:要访问新创建的实例则必须解包的句柄
    描述:使用命名的程序集和默认构造函数,创建名称已指定的类型的实例。
    Formfm;
    fm=(Form)Activator.CreateInstance(Type.GetType("WindowsApplication2.Form2"));
    fm.Show();

    //第一步:得到类的全名(命名空间+类名)
    //
    第二部:根据全名得到类的类型
    //
    第三步:创建类实例

    反射动态创建对象:
    如果A,B,C,D都与执行代码同一个程序集.则可以这样调用
    System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("
    命名空间.类名",false);
    :
    objecto=System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("MyNameSpace.A",false);

    不同程序集的话.则要装载调用.如下:
    System.Reflection.Assembly.Load("
    程序集名称").CreateInstance("命名空间.类名",false);
    :
    objecto=System.Reflection.Assembly.Load("MyDll").CreateInstance("MyNameSpace.A",false);

    动态创建后再将o强制转换为IChar接口,如下:
    ICharichar=oasIChar;

    如果转换失败则icharnull.

    stringfullName=this.GetType().FullName;
    stringNamespace=this.GetType().Namespace;
    stringname=this.GetType().Name;

    006.TreeView
    当前选中项:TreeView.SelectedNode
    增加顶级节点:TreeView.Nodes.Add("Key","Text")
    增加同级节点:TreeView.SelectedNode.Parent.Nodes.Add("Key","Text")
    增加子节点:TreeView.SelectedNode.Nodes.Add("Key","Text")
    全部展开:TreeView.ExpandAll()
    全部收拢:TreeView.CollapseAll()

    007.c#与数据库的null值转换.
    publicstaticobjectToDBValue(thisobjectvalue)
    {
    returnvalue==null?DBNull.Value:value;
    }

    publicstaticobjectFromDBValue(thisobjectdbValue)
    {
    returndbValue==DBNull.Value?null:dbValue;
    }

    008.当前程序的基目录
    stringcurrentDir=AppDomain.CurrentDomain.BaseDirectory//
    获得当前程序的
    stringfilePath=System.IO.Path.Combine(CurrentDir,"filename.txt");

    009.Log4Net

    010.全局属性值
    Application.Current.Properties["OperatorId"]//
    全局属性值

    011.抛出异常
    thrownewException("error");

    012.关于集合List<T>
    List<int>list=newList<int>();
    list.add(11);
    list.add(22);
    foreach(intiinlist)
    {
    MessageBox.Show(i.ToString());
    }

    013.关于可空类型:
    可空类型(null):引用类型.自定义类、string、大部分类
    不可空类型:值类型.intbooleandecimalDateTime.
    可空值类型:不可空类型后加?.int?i=null;可以把int赋值给int?,不可以把int?赋值给int.
    bool?
    转换为bool需要强制类型转换

    复选框是否选中
    if((bool)checkBox.IsChecked)
    {
    MessageBox.Show("
    选中");
    }
    if(checkBox.IsChecked==true)

     

     

    Cited From:http://blog.csdn.net/gyming/article/details/9209563

  • 相关阅读:
    jQuery Validate 表单验证
    在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突
    移动端手势库hammerJS 2.0.4官方文档翻译
    Linux下查看nginx安装目录
    Mac Mini中添加VNC访问
    CSS3属性选择通配符
    【LeetCode】9 Palindrome Number 回文数判定
    【LeetCode】8. String to Integer (atoi) 字符串转整数
    【LeetCode】7. Reverse Integer 整型数反转
    【LeetCode】6. ZigZag Conversion 锯齿形转换
  • 原文地址:https://www.cnblogs.com/AmatVictorialCuram/p/5047503.html
Copyright © 2011-2022 走看看