星期三, 8月 24, 2016

[LINQ] 目標字串在文字內容出現次數

練習這篇 MSDN 文章 - How to: Count Occurrences of a Word in a String (LINQ)
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 字串內容
            string text = @"Historically, the world of data and the world of objects" +
                @" have not been well integrated. Programmers work in C# or Visual Basic" +
                @" and also in SQL or XQuery. On the one side are concepts such as classes," +
                @" objects, fields, inheritance, and .NET Framework APIs. On the other side" +
                @" are tables, columns, rows, nodes, and separate languages for dealing with" +
                @" them. Data types often require translation between the two worlds; there are" +
                @" different standard functions. Because the object world has no notion of query, a" +
                @" query can only be represented as a string without compile-time type checking or" +
                @" IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" +
                @" objects in memory is often tedious and error-prone.";

            // 目標字串
            string SearchTarget = "data";

            // 目標字串出現在字串內容多少次
            int counts = text
                .Split(
                    new char[] { '.', '?', '!', ' ', ';', ':', ',' } , 
                    StringSplitOptions.RemoveEmptyEntries)
                .Where(word => word.ToLowerInvariant() == SearchTarget.ToLowerInvariant())
                .Count();

            Console.WriteLine($"{SearchTarget} 字串,在文字內容中出現 {counts} 次");
        }
    }
}
[LINQ] 目標字串在文字內容出現次數

星期日, 8月 21, 2016

[LINQ] 解析 csv 檔案

這篇 MSDN 文章 - 如何:重新排列有分隔符號的檔案中的欄位(LINQ) 是用 LINQ 來做,改成用 Lambda 方式來練習

原資料
Adams,Terry,120
Fakhouri,Fadi,116
Feng,Hanying,117
Garcia,Cesar,114
Garcia,Debra,115
Garcia,Hugo,118
Mortensen,Sven,113
O'Donnell,Claire,112
Omelchenko,Svetlana,111
Tucker,Lance,119
Tucker,Michael,122
Zabokritski,Eugene,121
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = System.IO.File.ReadAllLines(@"D:\spreadsheet1.csv");

            var result = lines
                .Select(line => line.Split(','))
                .Select(s => new
                {
                    Col1 = s[2].ToString(),
                    Col2 = $"{s[1]} {s[0]}"
                })
                .OrderBy(c => c.Col1);

            foreach (var item in result)
            {
                Console.WriteLine($"{item.Col1} - {item.Col2}" );
            }
        }
    }
}

[LINQ] 解析 csv 檔案

星期五, 8月 19, 2016

