iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
1
Software Development

C#可以做出甚麼?系列 第 20

C#實作-LINE Bot

  • 分享至 

  • xImage
  •  

顯示星期幾範例:
https://ithelp.ithome.com.tw/upload/images/20210410/20119035lDmZj519Ms.png

開始產生變數
https://ithelp.ithome.com.tw/upload/images/20210410/20119035Hn8GA5mjy7.png
強制轉換型態
int iVk = Convert.ToInt32(numericUpDown1.Value);

再來是switch
https://ithelp.ithome.com.tw/upload/images/20210410/20119035IaGkmQTQ8f.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210410
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            numericUpDown1.Maximum = 7;
            numericUpDown1.Minimum = 1;
            Lab_1.Text = Lab_2.Text = Lab_3.Text = "";
        }

        private void NumericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            int iVk = Convert.ToInt32(numericUpDown1.Value);

        switch(iVk)
            {
                case 1:
                    Lab_1.Text = "Monday";Lab_2.Text = "Mon";Lab_3.Text = "月曜日";
                    break;
                case 2:
                    Lab_1.Text = "Tuesday"; Lab_2.Text = "Tue"; Lab_3.Text = "火曜日";
                    break;
                case 3:
                    Lab_1.Text = "Wednesday"; Lab_2.Text = "Wed"; Lab_3.Text = "水曜日";
                    break;

                case 4:
                    Lab_1.Text = "Thursday"; Lab_2.Text = "Thu"; Lab_3.Text = "木曜日";
                    break;
                case 5:
                    Lab_1.Text = "Friday"; Lab_2.Text = "Fri"; Lab_3.Text = "金曜日";
                    break;
                case 6:
                    Lab_1.Text = "Sturday"; Lab_2.Text = "Sat"; Lab_3.Text = "土曜日";
                    break;
                case 7:
                    Lab_1.Text = "Sunday"; Lab_2.Text = "Sun"; Lab_3.Text = "日曜日";
                    break;


            }
        }
    }
}



清單方塊
https://ithelp.ithome.com.tw/upload/images/20210410/20119035OJ7KOMm1Dc.png
選Items會有集合可以輸入
MultColum就可以顯示多欄
selectionmode顯示單選/複選


做課程選取清單
checklistbox的checkon..要是flase才可以作動
https://ithelp.ithome.com.tw/upload/images/20210410/20119035Et2207DGTd.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2021041002
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cklstbx.CheckOnClick = true;
            cklstbx.Items.Add("JAVA");

        }

        
    }
}


新增課程
https://ithelp.ithome.com.tw/upload/images/20210410/20119035MC0BDdcQuR.png

記得不要多點兩下..才不會要刪程式碼
https://ithelp.ithome.com.tw/upload/images/20210410/20119035TvAKXDsN6O.png

同樣資料輸入用"陣列"Array輸入

Length長度

宣告語法:

陣列型態 || 陣列名稱 =new 型態 [陣列長度];

int || iA =new int[5];
指定陣列初始值
int || iA =[59,83,62,91,98];

陣列名稱[索引值]:從0開始等差1

https://ithelp.ithome.com.tw/upload/images/20210411/20119035QZVJwhzzLY.png

變比較少字的code
https://ithelp.ithome.com.tw/upload/images/20210411/20119035XSQ4vDJjGJ.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2021041002
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cklstbx.CheckOnClick = true;
            string[] sElt = { "還是JAVA" };

            for (int i = 0; i < sElt.Length; i++)
            { cklstbx.Items.Add(sElt[i]); }
                

            

        }

        
    }
}



while前測試迴圈(這次要不要執行)
語法:

初始狀態(起始值);
while(迴圈執行條件)
{迴圈程式區段;
狀態變化(值變方式);
}


int i=0;
while(i< sElt.Length)
{ cklstbx.Items.Add(sElt[i++]); }

https://ithelp.ithome.com.tw/upload/images/20210411/20119035j0MJvgaP6d.png

do...while迴圈(下次要不要執行)執行到do會繼續執行
語法:

初始狀態(起始值);
do
{
迴圈程式區段;

狀態變化(值變方式);
}while(執行條件);分號


顯示選了幾科
listbox和checklist box
的selectindexchanged開始用

產生副程式 來計算
https://ithelp.ithome.com.tw/upload/images/20210411/20119035kb97SjbjSt.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2021041002
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cklstbx.CheckOnClick = true;
            string[] sElt = { "還是JAVA" };

            //for (int i = 0; i < sElt.Length; i++)
            //{ cklstbx.Items.Add(sElt[i]); }


            int i = 0;
            while (i < sElt.Length)
            { cklstbx.Items.Add(sElt[i++]); }
            vItmCnt();


        }

        private void Cklstbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            vItmCnt();
        }

        private void vItmCnt()
        {
            int iItmCnt = cklstbx.Items.Count; //取得清單總數
            int iCkItmCnt = cklstbx.CheckedItems.Count; //取得清單總數
            lab_Cnt.Text = iCkItmCnt + "/" + iItmCnt;
        }
    }
}



讓勾選的顯示在下面

do...while迴圈(下次要不要執行)執行到do會繼續執行
語法:

初始狀態(起始值);
do
{
迴圈程式區段;

狀態變化(值變方式);
}while(執行條件);分號

把int iCkItmCnt提到範圍外do..while迴圈才可以使用

https://ithelp.ithome.com.tw/upload/images/20210411/20119035GaP5thvQLP.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2021041002
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int iCkItmCnt;
        private void Form1_Load(object sender, EventArgs e)
        {
            cklstbx.CheckOnClick = true;
            string[] sElt = { "還是JAVA" };

            //for (int i = 0; i < sElt.Length; i++)
            //{ cklstbx.Items.Add(sElt[i]); }


            int i = 0;
            while (i < sElt.Length)
            { cklstbx.Items.Add(sElt[i++]); }
            vItmCnt();


        }

        private void Cklstbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            vItmCnt();
        }

        private void vItmCnt()
        {
            int iItmCnt = cklstbx.Items.Count; //取得清單總數
            iCkItmCnt = cklstbx.CheckedItems.Count; //取得清單總數
            lab_Cnt.Text = iCkItmCnt + "/" + iItmCnt;
        }

        private void Btn_Add_Click(object sender, EventArgs e)
        {
            int i = 0;
            do
            {
                litbx.Items.Add(cklstbx.CheckedItems[i]);
            } while (++i<iCkItmCnt);
        }
    }
}



加入沒有V時才部會出現錯誤

 litbx.Items.Clear();
            int i = 0;
            if(iCkItmCnt>0)
            {
                do
                {
                    litbx.Items.Add(cklstbx.CheckedItems[i]);
                } while (++i < iCkItmCnt);
            }
            
        }

變成

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2021041002
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int iCkItmCnt;
        private void Form1_Load(object sender, EventArgs e)
        {
            cklstbx.CheckOnClick = true;
            string[] sElt = { "還是JAVA" };

            //for (int i = 0; i < sElt.Length; i++)
            //{ cklstbx.Items.Add(sElt[i]); }


            int i = 0;
            while (i < sElt.Length)
            { cklstbx.Items.Add(sElt[i++]); }
            vItmCnt();


        }

        private void Cklstbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            vItmCnt();
        }

        private void vItmCnt()
        {
            int iItmCnt = cklstbx.Items.Count; //取得清單總數
            iCkItmCnt = cklstbx.CheckedItems.Count; //取得清單總數
            lab_Cnt.Text = iCkItmCnt + "/" + iItmCnt;
        }

        private void Btn_Add_Click(object sender, EventArgs e)
        {
            litbx.Items.Clear();
            int i = 0;
            if(iCkItmCnt>0)
            {
                do
                {
                    litbx.Items.Add(cklstbx.CheckedItems[i]);
                } while (++i < iCkItmCnt);
            }
            
        }
    }
}



雖然網路上用LINE Bot實作的資料應該很多~
但是為了讓我自己練習一下
所以~還是排入
還有11天到時補坑應該來得及

預計寫的內容如下:
1.透過簡單的命令列嘗試發送訊息
2.嘗試透過C#程式碼發送文字、貼圖、和圖…
3.如何使用Visual Studio 2019與LineBotSDK套件
4.關於template message
5. Hands-on Lab :如何發送卡片格式的Template Messages
6. .....

DEAR ALL 我們明天見/images/emoticon/emoticon08.gif


上一篇
應該還是實作~
下一篇
C#程式碼會寫嗎?
系列文
C#可以做出甚麼?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言