Show toolbar

2013年1月31日 星期四

C# Multi List

標題:C#多維List寫法
C# (Program.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
namespace MultiList
{
class Program
{
static void Main(string[] args)
{
//建立一維List
List<String> titles = new List<String>() {
"ChatBoxy",
"CodeBoxy",
"ToolBoxy"
};
//建立一維List
List<String> websites = new List<String>() {
"http://chatboxy.blogspot.com/",
"http://codeboxy.blogspot.com/",
"http://toolboxy.blogspot.com/"
};
//建立多維List關係
List<List<String>> options = new List<List<String>> {
titles,
websites
};
//新增QQBoxy資料
titles.Add("QQBoxy");
websites.Add("http://qqboxy.blogspot.com/");
//移除ChatBoxy資料
titles.Remove("ToolBoxy");
websites.Remove("http://toolboxy.blogspot.com/");
//移除第0筆資料
titles.RemoveAt(0);
websites.RemoveAt(0);
//印出資料
foreach (List<String> value in options)
{
foreach (var v in value)
{
Console.WriteLine(v);
}
}
}
}
}

結果:


說明:
C#多維List新增移除資料後印出內容。

C# WPF Confirm Options

標題:使用ShowDialog顯示Confirm視窗並回傳RadioButton的Option名稱
XAML (MainWindow.xaml):
1
2
3
4
5
6
7
8
<Window x:Class="WPFConfirmOptions.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="100" Width="200">
<Grid>
<Button Content="Confirm Option" Name="confirmBtn" Click="confirmBtn_Click" />
</Grid>
</Window>

C# (MainWindow.xaml.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Windows;
using System.Collections.Generic; //List
namespace WPFConfirmOptions
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void confirmBtn_Click(object sender, RoutedEventArgs e)
{
var options = new List<Confirm.Option>();
options.Add(new Confirm.Option
{
Name = "Test1",
Description = "Test Description 1."
});
options.Add(new Confirm.Option
{
Name = "Test2",
Description = "Test Description 2."
});
options.Add(new Confirm.Option
{
Name = "Test3",
Description = "Test Description 3."
});
options.Add(new Confirm.Option
{
Name = "Test4",
Description = "Test Description 4."
});
options.Add(new Confirm.Option
{
Name = "Test5",
Description = "Test Description 5."
});
Confirm confirm = new Confirm("Confirm", "Hello World", options);
confirm.ShowDialog();
if (confirm.DialogResult.HasValue && confirm.DialogResult.Value)
{
System.Windows.MessageBox.Show(confirm.CheckedName.ToString());
}
}
}
}

XAML (Confirm.xaml):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Window x:Class="WPFConfirmOptions.Confirm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="Windows" Title="Confirm" Height="230" Width="300">
<Grid>
<TextBlock Height="20" HorizontalAlignment="Center" Margin="0,10,0,0" Name="tbk_StlTypeTitle" Text="Confirm" VerticalAlignment="Top" Width="120" TextAlignment="Center" />
<GroupBox Header="Options" Margin="0,30,0,50" Name="gbx">
<Grid Name="OptionsGrid"></Grid>
</GroupBox>
<Grid Height="50" Width="240" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<Button Content="OK" Height="30" Width="100" Name="btn_OK" VerticalAlignment="Center" HorizontalAlignment="Left" Click="btn_OK_Click" />
<Button Content="Cancel" Height="30" Width="100" Name="btn_Cancel" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btn_Cancel_Click" />
</Grid>
</Grid>
</Window>

