尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
c#——《c#语言程序设计》实验报告——windows桌面编程
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
熟悉使用wpf进行界面编程的基本过程;掌握wpf布局、控件、事件的使用。
使用wpf技术,进行合理布局,设计一个窗体应用程序,完成一些常用度量单位的转换,基本模板如下图:
对于本实验,属于一个小型的界面,主要设计过程包括:
(1)新建wpf工程。
(2)选择内容布局控件。以grid控件为主,在其中内嵌其它布局控件。并为布局控件设置一些重要的属性,如行数与列数,平均分配还是比例分配等。操作方式,主要通过拖放控件,再辅以少量xaml编辑。
(3)选择各类控件,为它们安排好位置,设置它们的各类属性,如颜色、大小、靠左还是靠右、margin、padding等。首先拖放控件,然后在属性窗口中编辑。
(4)添加事件处理方法。通过事件标签页或双击控件添加。
另外要求:
在原始数据和换算结果后加两个textblock,绑定到原始单位和换算单位。当在textbox输入内容时,换算结果内容清空。如果原始数据格式有误,要提示用户。可在颜色、布局、功能等方面按自己的想法进行完善。 其中:
(1)中间的button用下面的方式面出来:
(2)为在后台代码中访问控件,需要给它命名,如
(3)响应combobox的selectionchanged事件,获取到选定内容的方法是:
combobox cb = sender as combobox;comboboxitem item = cb.selecteditem as comboboxitem;string selected = item.content.tostring(); 然后通过switch语句分别处理长度、重量等选项。
对于长度等单位,可构建如下字典对象辅助转换:
dictionary
length = new dictionary{ {"m(米)", 1 }, {"cm(厘米)", 0.01 }, {"mm(毫米)", 0.001 }};
但温度不行,需自行转换。
ui设计
长度质量温度
逻辑设计
using system;
using system.collections.generic;
using system.componentmodel;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows;
using system.windows.controls;
using system.windows.data;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;namespace homework10
{/// /// mainwindow.xaml 的交互逻辑/// public partial class mainwindow : window{dictionary length = new dictionary{ {"m(米)", 1 }, {"cm(厘米)", 0.01 }, {"mm(毫米)", 0.001 }};dictionary mass = new dictionary{ {"k(克)", 1 }, {"jin(斤)", 500 }, {"kg(千克)", 1000 }};list items = new list() { "摄氏度", "华氏度" };public mainwindow(){initializecomponent();}private void grid_loaded(object sender, routedeventargs e){}private void window_loaded(object sender, routedeventargs e){lstsource.itemssource = length.keys;tndsource.itemssource = length.keys;combotypes.selectedindex = 0;}private void lstsource_selectionchanged(object sender, selectionchangedeventargs e){listbox listbox1 = sender as listbox;string txt = listbox1.selecteditem as string;lsttext.text = txt;}private void tndsource_selectionchanged(object sender, selectionchangedeventargs e){listbox listbox2 = sender as listbox;string txt = listbox2.selecteditem as string;tndtext.text = txt;}private void combotypes_selectionchanged(object sender, selectionchangedeventargs e){combobox cb = sender as combobox;comboboxitem item = cb.selecteditem as comboboxitem;string selected = item.content.tostring();if (selected == "长度") {lstsource.itemssource = length.keys;tndsource.itemssource = length.keys;}else if (selected == "质量"){lstsource.itemssource = mass.keys;tndsource.itemssource = mass.keys;}else if (selected == "温度"){lstsource.itemssource = items;tndsource.itemssource = items;}}private void calbtn_click(object sender, routedeventargs e){}private void cal_text(object sender, textchangedeventargs e){if (lstsource == null || tndsource == null)return;if(errtext!=null)errtext.text = "";if (lstvaltext.text == "" || lstsource.selectedindex == -1 || tndsource.selectedindex == -1) {tndvaltext.text = "0";return;}try{tndvaltext.text = "0";double a = double.parse(lstvaltext.text);if (combotypes.selectedindex == 0){tndvaltext.text = (a * length[lsttext.text] / length[tndtext.text]).tostring();}else if (combotypes.selectedindex == 1){tndvaltext.text = (a * mass[lsttext.text] / mass[tndtext.text]).tostring();}else if (combotypes.selectedindex == 2){if (lstsource.selectedindex == 0){tndvaltext.text = (a * 1.8 32).tostring();}else{tndvaltext.text = ((a - 32) / 1.8).tostring();}}}catch (formatexception fe){errtext.text = "数据格式错误:" fe.message;}catch (keynotfoundexception kfe) {errtext.text = "单位错误:" kfe.message;}}}
}
熟悉使用wpf进行界面编程的基本过程;掌握wpf布局、控件、事件的使用。
https://baike.baidu.com/item/华氏度/9982416?fr=aladdin#2
https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/controls/how-to-get-a-listboxitem
https://blog.csdn.net/weixin_44543135/article/details/98762476
总结
以上是尊龙游戏旗舰厅官网为你收集整理的c#——《c#语言程序设计》实验报告——windows桌面编程的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。