首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

WinCE C# 開發備忘錄:型別轉換、下拉框、ListView、List 轉陣列

     編輯:關於C#

型別轉換


// 字串轉整數
int val = Int32.Parse(str); // 或 int.TryParse(str, out val)

// DateTime 格式化字串
string dateStr = dateTime.ToString("yyyy-MM-dd HH:mm:ss");

ComboBox (下拉框) 資料繫結


var list = new ArrayList();
list.Add(new DictionaryEntry(1, "進入"));
list.Add(new DictionaryEntry(2, "離開"));

comboBox1.DataSource = list;
comboBox1.DisplayMember = "Value"; // 顯示文字
comboBox1.ValueMember = "Key";     // 實際值

// 取得選取值
string selected = comboBox1.SelectedValue?.ToString() + "," + comboBox1.Text;

ListView 操作:取得勾選項目與刪除


string ckvalue = "";
// 反向迴圈避免刪除時索引錯亂
for (int i = listView2.Items.Count - 1; i >= 0; i--)
{
    if (listView2.Items[i].Checked)
    {
        ckvalue += listView2.Items[i].SubItems[3].Text + ",";
        listView2.Items.RemoveAt(i);
    }
}

List 轉陣列


var rys = new List();
RyBean[] rysArr = rys.ToArray();

WinCE 開發特別提醒

項目注意事項
.NET 版本WinCE 通常使用 .NET Compact Framework 3.5,API 子集有限
反射/泛型受限,避免複雜反射、動態程式碼生成
記憶體嵌入式裝置記憶體小,注意物件釋放、避免大型集合長駐
檔案系統路徑區分大小寫視儲存裝置而定,建議統一使用 Path.Combine
UI 執行緒同樣需 Invoke/BeginInvoke 跨執行緒更新 UI
資料庫常用 SQL Server Compact (SQLCE),ADO.NET API 類似但功能精簡
部署CAB 檔安裝,需包含 CF 框架執行環境

常用命名空間


using System;
using System.Collections;        // ArrayList, DictionaryEntry
using System.Collections.Generic; // List
using System.Windows.Forms;      // ComboBox, ListView
using System.Data.SqlServerCe;   // SQL CE

WinCE/.NET CF 開發雖然環境受限,但掌握核心 API 差異與資源管理技巧,仍能開發穩定可靠的嵌入式應用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved