2014年1月28日 星期二
[C#] msdn - 迷宮遊戲 (類似電流急急棒?)
Step by Step
學習 撰寫自己的方法 (own function)
學習 滑鼠碰觸事件
學習 播放音效
因為使用者調整視窗大小時,(面板)(Panel)就會沒有用處
所以當表單為固定大小,可以用Panel這個容器
2014年1月27日 星期一
[C#] msdn - 圖片檢視器
Step by Step
學習 基本表單、基本控制項、基本屬性、對話方塊
學習 TableLayoutPanel、FlowLayoutPanel
學習 顯示圖片、清除圖片
TableLayoutPanel是一種容器,可如表格一般整齊地排列其他控制項。
FlowLayoutPanel則依流量方向來排列內容。
皆提供,在執行階段的動態排列。
------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PictureViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void showButton_Click(object sender, EventArgs e)
{
// Show the Open File dialog. If the user clicks OK, load the
// picture that the user chose.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(openFileDialog1.FileName);
}
}
private void clearButton_Click(object sender, EventArgs e)
{
// Clear the picture.
pictureBox1.Image = null;
}
private void backgroundButton_Click(object sender, EventArgs e)
{
// Show the color dialog box. If the user clicks OK, change the
// PictureBox control's background to the color the user chose.
if (colorDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.BackColor = colorDialog1.Color;
}
private void closeButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// If the user selects the Stretch check box,
// change the PictureBox's
// SizeMode property to "Stretch". If the user clears
// the check box, change it to "Normal".
if (checkBox1.Checked)
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
else
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
}
}
2014年1月24日 星期五
[C#] 字串輸出格式 String.Format 整理
聽說常用..
String.Format()函式範例寫法:
str1 = String.Format( "您的名字是:{0},訪問了{1}次" , strName , iVisit );
字串{0}的位置將會被塞入第1個參數的值:strName
字串{1}的位置將會被塞入第2個參數的值:iVisit
以此類推...
標準數值格式
格式
|
說明
|
Format
|
Input
|
Output
|
補充
|
| C | 貨幣 | {0:C} | 1234.567 | NT$1,234.57 | Currency :C預設到小數2位…C1取小數一位,C3取小數三位… |
| D | 十進位 | {0:D} | 1234 | 1234 | Decimal: 只支援整數資料型別(integral types),D後面數字表示指定的位數 |
| E | 科學指數 | {0:E} | 1234 | 1.234000E+003 | Scientific |
| F | 固定 | {0:F} | 1234.4567 | 1234.46 | Fixed-point |
| G | 一般 | {0:G} | 1234.567 | 1234.57 | General |
| N | 數字 | {0:N} | 120000 | 120,000.00 | Number:每三位數用 "," 隔開 |
| P | 百分比 | {0:P} | 0.25 | 25.00% | Percent:輸入數值*100 ; 預設取小數2位,P0可取小數 |
| R | 來回 | {0:R} | 0.25 | 0.25 | Round-trip:只支援Double、Single |
| X | 十六進位 | {0:X} | 123 | 7B | Hexadecimal:只支援整數資料型別(integral types) |
自訂數值格式化:{0:(###) ### – ####} ,1234567890→(123) 456 – 7890 自訂數值格式
格式
|
說明
|
Format
|
Input
|
Output
|
補充
|
| 0 | 零值預留位置 | {0:000.000} | 12.3 | 012.300 | Zero placeholder |
| # | 數字預留位置 | {0:###.###} | 12.3 | 12.3 | Digit placeholder #,,:1234567890→1235 #,,,:1234567890→1 #,##0,,:1234567890→1,235 |
| . | 小數點 | {0:0.0} | 12.3 | 12.3 | Decimal point |
| , | 千位分隔符號 | {0:0,0} | 1200 | 1,200 | Thousand separator and number scaling |
| % | 百分比預留位置 | {0:0%} | 0.25 | 25% | Percentage placeholder |
| e | 科學標記法 | {0:0e+0} | 123 | 1e+2 | Scientific notation |
| \ | 跳脫字元 | {0:00\n0} | 123 | 12 3 | Escape character |
標準DateTime格式
格式
|
說明
|
Format
|
Output
|
補充
|
| dd | 月份日期 | {0:dd} | 11 | |
| ddd | 星期幾的縮寫 | {0:ddd} | 星期日 | Sun |
| dddd | 星期幾的完整名稱 | {0:dddd} | 星期日 | Sunday |
| f, ff… | 秒數 | {0:fff} | 364 | |
| gg,… | 時期或時代 | {0:gg} | 西元 | |
| hh | 小時(12 小時制) | {0:hh} | 02 | |
| HH | 小時(24 小時制) | {0:HH} | 14 | |
| mm | 分鐘 | {0:mm} | 21 | |
| MM | 月份 | {0:MM} | 03 | |
| MMM | 月份的縮寫名稱 | {0:MMM} | 三月 | Mar |
| MMMM | 月份的完整名稱 | {0:MMMM} | 三月 | March |
| ss | 秒數 | {0:ss} | 49 | |
| tt | A.M./P.M | {0:tt} | 下午 | |
| yy | 兩個位數的數字來表示年份 | {0:yy} | 12 | |
| yyy | 三個位數的數字來表示年份 | {0:yyy} | 2012 | |
| yyyy | 四個位數的數字來表示年份 | {0:yyyy} | 2012 | |
| zz | 時差(小時) | {0:zz} | +08 | 系統時區與格林威治標準時間 (GMT) 時差 |
| zzz | 時差(小時&分鐘) | {0:zzz} | +08:00 | 系統時區與格林威治標準時間 (GMT) 時差 (帶正負號) |
| : | 時間分隔符號 | {0:hh:mm:ss} | 02:29:06 | |
| / | 日期分隔符號 | {0:yyyy/MM/dd} | 2012/03/11 |
2014年1月22日 星期三
[物件導向] 多載 觀念
多載(Overloading)
@:行為多載,方法多載(Method/Function Overloading)
function名稱一樣,但傳入參數不一樣(型別不同or數量不同)
ex:
void function(int i) {...}
void function(string s) {...}
void function(int i, string s) {...}
是多型(Polymorphism)的其中一種。
------------------------------------------------
------------------------------------------------
------------------------------------------------
@:運算子多載(Operator Overloading)
對於不同型別,運算子做的運算不一樣
ex:
public static NEW_TYPE operator +(NEW_TYPE n1, NEW_TYPE n2)
1。運算子多載主要使用在,使用者自己定義的型別,為自訂型別定義獨特的運算方式。
此外我們也不能重新定義內建資料型別的運算方式,例如我們不能重新定義整數的加法。
2。某些運算子可以多載,某些運算子不可以多載。
3。運算子多載可以使程式碼簡練。
@:行為多載,方法多載(Method/Function Overloading)
function名稱一樣,但傳入參數不一樣(型別不同or數量不同)
ex:
void function(int i) {...}
void function(string s) {...}
void function(int i, string s) {...}
是多型(Polymorphism)的其中一種。
------------------------------------------------
------------------------------------------------
------------------------------------------------
@:運算子多載(Operator Overloading)
對於不同型別,運算子做的運算不一樣
ex:
public static NEW_TYPE operator +(NEW_TYPE n1, NEW_TYPE n2)
1。運算子多載主要使用在,使用者自己定義的型別,為自訂型別定義獨特的運算方式。
此外我們也不能重新定義內建資料型別的運算方式,例如我們不能重新定義整數的加法。
2。某些運算子可以多載,某些運算子不可以多載。
3。運算子多載可以使程式碼簡練。
2014年1月21日 星期二
[物件導向] 多型 觀念
多型(Polymorphism)
【多型】(多行為),一般指【子類別多型】(Subtype Polymorphism)
子類別,來自同樣父類別的〝行為〞[method],可以有不同響應
ex:
動物 [class] 。。。有。。。 叫() [method]
雞 [class] 。。。會。。。 啼叫() [method]
狗 [class] 。。。會。。。 吠叫() [method]
稱之為多型
【多型】(多行為),一般指【子類別多型】(Subtype Polymorphism)
子類別,來自同樣父類別的〝行為〞[method],可以有不同響應
ex:
動物 [class] 。。。有。。。 叫() [method]
雞 [class] 。。。會。。。 啼叫() [method]
狗 [class] 。。。會。。。 吠叫() [method]
稱之為多型
2014年1月20日 星期一
[物件導向] 物件導向基礎複習 類別~
先不管封裝,皆先用public...
術語
英文 |
中文 | 類似概念 | 舉例 |
| class | 類別 | 類別:請視為→設計圖 或看成→自訂的型別 (下定義、分門別類) |
ex:定義"人 "類別.. |
| property | 屬性 | 名詞 語法似C語言的變數 |
人有: 性別,生日,身高,體重,... |
| method | 方法 | 動詞 也有翻譯成:行為 語法似C語言的function |
人會: 跑,跳,吃,喝,... |
object |
物件 | 屬於該類別的實體物 | 張三,李四,志明,春嬌,... |
新增類別step:
方案總管:專案圖示上點右鍵
[加入]->[新增項目]->[類別](取名)
新增 Student 類別後,則出現 Student.cs 檔案
先來設計、撰寫 Student 類別,撰寫它的屬性與方法
Student.cs 添加以下內容
namespace example
{
class Student
{
// Property 屬性
public int grade; // 幾年級
public string name; // 姓名
// Method 方法
public string Say() // 說:自我介紹
{
return "我叫" + name + ",我是" + grade + "年級";
}
public string Talk(Student s) // 有輸入:Student
{
return grade + "年級的" + name + "對" + s.name + "說早安~";
}
public void Upgrade() // 升高一個年級
{
grade++;
}
}
}
Form1.cs 按鈕按下,測試 method
namespace example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Student s1 = new Student(); // 新增物件
s1.grade = 1; // 設定屬性
s1.name = "張三";
Student s2 = new Student();
s2.grade = 2;
s2.name = "李四";
// 方法測試1
MessageBox.Show(s1.Say());
MessageBox.Show(s1.Talk(s2));
// 方法測試2
MessageBox.Show(s1.Say());
s1.Upgrade();
MessageBox.Show(s1.Say());
}
}
}
新增物件語法:
Student s1 = new Student();
類別 物件 = new 類別();
存取物件的屬性、
使用物件的方法:
在object(物件)後面加小數點,
來存取物件的 property(屬性) 跟 method(方法)。
2014年1月18日 星期六
[C#] 第一個 WinForm 教學
寫第一個 Windows Form 程式
練習 textbox 字串轉換數字 & 檢查
敘述:
在textbox輸入考試分數,按下button看反應
------------------------------------------------
步驟如下:
1。在選單選[檔案(F)]→[新增專案(P)...]→[Windows Form應用程式]
2。輸入專案名稱:[WinFormEX1]後→按[確定]
3。左邊[工具箱]中拉一個[textbox]元件與一個[button]元件到form1上
4。滑鼠[雙擊buton元件],會自動生成以下程式碼於Form1.cs
private void button1_Click(object sender, EventArgs e)
{
在這撰寫按了button後想要執行的程式..
}
5。程式碼參考..
6。按下[F5]執行成果
------------------------------------------------
7。其他額外練習:將按下Enter預設執行的button指定為button1
滑鼠[雙擊Form1表單],
自動生成 private void Form1_Load(object sender, EventArgs e)
寫入下面這行程式碼
練習 textbox 字串轉換數字 & 檢查
敘述:
在textbox輸入考試分數,按下button看反應
------------------------------------------------
步驟如下:
1。在選單選[檔案(F)]→[新增專案(P)...]→[Windows Form應用程式]
2。輸入專案名稱:[WinFormEX1]後→按[確定]
3。左邊[工具箱]中拉一個[textbox]元件與一個[button]元件到form1上
4。滑鼠[雙擊buton元件],會自動生成以下程式碼於Form1.cs
private void button1_Click(object sender, EventArgs e)
{
在這撰寫按了button後想要執行的程式..
}
5。程式碼參考..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinFormEX1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int score;
if (!int.TryParse(textBox1.Text, out score)) //把字串轉換為int,傳值給score
{
//若轉換失敗,會傳 0 給score
textBox1.Text = "0"; // 把畫面也更新為0
}
if (score < 100 && score >= 0)//輸入的數值介於 0~100之間
{
MessageBox.Show("你要被打" + (100 - score) + "下屁股");
}
else if (score < 0 || score > 100)//輸入值 大於100 或 小於 0
{
MessageBox.Show("小小年紀就說謊,看我不打死你才怪");
}
else//只有100分會執行這個條件
{
MessageBox.Show("恭喜你,滿分不用被打屁股");
}
}
}
}
6。按下[F5]執行成果
------------------------------------------------
7。其他額外練習:將按下Enter預設執行的button指定為button1
滑鼠[雙擊Form1表單],
自動生成 private void Form1_Load(object sender, EventArgs e)
寫入下面這行程式碼
this.AcceptButton = button1; // 按Enter預設執行的button指定為button1
2014年1月17日 星期五
[C#] Hello World
[新增專案]->[主控台應用程式]
// 以下自動生成,只要手寫兩行秀字&讀key
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld // 依專案名稱
{
class Program
{
static void Main(string[] args)
{
// 兩行解決
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
2014年1月16日 星期四
syntaxhighlighter - blog程式碼排版
插入語法
brush:.....<-顯示語言
<pre class="brush:csharp">
... 呈現的程式碼,放置於此 ...
</pre>
================================================
在blogger上使用..
找到[編輯Html]按鈕(在範本分頁下)
插入以下code,可在最後</body></html>的前面
------------------------------------------------
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shCore.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushCSharp.js"></script>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/3.0.83/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/3.0.83/styles/shThemeDefault.css" id="shTheme"/>
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
------------------------------------------------
這是使用外部資源連結(官網資源)..
------------------------------------------------
以下是可支援語言..
brush:.....<-顯示語言
<pre class="brush:csharp">
... 呈現的程式碼,放置於此 ...
</pre>
================================================
在blogger上使用..
找到[編輯Html]按鈕(在範本分頁下)
插入以下code,可在最後</body></html>的前面
------------------------------------------------
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shCore.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushCSharp.js"></script>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/3.0.83/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/3.0.83/styles/shThemeDefault.css" id="shTheme"/>
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
------------------------------------------------
這是使用外部資源連結(官網資源)..
------------------------------------------------
以下是可支援語言..
| Brush name | Brush aliases | File name |
|---|---|---|
| Bash/shell | bash, shell | shBrushBash.js |
| C# | c-sharp, csharp | shBrushCSharp.js |
| C++ | cpp, c | shBrushCpp.js |
| CSS | css | shBrushCss.js |
| Delpi | delphi, pas, pascal | shBrushDelphi.js |
| Diff | diff, patch | shBrushDiff.js |
| Groovy | groovy | shBrushGroovy.js |
| JavaScript | js, jscript, javascript | shBrushJScript.js |
| Java | java | shBrushJava.js |
| Perl | perl, pl | shBrushPerl.js |
| PHP | php | shBrushPhp.js |
| Plain Text | plain, text | shBrushPlain.js |
| Python | py, python | shBrushPython.js |
| Ruby | rails, ror, ruby | shBrushRuby.js |
| Scala | scala | shBrushScala.js |
| SQL | sql | shBrushSql.js |
| Visual Basic | vb, vbnet | shBrushVb.js |
| XML | xml, xhtml, xslt, html, xhtml | shBrushXml.js |
訂閱:
意見 (Atom)