这篇文章来自是mukund pujari的《some cool tips for .net》,本人给大家翻译总结一下,我英语水平也就那么回事,不合适的地方还是请大家提出来。
1. 如何创建一个可改变大小没有标题栏的窗体?(how to create a form with resizing borders and no title bar?)
form1.text = string. empty;
form1.controlbox = false;
2. 如何在.net的windows窗体上启用xp主题集?(how to use xp themes with windows forms using the .net?)
确认你的控件中flatstyle属性已经修改为system,再修改main方法。
static void main()
{
application.enablevisualstyles();
application.doevents();
application. run(new form1());
}
3. 如何为一个窗体设置一个默认按钮?(how to set the default button for a form?)
form1.acceptbutton = button1;
4. 如何为一个窗体设置一个取消按钮?(how to set the cancel button for a form?)
form1.cancelbutton = button1;
5. 如何阻止一个窗体标题显示在任务栏上?(how to prevent a form from being shown in the taskbar?)
设置窗体的showintaskbar属性为false
6. 如何用现有可用字体绑定到combobox控件?(how to fill a combobox with the available fonts?)
combobox1.items.addrange (fontfamily.families);
7. 如何禁止textbox控件默认的邮件菜单?(how to disable the default contextmenu of a textbox?)
textbox1.contextmenu = new contextmenu ();
8. 如何获取“我的文档”等一些系统文件夹路径?(how to get the path for "my documents" and other system folders?)
environment.specialfolder中包含了一些系统文件夹信息
messagebox.show(environment.getfolderpath( environment.specialfolder.personal ));
9. 如何获取应用程序当前执行的路径?(how to get the path to my running exe?)
string apppath = application.executablepath;
10. 如何确定当前运行的系统?(how to determine which operating system is running?)
operatingsystem os = environment.osversion;
messagebox.show(os.version.tostring());
messagebox.show(os.platform.tostring());
11. 如何从完整的路径中获取文件名?(how to get a file's name from the complete path string?)
用system.io.path.getfilename 和 system.io.path.getfilenamewithoutextension(无扩展名)的方法
12. 如何从完整的路径中获取文件扩展名?(how to get a file's extension from the complete path string?)
用system.io.path.getextension方法
13. 如何使没有选择日期的datetimepicker控件为空文本?(how to make the datetimepicker show empty text if no date is selected?)
datetimepicker1.customformat = " ";
datetimepicker1.format = datetimepickerformat.custom;
14. 如何在report viewer中隐藏crystal report的状态栏?(how to hide the status bar of crystal report in report viewer?)
foreach(object obj in this.crystalreportviewer1.controls)
{
if( obj.gettype()== typeof(system.windows.forms.statusbar))
{
statusbar sbar=(statusbar)obj;
sbar.visible=false;
}
}
15. 如何利用crystal report程序来生成pdf版本?(how to generate pdf version of crystal report programmatically?)
reportdocument o_report=new reportdocument();
exportoptions exportopts = new exportoptions();
pdfrtfwordformatoptions pdfformatopts = new pdfrtfwordformatoptions ();
diskfiledestinationoptions diskopts = new diskfiledestinationoptions();
exportopts = o_report.exportoptions;
// 设置pdf格式
exportopts.exportformattype = exportformattype.portabledocformat;
exportopts.formatoptions = pdfformatopts;
// 设置文件选项和导出
exportopts.exportdestinationtype = exportdestinationtype.diskfile;
diskopts.diskfilename = "c://trial.pdf"; //设置pdf导出路径
exportopts.destinationoptions = diskopts;
o_report.export ();
16.通过代码如何输入多行文本?(how to enter multiline text in textbox through code? )
利用textbox控件的lines属性
string [] straddress = {"mukund pujari","global transformation technologies","pune, india"};
textbox1.multiline=true;
textbox1.lines=straddress;
或者
textbox1.text="line 1/r/nline2/r/nline3.";
或者
用"system.environment.newline"来替代换行符号
17. 如何在datagrid中去掉checkbox不确定状态?(how to remove the indeterminate status of checkbox in datagrid?)
datagridtablestyle ts1 = new datagridtablestyle(); //创建table样式
ts1.mappingname = "items"; //分配要应用样式的data table
datagridcolumnstyle boolcol = new datagridboolcolumn(); // 创建checkbox列
boolcol.mappingname = "ch"; //分配数据列名称
boolcol.allownull=false; // 修改allownull属性
18. 如何在用一个数据源datatable绑定两个控件,确保变化不反映在两个控件中?( how to bind two controls to the same datatable without having changes in one control also change the other control?)
我们在一个form中放置一个listbox和一个combobox控件,当数据源是一个datatable而且绑定的valuemember一致的时候我们选择listbox中的一个item时,combobox控件中的相同的item也会被自动选中,我们可以采取建立新的上下文绑定对象来拒绝这样的同步操作
combobox1.datasource = dataset.tables[ "items" ];
combobox1.valuemember = "customerid";
combobox1.displaymember = "customerid";
listbox1.bindingcontext = new bindingcontext(); // 设置新的上下文绑定对象
listbox1.datasource = dataset.tables[ "items" ];
listbox1.valuemember = "customerid";
listbox1.displaymember = "customerid";
19. 一个简单的创建链接字符串的方法。(an easy way to build connection string.)
记事本创建一个new.udl的文件,一个microsoft 数据链接文件
双击打开,熟悉吧
按照向导创建完成一个数据库链接,测试成功
确定后,链接字符串写入这个文件,用记事本打开就看到了
20. 如何打开客户端e-mail程序,windows应用和web应用?( how to open default e-mail client on your system with all parameters entered in it,like outlook express or eudora, from your .net windows or web application? )
web application:
a href="mailto:[email protected],[email protected][email protected]&subject=hello&body=happy new year"
windows application:
引用system.diagnostics.process 命名空间
process process = new process();
process.startinfo.filename = "mailto:[email protected],[email protected]?subject=hello&[email protected]
&[email protected]&body=happy new year" ;
process.start();
21. vb.net和c#有什么不同?( what is difference beween vb.net and c#.net? )
去微软下载一个文档吧,http://download.microsoft.com/download/6/3/5/6354bf47-c597-4029-89e9-2495e7539ab9/vbcsharpwp.exe
22. how to find whether your system has mouse or the number of buttons, whether it has wheel, or whether the mouse buttons are swapped or size of your monitor and many such information?
23. 如何使windows form上的panel或者label控件半透明?(how to make a panel or label semi-transparent on a windows form? )
通过设置控件背景色的alpha值
panel1.backcolor = color.fromargb(65, 204, 212, 230);
注意:在设计时手动输入这些值,不要用颜色选取
24. c#程序的主函数写[sta thread] 属性是什么目的?(what is the purpose of the [sta thread] attribute for the main method of a c# program? )
http://community.csdn.net/expert/topic/4132/4132313.xml?temp=.2285272
25. 如何触发button的click事件?(how to trigger a button click event? )
button1.performclick();
新闻热点
疑难解答