Change text color of a listbox item

  1. Change DrawMode = OwnerDrawFixed

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) {

e.DrawBackground();

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, Brushes.Green, e.Bounds);

}

Anuncios

Kill specific Excel process

1. KillSpecificExcelFileProcess(“example1.xlsx”);

2. private void KillSpecificExcelFileProcess(string excelFileName){

var processes = from p in Process.GetProcessesByName(“EXCEL”)
select p;

foreach (var process in processes){

if (process.MainWindowTitle.Contains(“Microsoft Excel – ” + excelFileName))

process.Kill();

}

}

Open file from browser

Response.ContentType = “application/pdf”;

Response.AddHeader(“content-disposition”, “attachment; filename=” + path);

Response.WriteFile(path);

HttpContext.Current.ApplicationInstance.CompleteRequest();

 

Note. Comment second line to hide download file dialog OR change with this line

Response.AddHeader(“content-disposition”, “inline; filename=” + directory + pathFile);

Get location outside of website folder

webRootPath = Server.MapPath(“~”);

docPath = Path.GetFullPath(Path.Combine(webRootPath, “..\Documents\MyDocument.xml”));

Get selected and set selected in ListBox

// Loop through all items the ListBox.

for (int x = 0; x < listBox1.Items.Count; x++){

// Determine if the item is selected.

if(listBox1.GetSelected(x) == true)

// Deselect all items that are selected.

listBox1.SetSelected(x,false);

else

// Select all items that are not selected.

listBox1.SetSelected(x,true);

}

Establish network connection

//開始網路連線
System.Diagnostics.ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo(@”C:\WINDOWS\system32\net.exe”);
psInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psInfo.Arguments = @”net use \\192.168.0.15\RD$ ams.prog /user:programw”; //輸入相關資訊
System.Diagnostics.Process.Start(psInfo);

Check ToolStripMenuItem’s DropDownItems and exclude ToolStripSeparator

foreach(object obj in item.DropDownItems){

if(obj is ToolStripMenuItems){

ToolStripMenuItem itemDrop = obj as ToolStripMenuItem;

if(reader1.GetString(6) == itemDrop.Name){

itemDrop.Enabled = true;

}

}

else if(obj is ToolStripSeparator){

continue;

}

}