2015年5月29日 星期五
C# ListBox 如何新增ListBox裡面的item項目的背景顏色 或字體顏色
ListBox預設是存放字串, 如果ListBox裡面的item要做到背景色或字型顏色的變化,就 要使用Drawitem事件調適,傳統的方法是不合適的。
//--------產生ListBox
main
{
ListBox SearchListBoxResult = new ListBox();
SearchListBoxResult.Name = "SearchListBoxResult_ListBox";
SearchListBoxResult.Tag = "AriesItemTitle";
SearchListBoxResult.Left = 5;
SearchListBoxResult.Top = 65;
SearchListBoxResult.Width = 200;
SearchListBoxResult.Height = 300;
SearchListBoxResult.Items.Add(new MyListBoxItem(Color.White , "123" ));
SearchListBoxResult.DoubleClick += SearchListBoxResult_Click;
SearchListBoxResult.DrawItem += SearchListBoxResult_DrawItem;
this.tabPage3.Controls.Add(SearchListBoxResult);
SearchListBoxResult.DrawMode = DrawMode.OwnerDrawFixed;//----啟動drawitem事件
}
/// <summary> 製作一個ListBox item項目顏色的增加
/// 製作一個ListBox item項目顏色的增加
/// </summary>
public class MyListBoxItem
{
public MyListBoxItem(Color c, string m)
{
ItemColor = c;//------顏色
Message = m;//------- string 文字
}
public Color ItemColor { get; set; }
public string Message { get; set; }
}
/// <summary>
/// ListBox繪畫底圖的事件功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SearchListBoxResult_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox Temp = new ListBox();
for (int i = tabPage3.Controls.Count - 1; i >= 0; i--)
{
if (this.tabPage3.Controls[i].Name == "SearchListBoxResult_ListBox")
Temp = (ListBox)this.tabPage3.Controls[i];
}
MyListBoxItem item = Temp.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
if (item != null)
{
e.Graphics.FillRectangle(new SolidBrush(item.ItemColor), e.Bounds);
e.Graphics.DrawString( // mDraw the appropriate text in the ListBox
item.Message, // The message linked to the item
Temp.Font, // Take the font from the listbox
new SolidBrush(Color.Black), // Set the color
0, // X pixel coordinate
e.Index * Temp.ItemHeight // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox.
);
}
else
{
// The item isn't a MyListBoxItem, do something about it
}
}
2015年5月6日 星期三
SQLite 刪除資料後,檔案大小不會變小的解決方法 Vacuum 指令,也等於壓縮Sqlite資料
當SQLite資料內容頻繁的做插入、刪除、此時SQLITE檔案並不會變小,這時就可以透過下面的C#語法將資料壓縮。
筆者進行360W筆資料的移除後發現檔案大小依舊為300MB,當執行vacuum後壓縮資料庫為16MB。同時效能也可以提高。
//--插入進資料庫 這裡的資料一定為唯一的
SQLiteConnection sqlite_conn;//----連結建立
SQLiteCommand sqlite_cmd;//--------連結指令建立
sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
sqlite_conn.Open();//--------------打開資料庫
sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令
sqlite_cmd.CommandText = "vacuum; ";
sqlite_cmd.ExecuteNonQuery();//----執行
筆者進行360W筆資料的移除後發現檔案大小依舊為300MB,當執行vacuum後壓縮資料庫為16MB。同時效能也可以提高。
//--插入進資料庫 這裡的資料一定為唯一的
SQLiteConnection sqlite_conn;//----連結建立
SQLiteCommand sqlite_cmd;//--------連結指令建立
sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
sqlite_conn.Open();//--------------打開資料庫
sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令
sqlite_cmd.CommandText = "vacuum; ";
sqlite_cmd.ExecuteNonQuery();//----執行
2015年5月5日 星期二
C# SQLite 在寫入資料時如何加快的方法 - 批次處理SQLite 寫入的指令 DbTransaction
SQLite 大家最知道的缺點就是寫入效率太差,但有一個批次處裡的功能可以稍微加快這方面
效率的問題。
筆者在處理18000筆資料寫入上傳統做法要10分左右 但使用該方法只需要2秒鐘左右
//-以下為批次處理SQLite語法
sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
sqlite_conn.Open();//--------------打開資料庫
sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令
List<string> TempResult = new List<string>;
trans.add("1");//---加入兩筆資料
trans.add("2");
//--批次交易處理方法
DbTransaction trans = sqlite_conn.BeginTransaction();
try
{
for (int i = 0; i < TempResult.Count; i++)//---裡面有兩筆資料
{
sqlite_cmd.CommandText = "INSERT INTO QryStock (SQL) VALUES (" + TempResult[i] +")";
sqlite_cmd.ExecuteNonQuery();//----執行
}
trans.Commit(); // <-------------------批次執行上述的SQL指令
}
catch (Exception ex)
{
trans.Rollback(); // <-------------------如果有發生錯誤會中斷
throw; // <-------------------
}
sqlite_conn.Close();
效率的問題。
筆者在處理18000筆資料寫入上傳統做法要10分左右 但使用該方法只需要2秒鐘左右
//-以下為批次處理SQLite語法
sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
sqlite_conn.Open();//--------------打開資料庫
sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令
List<string> TempResult = new List<string>;
trans.add("1");//---加入兩筆資料
trans.add("2");
//--批次交易處理方法
DbTransaction trans = sqlite_conn.BeginTransaction();
try
{
for (int i = 0; i < TempResult.Count; i++)//---裡面有兩筆資料
{
sqlite_cmd.CommandText = "INSERT INTO QryStock (SQL) VALUES (" + TempResult[i] +")";
sqlite_cmd.ExecuteNonQuery();//----執行
}
trans.Commit(); // <-------------------批次執行上述的SQL指令
}
catch (Exception ex)
{
trans.Rollback(); // <-------------------如果有發生錯誤會中斷
throw; // <-------------------
}
sqlite_conn.Close();
2015年2月10日 星期二
C# 取得音樂檔(例如.mp3)播放長度的方法
using System.IO;
using Shell32;
using Microsoft.Win32;
using System.Runtime.InteropServices;
/// <summary>
/// 取得音樂檔播放長度的方法
/// </summary>
/// <param name="MusicPath"></param>帶入音樂.mp3的位置 例如 C:\\hello.mp3
/// <returns></returns>
public int GetMediaLen2(string MusicPath)
{
string dirName = Path.GetDirectoryName(MusicPath);//---------取得檔案路徑 (不包含檔案)
string SongName = Path.GetFileName(MusicPath);//-取得檔案名稱 (此處為音樂檔案.mp3)
FileInfo fInfo = new FileInfo(MusicPath);//------------取得檔案的相關屬性
ShellClass sh = new ShellClass();//----------------------取得文件屬性 使用microsoft的Com元 //件 Dll的Embed Interop Type(內嵌Interop類型)=false
Folder dir = sh.NameSpace(dirName);//----------------------將檔案路徑轉為Folder的型態
FolderItem item = dir.ParseName(SongName);//---------------將檔案轉為FolderItem的型態
string SongTime = Regex.Match(dir.GetDetailsOf(item, -1), "\\d{2}:\\d{2}:\\d{2}").Value;
//取得string型態的音樂長度
//dir.GetDetailsof(該路徑下的這個檔案 , -1 ) -1 = 所有的資料
//Regex.Match("字串" , 找出指定的字串項目) 時間格式為 00:00:00 所以
// \\d{2}:\\d{2}:\\d{2}
SongTime = SongTime.Replace(':', '0');//----------------將:以0取代
int data = int.Parse(SongTime);//--------------------------再轉為int 即為秒數
return data;
}
在 dir.GetDetailsof 時 第二項參數有以下
-1 所有資料
0 檔案的名稱包含附檔名 (ex: hello.mp3)
1 檔案大小 (ex: 47.4kb)
2 格式 (ex: MP3 格式聲音)
using Shell32;
using Microsoft.Win32;
using System.Runtime.InteropServices;
/// <summary>
/// 取得音樂檔播放長度的方法
/// </summary>
/// <param name="MusicPath"></param>帶入音樂.mp3的位置 例如 C:\\hello.mp3
/// <returns></returns>
public int GetMediaLen2(string MusicPath)
{
string dirName = Path.GetDirectoryName(MusicPath);//---------取得檔案路徑 (不包含檔案)
string SongName = Path.GetFileName(MusicPath);//-取得檔案名稱 (此處為音樂檔案.mp3)
FileInfo fInfo = new FileInfo(MusicPath);//------------取得檔案的相關屬性
ShellClass sh = new ShellClass();//----------------------取得文件屬性 使用microsoft的Com元 //件 Dll的Embed Interop Type(內嵌Interop類型)=false
Folder dir = sh.NameSpace(dirName);//----------------------將檔案路徑轉為Folder的型態
FolderItem item = dir.ParseName(SongName);//---------------將檔案轉為FolderItem的型態
string SongTime = Regex.Match(dir.GetDetailsOf(item, -1), "\\d{2}:\\d{2}:\\d{2}").Value;
//取得string型態的音樂長度
//dir.GetDetailsof(該路徑下的這個檔案 , -1 ) -1 = 所有的資料
//Regex.Match("字串" , 找出指定的字串項目) 時間格式為 00:00:00 所以
// \\d{2}:\\d{2}:\\d{2}
SongTime = SongTime.Replace(':', '0');//----------------將:以0取代
int data = int.Parse(SongTime);//--------------------------再轉為int 即為秒數
return data;
}
在 dir.GetDetailsof 時 第二項參數有以下
-1 所有資料
0 檔案的名稱包含附檔名 (ex: hello.mp3)
1 檔案大小 (ex: 47.4kb)
2 格式 (ex: MP3 格式聲音)
訂閱:
文章 (Atom)