




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
5(1)使用菜單及上下文菜單設置窗體顏色(2)使用Timer控件及狀態(tài)欄控件實現在狀態(tài)欄中顯示當前時間及鼠標移動時的坐標位置。-------------------------------5.1實驗代碼-----------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_5{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidredToolStripMenuItem1_Click(objectsender,EventArgse){this.BackColor=Color.Red;}privatevoidredToolStripMenuItem_Click(objectsender,EventArgse){this.BackColor=Color.Red;}privatevoidgreenToolStripMenuItem_Click(objectsender,EventArgse){this.BackColor=Color.Green;}privatevoidyellowToolStripMenuItem_Click(objectsender,EventArgse){this.BackColor=Color.Yellow;}privatevoidblackToolStripMenuItem_Click(objectsender,EventArgse){this.BackColor=Color.Black;}privatevoidpinkToolStripMenuItem_Click(objectsender,EventArgse){this.BackColor=Color.Pink;}privatevoidForm1_Load(objectsender,EventArgse){groupBox1.Left=-1;groupBox1.Width=this.Width+100;groupBox1.Height=this.Height+100;}}}---------------------------------------5.2實驗代碼-------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){timer1.Start();}privatevoidtimer1_Tick(objectsender,EventArgse){l1.Text=DateTime.Now.ToString();}privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse){timer1.Stop();}privatevoidForm1_MouseMove(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){l2.Text="X="+e.X+":"+"Y="+e.Y;}}}}6(1)使用菜單設計打開文件、保存文件對話框(打開及保存對話框點擊確認按鈕后,使用對話框輸出選中的文件名稱);顏色對話框、字體對話框設置文本框字體顏色及字體。(2)定義學生類(姓名、年齡、性別);定義菜單添加學生信息(該功能使用自定義的對話框)、添加的學生信息顯示在主窗體中的列表框中6.1實驗代碼usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_6_1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidopenToolStripMenuItem_Click(objectsender,EventArgse){OpenFileDialogofd=newOpenFileDialog();ofd.InitialDirectory="c:\\";ofd.Filter="(*.txt)|*.txt|(*.*)|*.*";ofd.FilterIndex=0;if(ofd.ShowDialog()==DialogResult.OK){MessageBox.Show(ofd.FileName);}}privatevoidsaveToolStripMenuItem_Click(objectsender,EventArgse){SaveFileDialogsfg=newSaveFileDialog();sfg.Filter="(*.txt)|*.txt|(*.*)|*.*";sfg.FilterIndex=1;sfg.InitialDirectory=".";if(sfg.ShowDialog()==DialogResult.OK){MessageBox.Show(sfg.FileName);}}privatevoidfontToolStripMenuItem_Click(objectsender,EventArgse){FontDialogfd=newFontDialog();fd.AllowVectorFonts=true;fd.AllowVerticalFonts=true;if(fd.ShowDialog()==DialogResult.OK){textBox1.Font=fd.Font;}}privatevoidcolorToolStripMenuItem_Click(objectsender,EventArgse){ColorDialogcd=newColorDialog();cd.AllowFullOpen=true;cd.AnyColor=true;if(cd.ShowDialog()==DialogResult.OK){textBox1.ForeColor=cd.Color;}}}}-------------------------------6.2實驗代碼--------------------------------------------------------Form1.cs------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;namespace_6_2{publicpartialclassForm1:Form{privateArrayListstus=newArrayList();privateArrayListfrms=newArrayList();publicArrayListFrms{get{returnfrms;}set{frms=value;}}publicArrayListStus{get{returnstus;}set{stus=value;}}publicForm1(){InitializeComponent();}publicvoidAddStudent(Students){Stus.Add(s);}privateboolHastheSameFrm(FrmAddfrm){if(Frms.Count>0){returntrue;}returnfalse;}privatevoidnewToolStripMenuItem_Click(objectsender,EventArgse){FrmAddfrm=newFrmAdd(this);//if(!this.HastheSameFrm(frm))//{//Frms.Add(frm);frm.Show();//}//else//{//frm.Close();//frm.Dispose();//}}publicvoidList(){lst.Items.Clear();Students=null;for(inti=0;i<stus.Count;i++){s=(Student)stus[i];lst.Items.Add(s.ToString());}}privatevoidForm1_Load(objectsender,EventArgse){this.MaximizedBounds=Screen.FromControl(this).WorkingArea;}}}-------------------------------------FrmAdd.cs---------------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_6_2{publicpartialclassFrmAdd:Form{privateForm1frm=null;publicFrmAdd(Form1frm){this.frm=frm;InitializeComponent();}privatevoidFrmAdd_Load(objectsender,EventArgse){}privatevoidFrmAdd_FormClosing(objectsender,FormClosingEventArgse){frm.Frms.Remove(this);this.Dispose();}privatevoidbtnCancel_Click(objectsender,EventArgse){this.Close();this.Dispose();}privatevoidbtnAdd_Click(objectsender,EventArgse){if(txtName.Text==""){ep.SetError(txtName,"姓?名?不?能¨1為a空?!ê?");return;}else{ep.Clear();}if(cmbSex.Text==""){ep.SetError(cmbSex,"性?別àe不?能¨1為a空?");return;}else{ep.Clear();}stringname=txtName.Text;stringsex=cmbSex.Text;intage=(int)numAge.Value;Students=newStudent(name,sex,age);frm.AddStudent(s);txtName.Text="";cmbSex.SelectedIndex=-1;numAge.Value=0;frm.List();}}}--------------------------------------學生類--------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespace_6_2{publicclassStudent{privatestringname=null;publicstringName{get{returnname;}set{name=value;}}privatestringsex=null;publicstringSex{get{returnsex;}set{sex=value;}}privateintage=0;publicintAge{get{returnage;}set{age=value;}}publicStudent(stringname,stringsex,intage){=name;this.sex=sex;this.age=age;}publicoverridestringToString(){returnname+":"+age+":"+sex;}}}7.實現如下功能:其中,信息保存與讀取使用字符流形式。--------------------------------------------7.1實驗代碼------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespaceStudentManagement{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidtextBox3_TextChanged(objectsender,EventArgse){}privatevoidForm1_KeyUp(objectsender,KeyEventArgse){}privatevoidtextBox1_TextChanged(objectsender,EventArgse){}privatevoidtextBox1_KeyUp(objectsender,KeyEventArgse){//MessageBox.Show(e.KeyCode.ToString()+e.KeyData.ToString());if(e.KeyCode==Keys.Enter){//MessageBox.Show("dd");comboBox1.Focus();}}privatevoidbtnAdd_Click(objectsender,EventArgse){if((textBox1.Text!="")&&(textBox3.Text!="")&&(comboBox1.Text!="")){ListViewItemlvi=newListViewItem(textBox1.Text);lv.Items.Add(lvi);ListViewItem.ListViewSubItemlvsi=newListViewItem.ListViewSubItem(lvi,comboBox1.Text);lvi.SubItems.Add(lvsi);lvsi=newListViewItem.ListViewSubItem(lvi,textBox3.Text);lvi.SubItems.Add(lvsi);textBox3.Text="";textBox1.Text="";comboBox1.SelectedIndex=-1;}}privatevoidlv_SelectedIndexChanged(objectsender,EventArgse){if(lv.SelectedItems.Count>0){textBox1.Text=lv.Items[lv.SelectedIndices[0]].Text;comboBox1.Text=lv.Items[lv.SelectedIndices[0]].SubItems[1].Text;textBox3.Text=lv.Items[lv.SelectedIndices[0]].SubItems[2].Text;}}privatevoidbutton1_Click(objectsender,EventArgse){if(lv.SelectedItems.Count>0){lv.Items.RemoveAt(lv.SelectedIndices[0]);}}privatevoidbutton2_Click(objectsender,EventArgse){FileStreamfs=null;stringfilename="c:\\student.txt";if(!File.Exists(filename)){fs=newFileStream(filename,FileMode.Create);}else{fs=newFileStream(filename,FileMode.Append);}StreamWritersw=newStreamWriter(newBufferedStream(fs));if(lv.Items.Count>0){foreach(ListViewItemlviinlv.Items){sw.WriteLine(lvi.Text+"|"+lvi.SubItems[1].Text+"|"+lvi.SubItems[2].Text);}}sw.Flush();sw.Close();}privatevoidbutton3_Click(objectsender,EventArgse){FileStreamfs=null;stringfilename=null;SaveFileDialogsfd=newSaveFileDialog();sfd.Filter="(*.txt)|*.txt";if(sfd.ShowDialog()==DialogResult.OK){filename=sfd.FileName;if(File.Exists(filename)){fs=newFileStream(filename,FileMode.Open);StreamReadersr=newStreamReader(newBufferedStream(fs));stringstrRec=null;string[]strFields=null;ListViewItemlvi=null;ListViewItem.ListViewSubItemlvsi=null;while(sr.Peek()>-1){strRec=sr.ReadLine();strFields=strRec.Split('|');lvi=newListViewItem(strFields[0]);lvsi=newListViewItem.ListViewSubItem(lvi,strFields[1]);lvi.SubItems.Add(lvsi);lvsi=newListViewItem.ListViewSubItem(lvi,strFields[2]);lvi.SubItems.Add(lvsi);lv.Items.Add(lvi);}sr.Close();}else{MessageBox.Show("NoFile","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}}}privatevoidbutton6_Click(objectsender,EventArgse){lv.Items.Clear();}}}8.畫圖要求實現畫線的橡皮筋功能,可以設置畫線顏色、寬度,可以將畫圖保存,讀取顯示。---------------------------------------實驗8代碼-------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing.Imaging;namespacePic{publicpartialclassForm1:Form{privateinttype=1;privateColorcolor=Color.Black;privateintwidth=1;privatePenpen=null;privatePointpStart=Point.Empty;privateList<Line>ls=newList<Line>();privatevoidDrawLines(Graphicsg,Pointstart,Pointend){BufferedGraphicsContextcontext=BufferedGraphicsManager.Current;BufferedGraphicsbg=context.Allocate(g,this.pictureBox1.ClientRectangle);bg.Graphics.Clear(Color.White);foreach(Linelineinls){line.Draw(bg.Graphics,pen);}bg.Graphics.DrawLine(pen,start,end);bg.Render();bg.Dispose();bg=null;}publicForm1(){InitializeComponent();pen=newPen(this.color,this.width);}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidpictureBox1_Click(objectsender,EventArgse){}privatevoidlinToolStripMenuItem_Click(objectsender,EventArgse){type=1;}privatevoidblueToolStripMenuItem_Click(objectsender,EventArgse){this.color=Color.Blue;pen.Color=this.color;}privatevoidgreenToolStripMenuItem_Click(objectsender,EventArgse){this.color=Color.Green;pen.Color=this.color;}privatevoidpurpleToolStripMenuItem_Click(objectsender,EventArgse){this.color=Color.Purple;pen.Color=this.color;}privatevoidtoolStripMenuItem2_Click(objectsender,EventArgse){this.width=1;pen.Width=this.width;}privatevoidtoolStripMenuItem3_Click(objectsender,EventArgse){this.width=2;pen.Width=this.width;}privatevoidtoolStripMenuItem4_Click(objectsender,EventArgse){this.width=3;pen.Width=this.width;}privatevoidpictureBox1_MouseDown(objectsender,MouseEventArgse){pStart=e.Location;}privatevoidpictureBox1_MouseMove(objectsender,MouseEventArgse){if(e.Button==Mo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- YY/T 1946-2024腫瘤組織基因突變檢測試劑盒(高通量測序法)
- 農副產品購銷示范合同
- 簽訂的門面租賃合同條款解析
- 建筑項目施工合同管理人員聘用合同
- 炒股合作經典合同案例
- 車輛采購合同細則
- 國際物流服務合同專業(yè)版詳解
- 農村土地流轉授權合同書
- 城市房屋拆遷補償安置標準合同樣本
- 鋼材買賣合同(示范文本GF-0155)
- 骶髂關節(jié)損傷郭倩課件
- 內科學疾病概要-支氣管擴張課件
- 2025陜西渭南光明電力集團限公司招聘39人易考易錯模擬試題(共500題)試卷后附參考答案
- 預防感冒和流感的方法
- 2024年黑龍江職業(yè)學院高職單招語文歷年參考題庫含答案解析
- 2024年南京旅游職業(yè)學院高職單招語文歷年參考題庫含答案解析
- 股指期貨基礎知識介紹培訓課件
- 2024年北京東城社區(qū)工作者招聘筆試真題
- xx學校培訓部工作職責
- T-GXAR 005-2024 制冷機房運行維護規(guī)程
- 開工第一課安全培訓總結精彩
評論
0/150
提交評論