1、浏覽器控件:textBox1輸入網址,webBrowser1顯示網頁 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _12_浏覽器控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
Uri uri = new Uri("https://"+text);
webBrowser1.Url = uri;
}
}
}
2、ComboBox控件:選擇年月日,日期顯示含閏年和月份的限制 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _13_ComboBox日期選擇器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//程式加載的時候,将年份添加到下拉框中
//獲得當年年份
int year = DateTime.Now.Year;
//for (int i = 1949; i <=year; i++)
//{
// cboYear.Items.Add(i + "年");
//}
//倒序循環
for (int i = year; i >=1949; i--)
{
cboYear.Items.Add(i + "年");
}
}
//當年份發生改變的時候,加載月份
private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
{
//添加之前應該把之前的items清空
cboMonth.Items.Clear();
for (int i = 1; i <= 12; i++)
{
cboMonth.Items.Add(i + "月");
}
}
//當月份發生變化時加載天
private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
{
cboDay.Items.Clear();
int day = 0;
//string[] strMonth=cboMonth.SelectedItem.ToString().Split(new char[] {'月'});
string strMonth = cboMonth.SelectedItem.ToString().Split(new char[] { '月' },StringSplitOptions.RemoveEmptyEntries)[0];
string strYear = cboYear.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
int year = Convert.ToInt32(strYear);
int month = Convert.ToInt32(strMonth);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 10:
case 12:day = 31;
break;
case 2:
if((year%400==0)||(year%4==0)&&(year%100)!=0)
{
day = 29;
}
else
{
day = 28;
}
break;
default:day = 30;
break;
}
for (int i = 1; i <= day; i++)
{
cboDay.Items.Add(i + "日");
}
}
}
}
3、ListBox控件-輕按兩下圖檔名稱顯示圖檔 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _14_ListBox_輕按兩下看圖檔
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] path = Directory.GetFiles(@"E:\C#教學\圖檔");
for (int i = 0; i < path.Length; i++)
{
string filename = Path.GetFileName(path[i]);
//listBox1.Items.Add(path[i]);這種方式顯示全路徑
//下面方式隻顯示檔案名,不顯示路徑
listBox1.Items.Add(filename);
}
}
string[] path = Directory.GetFiles(@"E:\C#教學\圖檔");
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//
pictureBox1.Image = Image.FromFile(path[listBox1.SelectedIndex]);
}
}
}
4、輕按兩下播放音樂 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _15_輕按兩下播放音樂_ListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//通過泛型存儲音樂檔案的全路徑
List<string> listsongs = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
string[] path =Directory.GetFiles(@"E:\C#教學","*.wav");
for (int i = 0; i < path.Length; i++)
{
string filename = Path.GetFileName(path[i]);
listBox1.Items.Add(filename);
//将音樂檔案的全路徑存到泛型集合中
listsongs.Add(path[i]);
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = listsongs[listBox1.SelectedIndex];
sp.Play();
}
}
}
5、點選“選擇檔案”按鈕,彈出檔案對話框,選擇txt文本,顯示在textBox1中 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _16_打開對話框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//點選按鈕,彈出檔案對話框
OpenFileDialog ofd = new OpenFileDialog();
//顯示出對話框的标題
ofd.Title = "請選擇要打開的文本檔案,這是一個title1";
//設定對話框可以多選
ofd.Multiselect = true;
//設定對話框的初始目錄
ofd.InitialDirectory = @"E:\C#教學";
//打開檔案的類型
ofd.Filter = "文本檔案|*.txt|媒體檔案|*.wav|所有檔案|*.*";
//顯示出該對話框
ofd.ShowDialog();
//獲得選中檔案的路徑
string path = ofd.FileName;
//沒有改if,則取消選擇檔案時抛出異常
if(path=="")
{
return;
}
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];//5M大小
//實際讀取的位元組數
int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
}
}
}
}
6、textBox1輸入文本,點選儲存,彈出儲存對話框,儲存檔案。 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _17_儲存檔案對話框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "請選擇要儲存的路徑,這是一個title!";
sfd.InitialDirectory = @"E:\C#教學";
sfd.Filter = "文本檔案|*.txt|所有檔案|*.*";
sfd.ShowDialog();
string path = sfd.FileName;
if(path=="")
{
return;
}
using(FileStream fsWrite =new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
fsWrite.Write(buffer,0,buffer.Length);
}
MessageBox.Show("儲存成功");
}
}
}
7、字型和顔色對話框-改變textBox中的字型和顔色 C#:04窗體介紹:webBrowser,ComboBox,ListBox,打開文本框,儲存文本框、字型和顔色對話框 namespace _18_字型和顔色對話框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
private void button2_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.ForeColor = cd.Color;
}
}
}