# VS C++.NETframework 筆記 ## 用Visual Studio建立GUI > 參考自: > {%youtube [youtubeid](https://www.youtube.com/watch?v=LF1cI7zeFm4&ab_channel=Simplilearn) %} > https://www.youtube.com/watch?v=LF1cI7zeFm4&ab_channel=Simplilearn 1. 打開Visual Studio建立新專案->搜尋欄打==clr==->然後點 ==CLR空白專案(.Net framework)==->建立 ![](https://i.imgur.com/0fAwD6u.gif) 2. 進去之後工具列==專案(P )==->==加入新項目(W)==->==UI==->==Windows Form==->新增->然後等到出現視窗 ![](https://i.imgur.com/P3YYaA6.gif) 3. 這裡開始比較複雜了,右邊找到==MyForm.cpp==然後點兩下->打開之後全選貼上下面這段Code->把Code裡面==MyProject的部分改成自己的檔案名稱== ![](https://i.imgur.com/kgCpjq1.gif) ``` cpp= #include "MyForm.h" using namespace System; using namespace System::Windows::Forms; [STAThread] void main(array<String^>^ args) { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); MyProject::MyForm form; Application::Run(% form); } ``` 4. 在上方工具列下面那一行找到Debug(正常應該在專案( P)下面)==把Debug改成Release== -> ==專案== -> ==屬性==(應該會是倒數第二個選項) ->==連結器(Linker)==->==系統== -> ==子系統==選==Windows (/SUBSYSTEM:WINDOWS)== -> ==進階(Advance)== -> ==進入點==輸入main -> 確定 ![](https://i.imgur.com/XfHkF75.gif) 5. 全部存檔關掉重開Visual Studio就完成了 ## ListBox 可以在ListBox裡面輸出 目前還沒遇到不能放進Add()裡面的 `listBox1->Items->Add(someting);` 清空ListBox `listBox1->Items->Clear();` ## Button 可以實現用程式碼對button1進行點擊,也就是觸發button1的Click事件 `button1->PerformClick()` ::: spoiler 參考自: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.button?view=windowsdesktop-7.0 (EN) https://learn.microsoft.com/zh-tw/dotnet/api/system.windows.forms.button?view=windowsdesktop-6.0 (中文) ::: ## TextBox 預設輸入的資料型態是String,要轉成int的話如下 `Convert::ToInt64(textBox1->Text);` 檢查TextBox是否為空 `String::IsNullOrEmpty(textBox1->Text)` ## MessageBox 可以跳出錯誤訊息 `MessageBox::Show("Text here");` ### MessageBoxIcon 可以選擇錯誤圖案樣式 `MessageBox::MessageBoxIcon(Icon type);` ::: spoiler 參考自: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox?view=windowsdesktop-6.0 (EN) https://learn.microsoft.com/zh-tw/dotnet/api/system.windows.forms.messagebox?view=windowsdesktop-6.0 (中文) ::: ## DataGridView Column是直向,Row是橫向 設定表格的Rows跟Column為n `dataGridView1->RowCount = n;` `dataGridView1->ColumnCount = n;` #### 對表格填入值 ``` cpp= for (i = 0; i < n; i++) for (j = 0; j < n; j++) dataGridView1->Rows[i]->Cells[j]->Value = square[i][j]; ``` #### 要在Cell裡面Display Emoji 去 ==專案== -> ==屬性( P)== -> ==C/C++== ->==命令列(Commender Line)== -> ==其他選項( D)== -> 輸入`/utf-8` source: https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170 ## TabControl 在一個From裡面可以切換多個頁面 預設為兩個tabpage1和tabpage2 如果想要多加幾個頁面或是調整更多細節的話 > :::success > 在右下角屬性框->行為->TabPages->集合 > ::: ![](https://i.imgur.com/55u4Whe.gif) ## String宣告方式 有別於一般C++(std::string)的宣告 C++/CLI(System::String)的宣告方式如下 `String^ str = "";` ## 資料型態轉換 ### 數字轉字串 int num; 想要把num轉成String的話 `Convert::ToString(num);` ### 字串轉數字 String^ str = "123456"; 想要把str轉成int的話 `Convert::ToInt64(str);` ### 計算時間 ### 隨機數 ### openFileDilog https://learn.microsoft.com/zh-tw/cpp/dotnet/file-handling-and-i-o-cpp-cli?view=msvc-170 ### 讀寫txt檔 https://learn.microsoft.com/zh-tw/cpp/dotnet/file-handling-and-i-o-cpp-cli?view=msvc-170 https://sites.google.com/view/sjdsalg/program/readwritefiles?authuser=0 </br><br> </br><br> </br><br>