C#手册

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一、进制转换//十进制转二进制Console.WriteLine(Convert.ToString(69,2));//十进制转八进制Console.WriteLine(Convert.ToString(69,8));//十进制转十六进制Console.WriteLine(Convert.ToString(69,16));//二进制转十进制Console.WriteLine(Convert.ToInt32(”100111101″,2));//八进制转十进制Console.WriteLine(Convert.ToInt32(”76″,8));//C#16进制转换10进制Console.WriteLine(Convert.ToInt32(”FF”,16));在C#中可以对整型运算对象按位进行逻辑运算。按位进行逻辑运算的意义是:依次取被运算对象的每个位,进行逻辑运算,每个位的逻辑运算结果是结果值的每个位。C#支持的位逻辑运算符如表2所示。运算符号意义运算对象类型运算结果类型对象数实例~位逻辑非运算整型,字符型整型1~a&位逻辑与运算2a&b|位逻辑或运算2a|b^位逻辑异或运算2a^b位左移运算2a4位右移运算2a21、位逻辑非运算位逻辑非运算是单目的,只有一个运算对象。位逻辑非运算按位对运算对象的值进行非运算,即:如果某一位等于0,就将其转变为1;如果某一位等于1,就将其转变为0。比如,对二进制的10010001进行位逻辑非运算,结果等于01101110,用十进制表示就是:~145等于110;对二进制的01010101进行位逻辑非运算,结果等于10101010。用十进制表示就是~85等于176。2、位逻辑与运算位逻辑与运算将两个运算对象按位进行与运算。与运算的规则:1与1等于1,1与0等于0。比如:10010001(二进制)&11110000等于10010000(二进制)。3、位逻辑或运算位逻辑或运算将两个运算对象按位进行或运算。或运算的规则是:1或1等1,1或0等于1,0或0等于0。比如10010001(二进制)|11110000(二进制)等于11110001(二进制)。4、位逻辑异或运算位逻辑异或运算将两个运算对象按位进行异或运算。异或运算的规则是:1异或1等于0,1异或0等于1,0异或0等于0。即:相同得0,相异得1。比如:10010001(二进制)^11110000(二进制)等于01100001(二进制)。5、位左移运算位左移运算将整个数按位左移若干位,左移后空出的部分0。比如:8位的byte型变量bytea=0x65(即二进制的01100101),将其左移3位:a3的结果是0x27(即二进制的00101000)。6、位右移运算位右移运算将整个数按位右移若干位,右移后空出的部分填0。比如:8位的byte型变量Bytea=0x65(既(二进制的01100101))将其右移3位:a3的结果是0x0c(二进制00001100)。在进行位与、或、异或运算时,如果两个运算对象的类型一致,则运算结果的类型就是运算对象的类型。比如对两个int变量a和b做与运算,运算结果的类型还是int型。如果两个运算对象的类型不一致,则C#要对不一致的类型进行类型转换,变成一致的类型,然后进行运算。C#16进制转换10进制类型转换的规则同算术运算中整型量的转换则一致。由位运算符连接整型量而成的表达式就是位运算表达式。C#16进制转换10进制就介绍到这里。二、Button双击实现添加类publicclassDoubleClickButton:System.Windows.Forms.Button{//NotethattheDoubleClickTimepropertygets//themaximumnumberofmillisecondsallowedbetween//mouseclicksforadouble-clicktobevalid.intpreviousClick=SystemInformation.DoubleClickTime;publicneweventEventHandlerDoubleClick;protectedoverridevoidOnClick(EventArgse){intnow=System.Environment.TickCount;//Adouble-clickisdetectedifthethetimeelapsed//sincethelastclickiswithinDoubleClickTime.if(now-previousClick=SystemInformation.DoubleClickTime){//RaisetheDoubleClickevent.if(DoubleClick!=null)DoubleClick(this,EventArgs.Empty);}else{//SetpreviousClicktonowsothat//subsequentdouble-clickscanbedetected.previousClick=now;//AllowthebaseclasstoraisetheregularClickevent.base.OnClick(e);}}//EventhandlingcodefortheDoubleClickevent.protectednewvirtualvoidOnDoubleClick(EventArgse){if(this.DoubleClick!=null)this.DoubleClick(this,e);}}在Designer.CS里将this.button1=newSystem.Windows.Forms.Button();换为this.button1=newDoubleClickButton();并将privateSystem.Windows.Forms.Buttonbutton1;换为privateDoubleClickButtonbutton1;调用格式this.button1.DoubleClick+=newSystem.EventHandler(this.button1_DoubleClick);privatevoidbutton1_DoubleClick(objecto,EventArgse){}三、C#与Excel交互项目-引用-COM-MicrosoftExcelusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingMicrosoft.Office.Core;usingExcel=Microsoft.Office.Interop.Excel;privatevoidreadExcel()//Read{//OpenFileDialogOpenFileDialogopenFileDialog1=newOpenFileDialog();//filterfiledialoghanyabisabukafileexcel(xls)openFileDialog1.Filter=MicrosoftExcellDocument|*.xls;openFileDialog1.Title=SelectDocument;if(openFileDialog1.ShowDialog()==DialogResult.OK){boolconfirm=true;if(confirm){//#regionImportdatakeExcelExcel.Application_excelApp=newExcel.Application();Excel.WorkbookworkBook=_excelApp.Workbooks.Open(openFileDialog1.FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);intnumSheets=workBook.Sheets.Count;//Iteratethroughthesheets.Theyareindexedstartingat1.//for(intsheetNum=1;sheetNumnumSheets+1;sheetNum++){Excel.Worksheetsheet=(Excel.Worksheet)workBook.Sheets[sheetNum];Excel.RangeexcelRange=sheet.UsedRange;object[,]valueArray=(object[,])excelRange.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);if(valueArray!=null){introwCount=valueArray.GetUpperBound(0);intcolCount=valueArray.GetUpperBound(1);for(inti=1;irowCount+1;i++)for(intj=1;jcolCount+1;j++){if(valueArray[i,j]==null)richTextBox1.Text+=i.ToString()++j.ToString()++NULL+\n;elserichTextBox1.Text+=i.ToString()++j.ToString()++valueArray[i,j].ToString()+\n;}}}workBook.Close(false,openFileDialog1.FileName,null);Marshal.ReleaseComObject(workBook);_excelApp.Quit();}}//returngetFromExcel;}privatevoidbutton2_Click(objectsender,EventArgse)//ChangeexistedExcel{Excel.ApplicationexcelApp=newExcel.Application();stringmyPath=@C:\Users\ideagao\Desktop\gao.xls;//excelApp.Workbooks.Open(myPath);//Excel.WorkbookworkBook=excelApp.Workbooks.Add();//宿主项不能以编程方式创建Excel.WorkbookworkBook=excelApp.Workbooks.Open(myPath,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);introwIndex=10;intcolIndex=10;for(rowIndex=3;rowIndex20;rowIndex++)excelApp.Cells[rowIndex,colIndex]=Firs

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功