前言 C#课设挺水的(基本题目都很简单
)。还是简单记录一下,简单地使用了WPF
的Combobox
和GroupBox
的控件。
功能需求 获取系统文化和国家信息(CultureInfo 类):在组合框中选择一个国家,则会显示操作系统中该国家的有关信息。 大致情况如下:
学习demo 看了下题确实挺简单的,不是很想花时间在WPF这玩意上。 所以到网上抄(x学)了个demo –>参考资料链接3 可以通过ComboBox设置一个SelectionChanged的函数来进行变化。有了这个东西,然后修改GroupBox的内容就好了。(所以不看了,等会直接写代码吧 把界面调整一下,然后有了以下MainWindow.xaml文件代码
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 <Window x:Class="bigwork.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:bigwork" mc:Ignorable="d" Title="演示获取系统文化和国家信息" Height="265.898" Width="486.772"> <Grid Margin="0,0,23.4,51.4"> <Grid HorizontalAlignment="Left" Height="230" VerticalAlignment="Top" Width="461" Margin="10,0,-13.6,-51.8"> <GroupBox x:Name="groupBox" Header="" HorizontalAlignment="Left" Margin="14,50,0,0" Width="424" Content="" RenderTransformOrigin="0.5,0.5" Height="158" VerticalAlignment="Top"> <GroupBox.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform Angle="-0.044"/> <TranslateTransform/> </TransformGroup> </GroupBox.RenderTransform> </GroupBox> <Label x:Name="label" Content="选择国家:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="162"/> </Grid> <ComboBox x:Name="comboBox" SelectionChanged="comboBox_SelectionChanged" HorizontalAlignment="Left" Margin="228,10,-0.4,0" VerticalAlignment="Top" Width="221" Height="29"/> </Grid> </Window>
写代码 有了上面的代码,界面就有了,下面写一下用户选择的逻辑即可,这里关联了两个类,RegionInfo
类和CultureInfo
类
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 using System;using System.Collections.Generic;using System.Globalization;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 bigwork { public partial class MainWindow : Window { public struct myRegion { public string name; public string countryName; }; private myRegion[] region; public MainWindow () { InitializeComponent(); region = new myRegion[220 ]; string [] select = getSelect(); comboBox.ItemsSource = select ; int a = 1 ; } private string [] getSelect () { CultureInfo[] custom = CultureInfo.GetCultures(CultureTypes.UserCustomCulture | CultureTypes.SpecificCultures); string [] select = new string [220 ]; int i = 0 ; foreach (var culture in custom) { select [i] = getCountry(culture.Name); region[i].name = culture.Name; region[i].countryName = select [i]; i++; } return select ; } private string getCountry (string name ) { RegionInfo myregion = new RegionInfo(name); return myregion.DisplayName; } private void comboBox_SelectionChanged (object sender, System.Windows.Controls.SelectionChangedEventArgs e ) { string text = comboBox.SelectedItem.ToString(); foreach (var temp in region) { if (text == temp.countryName) { text = temp.name; break ; } } RegionInfo myregion = new RegionInfo(text); this .groupBox.Header = myregion.DisplayName + "国家和文化信息" ; this .groupBox.Content = "国家全名:" + myregion.DisplayName+"\n" ; this .groupBox.Content += "国家英文名:" + myregion.EnglishName+"\n" ; this .groupBox.Content += "货币符号:" + myregion.CurrencySymbol+"\n" ; if (myregion.IsMetric) this .groupBox.Content += "是否使用公制:是\n" ; else this .groupBox.Content += "是否使用公制:否\n" ; this .groupBox.Content += "三字地区码:" + myregion.ThreeLetterISORegionName+"\n" ; CultureInfo MyCultureInfo = new CultureInfo(text); this .groupBox.Content += "语言名称:" + MyCultureInfo.Parent.DisplayName+"\n" ; } } }
然后运行的情况,首先选择国家
然后选中之后就可以看到显示了
参考资料 前两个是微软的说明文档,第三个是一个combobox的一个demo。