[C#] DataGridView 和 ReadOnly 屬性

使用 DataGridView 都只針對 Columns 設定 ReadOnly,後來發現原來 Rows 和 Cells 也可以設定 ReadOnly,寫個小範例練習

Project:就都寫在一個 Form 內
Form Layout:橘框內是對 DataGridView ReadOnly 選項設定的功能,FlowlayoutPanel 內的 CheckBox 則是利用 Code 動態產生

C# Code 功能簡易說明:
  • DataGridView 的 Columns、Rows 或 Cells 為 ReadOnly 的話,就把背景顏色設為紅色
  • 點選欄或列的 ComboBox 會勾選 FlowLayoutPanel 內的 CheckBox 並把對應的 DataGridView 欄或列設為 ReadOnly
  • 點選 ComboBox,假如對應的 CheckBox 都已經勾選,就會變成全部取消
  • 勾選 FlowLayoutPanel 內的 CheckBox,會設定對應 DataGridView 的 Cells ReadOnly 屬性
namespace DGVReadOnly
{
    public partial class Form1 : Form
    {
        private Color DefaultBGColor = Control.DefaultBackColor;

        private void Form1_Load(object sender, EventArgs e)
        {
            cboInit();
            cbInit();
            DGVInit();
        }

        private void cboInit()
        {
            for (int i = 1; i <= 5; i++)
            {
                cboColumns.Items.Add(i);
                cboRows.Items.Add(i);
            }
            cboColumns.SelectionChangeCommitted += CboColumns_SelectionChangeCommitted;
            cboRows.SelectionChangeCommitted += CboRows_SelectionChangeCommitted;
        }

        private void cbInit()
        {
            flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
            flowLayoutPanel1.WrapContents = true;
            flowLayoutPanel1.AutoScroll = true;

            int ColumnCount = 0, RowCount = 0;
            for (int i = 1; i <= 25; i++)
            {
                CheckBox cb = new CheckBox();
                cb.AutoSize = true;
                cb.Name = "cb" + i;
                cb.Text = $"第 {ColumnCount + 1} 欄 - 第 {RowCount + 1} 列";
                cb.Tag = $"{ColumnCount}-{RowCount}";
                cb.CheckedChanged += Cb_CheckedChanged; ;

                RowCount++;
                if (i % 5 == 0)
                {
                    flowLayoutPanel1.SetFlowBreak(cb, true);
                    ColumnCount++;
                    RowCount = 0;
                }

                flowLayoutPanel1.Controls.Add(cb);
            }
        }

        private void DGVInit()
        {
            dataGridView1.AllowUserToAddRows = false;
            for (int i = 1; i <= 5; i++)
            {
                DataGridViewTextBoxColumn Col = new DataGridViewTextBoxColumn();
                dataGridView1.Columns.Add(Col);
                DataGridViewRow Row = new DataGridViewRow();
                dataGridView1.Rows.Add(Row);
            }
        }

        private void CboRows_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string Row = cboRows.SelectedItem.ToString();
            int RowIndex = Text2Index(Row);
            IEnumerable<CheckBox> result = flowLayoutPanel1.Controls.OfType<CheckBox>()
                .Where(cb => TagParse2RowIndex(cb.Tag.ToString()) == RowIndex);

            bool readOnly = CheckBox5Judge(result);
            SetCheckBox(result, readOnly);

            dataGridView1.Rows[RowIndex].ReadOnly = readOnly;
            dataGridView1.Rows[RowIndex].DefaultCellStyle.BackColor = ReadOnlyBGColor(readOnly);
        }

        private void CboColumns_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string Col = cboColumns.SelectedItem.ToString();
            int ColIndex = Text2Index(Col);

            IEnumerable<CheckBox> result = flowLayoutPanel1.Controls.OfType<CheckBox>()
                .Where(cb => TagParse2ColIndex(cb.Tag.ToString()) == ColIndex);

            bool readOnly = CheckBox5Judge(result);
            SetCheckBox(result, readOnly);

            dataGridView1.Columns[ColIndex].ReadOnly = readOnly;
            dataGridView1.Columns[ColIndex].DefaultCellStyle.BackColor = ReadOnlyBGColor(readOnly);
        }

        private void SetCheckBox(IEnumerable<CheckBox> Target, bool Checked)
        {
            foreach (CheckBox cb in Target)
                cb.Checked = Checked;
        }

        /// <summary>
        /// 點選 ComboBox ,選擇對應資料時,假如 CheckBox 內都已經選擇,就取消 ReadOnly
        /// </summary>
        /// <param name="Target">待判斷的 CheckBox</param>
        /// <returns>是否為 ReadOnly</returns>
        private bool CheckBox5Judge(IEnumerable<CheckBox> Target)
        {
            bool result = false;

            if (Target.Where(cb => cb.Checked == true).Count() != 5)
                result = true;

            return result;
        }

        /// <summary>
        /// 把 ComboBox 選擇的欄列資料,轉為對應 CheckBox、DataGridView 的 Index
        /// </summary>
        /// <param name="Text">ComBox 選擇資料</param>
        /// <returns>CheckBox、DataGridView 的 Index</returns>
        private int Text2Index(string Text)
        {
            return Convert.ToInt16(Text) - 1;
        }

        private Color ReadOnlyBGColor(bool readOnly)
        { 
            if (readOnly == true)
                return Color.Red;
            else
                return DefaultBGColor;
        }

        private void Cb_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            string Tag = cb.Tag.ToString();
            bool Checked = cb.Checked;

            int
                ColIndex = TagParse2ColIndex(Tag),
                RowIndex = TagParse2RowIndex(Tag);

            dataGridView1.Rows[RowIndex].Cells[ColIndex].ReadOnly = Checked;
            dataGridView1.Rows[RowIndex].Cells[ColIndex].Style.BackColor = ReadOnlyBGColor(Checked);
        } 

        private int TagParse2ColIndex(string tag)
        {
            return Convert.ToInt16(tag.Substring(0, 1));
        }

        private int TagParse2RowIndex(string tag)
        {
            return Convert.ToInt16(tag.Substring(2, 1));
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            SetCheckBox(
                flowLayoutPanel1.Controls.OfType<CheckBox>(),
                false);

            foreach (DataGridViewRow Row in dataGridView1.Rows)
            {
                Row.ReadOnly = false;
                Row.DefaultCellStyle.BackColor = DefaultBGColor;
            }
        }
    }
}

星期三, 8月 17, 2016

[Win10] WinXP 連接 Win10 共享印表機

要從 WinXP 32bit 連接從 Win10 64bit 分享出來的 ZebraTLP 3842 標籤機簡易紀錄

以往連接分享印表機、標籤機設定的步驟,會遇上下面這個訊息
提供認證不足,無法存取這個印表機,你要指定新的認證嗎?
設定步驟如下
  • 選擇 [網路印表機或連接到其他電腦的印表機]
[Win10] WinXP 連接 Win10 共享印表機-1
  • 在連線到這部印表機選項內輸入 Win10 分享出來的標籤機
[Win10] WinXP 連接 Win10 共享印表機-2
  • 輸入 Win10 上事先建立好,列印專用帳號和密碼
[Win10] WinXP 連接 Win10 共享印表機-3
  • 出現認證不足訊息,就被擋在這邊
[Win10] WinXP 連接 Win10 共享印表機-4

星期一, 8月 15, 2016

鍵盤開機

同事跑來詢問,把 PC - BM 6835 的 OS 更新為 Win10 之後,現在只要按一下鍵盤就會開機,了解之後才發現,這是 BIOS 原有功能,而且還只限定在 PS/2 鍵盤、滑鼠才有的開機功能,Orz

把快速啟動選項整個關閉,再嘗試看看,竟然完全沒有效果,還是按鍵盤就會啟動,改找同型號 PC 來試,發現該功能真的關不掉,在 BIOS 內也找不到其他可以關閉選項,QQ

告知老大該情況,他拿 PC - BM 6675 來試也是同樣情況,關不掉

鍵盤開機-2

確認 BM 6835 BIOS 已經是官網最新的 1201

鍵盤開機-1

最後詢問使用者,是否要改成 USB 鍵盤來避免,對方是回答不用,只是告知有這種情況發生而已,順利 pass,^^

處理完後進自己用的 PC - BM 6350 BIOS,沒有看見快速啟動選項,吃飽太撐試看看,竟然也可以鍵盤開機,>.<

鍵盤開機-3

星期五, 8月 05, 2016

[C#] DataGridView 刪除資料

紀錄在 DataGridView 內刪除資料的兩種方法
  • FullRowSelect 狀態下且啟用刪除,按 Delete 鍵
  • 透過 ContextMenuStrip 來進行刪除
Project

[C#] DataGridView 刪除資料-3

Layout

[C#] DataGridView 刪除資料-4