C# (Confirm.xaml.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic; //List
using System.Linq;
namespace WPFConfirmOptions
{
/// <summary>
/// Interaction logic for StlTypeConfirm.xaml
/// </summary>
public partial class Confirm : Window
{
public Confirm(string header, string message, List<Option> options)
{
InitializeComponent();
this.Header = header;
this.Message = message;
this.Options = options;
AddRadioButton();
}
public string Header //Set Head Title
{
set
{
Windows.Title = value;
}
}
public string Message //Set Content Description
{
set
{
tbk_StlTypeTitle.Text = value;
}
}
public List<Option> Options;
public String CheckedName = "";
public String ResultName
{
get
{
return CheckedName;
}
}
public class Option
{
public string Name { get; set; }
public string Description { get; set; }
}
public void AddRadioButton()
{
int key = 0;
foreach (Option value in Options) // Create Options
{
RadioButton radioButton = new RadioButton();
radioButton.Name = value.Name;
radioButton.Content = value.Description;
radioButton.Height = 20;
radioButton.Width = 250;
radioButton.Margin = new Thickness(0, (20 + 30 * key), 0, 0);
radioButton.HorizontalAlignment = HorizontalAlignment.Center;
radioButton.VerticalAlignment = VerticalAlignment.Top;
radioButton.IsChecked = false;
this.OptionsGrid.Children.Add(radioButton);
this.RegisterName(radioButton.Name, radioButton);
key++;
}
Windows.Height = 200 + 30 * key;
return;
}
private void btn_OK_Click(object sender, RoutedEventArgs e)
{
foreach (Option value in Options)
{
RadioButton radiobutton = (RadioButton)OptionsGrid.FindName(value.Name);
if (radiobutton.IsChecked == true)
{
CheckedName = radiobutton.Name;
DialogResult = true;
return;
}
}
System.Windows.MessageBox.Show("No Select.");
}
private void btn_Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
return;
}
}
}

結果:

說明:
使用Add Window方式設計WPF Confirm視窗, 主視窗可設定加入多組選項包含名稱及描述, 並在ShowDialog後的Confirm視窗產生標題、說明及選項, 最後按下確認回傳RadioButton的Option名稱。

2013年1月30日 星期三

CSharp Goes to

標題:C#使用goes to(=>)(等於大於)語法範例
C# (Program.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Linq;
namespace GoesTo
{
class Program
{
static void Main(string[] args)
{
string[] words = { "Welcome", "to", "CodeBoxy" };
string shortestWord = words.OrderBy(w => w.Length).FirstOrDefault(); //Find shortest word
int shortestWordLength = words.Min(w => w.Length); //Find the length of the shortest word
Console.WriteLine(shortestWord + "\n" + shortestWordLength);
}
}
}

結果:


說明:
簡單使用goes to(=>)搜尋字串長度最小值。

2013年1月25日 星期五

C# Regex Double Backslash

標題:C#使用雙反斜線表示特殊字元
C# (Main.cs):
1
Regex solidVcg = new Regex("^\\s*solid\\svcg$");

說明:
在C#撰寫特殊字元如\s時,
若只有一個反斜線Visual Studio C#會報錯,
如下圖:

這時候必須使用雙反斜線來表示就能正常執行。

2013年1月24日 星期四

C# WPF Using List

標題:C# WPF使用List功能
C# (MainWindow.xaml.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Windows;
namespace WPFVectorList
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public System.Collections.Generic.List<int> arr = new System.Collections.Generic.List<int>();
private void btn_show_Click(object sender, RoutedEventArgs e)
{
tbk.Text = "";
foreach (int i in arr)
{
tbk.Text += i + "\n";
}
}
private void btn_add_Click(object sender, RoutedEventArgs e)
{
arr.Add(Int32.Parse(tbx.Text));
}
}
}

WPF (MainWindow.xaml):
1
2
3
4
5
6
7
8
9
10
11
<Window x:Class="WPFVectorList.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Margin="12,12,12,65" Name="tbk" />
<Button Content="ADD" Height="40" HorizontalAlignment="Left" Margin="118,259,0,0" Name="btn_add" VerticalAlignment="Top" Width="100" Click="btn_add_Click" />
<Button Content="Show" Height="40" HorizontalAlignment="Left" Margin="224,0,0,12" Name="btn_show" VerticalAlignment="Bottom" Width="100" Click="btn_show_Click" />
<TextBox Height="40" HorizontalAlignment="Left" Margin="12,0,0,12" Name="tbx" VerticalAlignment="Bottom" Width="100" Text="1" />
</Grid>
</Window>

說明:
在C# WPF使用System.Collections.Generic的List實現在陣列Push新值的功能。

MSDN:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx