c#中的修改

來源:互聯網
上載者:User

這篇文章來自是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@address1.com,email@address2.com?cc=email@address3.com&Subject=Hello&body=Happy New Year"

Windows Application:
引用System.Diagnostics.Process 命名空間
Process process = new Process();
process.StartInfo.FileName = "mailto:email@address1.com,email@address2.com?subject=Hello&cc=email@address3.com
&bcc=email@address4.com&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(); 


 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。