五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

24天學(xué)完C#第二篇

2023-02-28 15:50 作者:free_bee  | 我要投稿

C#基礎(chǔ):創(chuàng)建一個WPF應(yīng)用來試驗控件

實驗需求:

  • TextBox *1

  • RadioButton *6

  • ListBox *1

  • 滑動條 *2

  • ComboBox *2

  • TextBlock *1

創(chuàng)建一個新的WPF項目


設(shè)置窗口標題


增加行和列


添加TextBox


增加C#代碼來更新TextBlock

雙擊TextBox控件方法

(為TextBlock增加name)


(為changed編寫邏輯)

? ? ? ?private void numberTextBox_TextChanged(object sender, TextChangedEventArgs e)
? ? ? ?{
? ? ? ? ? ?number.Text = numberTextBox.Text;
? ? ? ?}
//這個版本存在bug,支持了非數(shù)值的顯示

增加一個事件處理器只允許輸入數(shù)字

(增加事件)


? ? ? ?private void numberTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
? ? ? ?{
? ? ? ? ? ?e.Handled = !int.TryParse(e.Text, out int result);
? ? ? ?}

添加其余的XAML組件

(單選框按鈕)


(向網(wǎng)格中間左邊添加一個列表框)


(為列表框添加ListBoxItem)


(向中各中間右邊的單元格增加兩個不同的ComboBox)


(添加集合)


<Window x:Class="ExperimentWithCOntrols.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:ExperimentWithCOntrols"
? ? ? ?mc:Ignorable="d"
? ? ? ?Title="ExperimentWIthControls" Height="450" Width="800">
? ?<Grid>
? ? ? ?<Grid.ColumnDefinitions>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ?</Grid.ColumnDefinitions>
? ? ? ?<Grid.RowDefinitions>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="0.5*"/>
? ? ? ?</Grid.RowDefinitions>
? ? ? ?<TextBox HorizontalAlignment="Left" FontSize="18" ?Text="Enter a number" VerticalAlignment="Top" Width="139" Height="26" Foreground="#FF3B2F2F" BorderBrush="White"/>
? ? ? ?<TextBlock x:Name="number" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="#" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" />
? ? ? ?<TextBox x:Name="numberTextBox" ?FontSize ="18" ?TextWrapping="Wrap" Text="0" Margin="10,49,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" TextChanged="numberTextBox_TextChanged" PreviewTextInput="numberTextBox_PreviewTextInput"/>
? ? ? ?<RadioButton Content="1" HorizontalAlignment="Left" Margin="230,127,0,0" VerticalAlignment="Top"/>
? ? ? ?<RadioButton Content="2" HorizontalAlignment="Left" Margin="270,127,0,0" VerticalAlignment="Top"/>
? ? ? ?<RadioButton Content="3" HorizontalAlignment="Left" Margin="311,127,0,0" VerticalAlignment="Top"/>
? ? ? ?<RadioButton Content="4" HorizontalAlignment="Left" Margin="230,150,0,0" VerticalAlignment="Top"/>
? ? ? ?<RadioButton Content="5" HorizontalAlignment="Left" Margin="270,150,0,0" VerticalAlignment="Top"/>
? ? ? ?<RadioButton Content="6" HorizontalAlignment="Left" Margin="311,150,0,0" VerticalAlignment="Top"/>
? ? ? ?<ListBox x:Name="myListBox" Grid.Row="1" Margin="10,10,10,10">
? ? ? ? ? ?<ListBoxItem Content="1"/>
? ? ? ? ? ?<ListBoxItem Content="2"/>
? ? ? ? ? ?<ListBoxItem Content="3"/>
? ? ? ? ? ?<ListBoxItem Content="4"/>
? ? ? ? ? ?<ListBoxItem Content="5"/>
? ? ? ?</ListBox>
? ? ? ?<ComboBox x:Name="readOnlyComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120">
? ? ? ? ? ?<ListBoxItem Content="1"/>
? ? ? ? ? ?<ListBoxItem Content="2"/>
? ? ? ? ? ?<ListBoxItem Content="3"/>
? ? ? ? ? ?<ListBoxItem Content="4"/>
? ? ? ? ? ?<ListBoxItem Content="5"/>
? ? ? ?</ComboBox>
? ? ? ?<ComboBox x:Name="editableComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="270,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" IsEditable="True">
? ? ? ? ? ?<ListBoxItem Content="1"/>
? ? ? ? ? ?<ListBoxItem Content="2"/>
? ? ? ? ? ?<ListBoxItem Content="3"/>
? ? ? ? ? ?<ListBoxItem Content="4"/>
? ? ? ? ? ?<ListBoxItem Content="5"/>
? ? ? ?</ComboBox>

? ?</Grid>
</Window>

為網(wǎng)格最下面一行增加滑動條

(添加小滑動條)


(雙擊小滑動條為其添加事件)

? ? ? private void smallSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
? ? ? ?{
? ? ? ? ? ?number.Text = smallSlider.Value.ToString("0");
? ? ? ?}

(增加一個滑動條來選擇電話號碼)


(雙擊滑動條添加valuechange事件)

? ? ? ?private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
? ? ? ?{
? ? ? ? ? ?Slider t = sender as Slider;//也可以直接指定bigSlider
? ? ? ? ? ?if (t != null)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?number.Text = t.Value.ToString("000-000-00000");
? ? ? ? ? ?}
? ? ? ?}

增加C#代碼讓其余控件開始工作

(RadioButton控制number.text)

? ? ? ?private void RadioButton_Checked(object sender, RoutedEventArgs e)
? ? ? ?{
? ? ? ? ? ?RadioButton r = sender as RadioButton;
? ? ? ? ? ?if (r != null) {
? ? ? ? ? ? ? ?number.Text ?= r.Content.ToString();
? ? ? ? ? ?}
? ? ? ?}

(ListBox更新TextBlock)

? ? ? ?private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (myListBox.SelectedItem is ListBoxItem boxItem) {
? ? ? ? ? ? ? ?number.Text = boxItem.Content.ToString();
? ? ? ? ? ?}
? ? ? ?}

(只讀組合框更新TextBlock)

? ? ? private void readOnlyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (readOnlyComboBox.SelectedItem is ListBoxItem item) {
? ? ? ? ? ? ? ?number.Text = item.Content.ToString();
? ? ? ? ? ?}
? ? ? ?}

(可編輯組合框更新TextBlock)


? ? ? ?private void editableComboBox_TextChanged(object sender, TextChangedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (sender is ComboBox combo) {
? ? ? ? ? ? ? ?number.Text = combo.Text;
? ? ? ? ? ?}
? ? ? ?}

總結(jié)

(XAML代碼)

<Window x:Class="ExperimentWithCOntrols.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:ExperimentWithCOntrols"
? ? ? ?mc:Ignorable="d"
? ? ? ?Title="ExperimentWIthControls" Height="450" Width="800">
? ?<Grid x:Name="bigSlider">
? ? ? ?<Grid.ColumnDefinitions>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ?</Grid.ColumnDefinitions>
? ? ? ?<Grid.RowDefinitions>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="0.5*"/>
? ? ? ?</Grid.RowDefinitions>
? ? ? ?<TextBox HorizontalAlignment="Left" FontSize="18" ?Text="Enter a number" VerticalAlignment="Top" Width="139" Height="26" Foreground="#FF3B2F2F" BorderBrush="White"/>
? ? ? ?<TextBlock x:Name="number" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="#" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" />
? ? ? ?<TextBox x:Name="numberTextBox" ?FontSize ="18" ?TextWrapping="Wrap" Text="0" Margin="10,49,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" TextChanged="numberTextBox_TextChanged" PreviewTextInput="numberTextBox_PreviewTextInput"/>
? ? ? ?<RadioButton Content="1" HorizontalAlignment="Left" Margin="230,127,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
? ? ? ?<RadioButton Content="2" HorizontalAlignment="Left" Margin="270,127,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
? ? ? ?<RadioButton Content="3" HorizontalAlignment="Left" Margin="311,127,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
? ? ? ?<RadioButton Content="4" HorizontalAlignment="Left" Margin="230,150,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
? ? ? ?<RadioButton Content="5" HorizontalAlignment="Left" Margin="270,150,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
? ? ? ?<RadioButton Content="6" HorizontalAlignment="Left" Margin="311,150,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
? ? ? ?<ListBox x:Name="myListBox" Grid.Row="1" Margin="10,10,10,10">
? ? ? ? ? ?<ListBoxItem Content="1" Selected="ListBoxItem_Selected"/>
? ? ? ? ? ?<ListBoxItem Content="2"/>
? ? ? ? ? ?<ListBoxItem Content="3"/>
? ? ? ? ? ?<ListBoxItem Content="4"/>
? ? ? ? ? ?<ListBoxItem Content="5"/>
? ? ? ?</ListBox>
? ? ? ?<ComboBox x:Name="readOnlyComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" SelectionChanged="readOnlyComboBox_SelectionChanged">
? ? ? ? ? ?<ListBoxItem Content="1"/>
? ? ? ? ? ?<ListBoxItem Content="2"/>
? ? ? ? ? ?<ListBoxItem Content="3"/>
? ? ? ? ? ?<ListBoxItem Content="4"/>
? ? ? ? ? ?<ListBoxItem Content="5"/>
? ? ? ?</ComboBox>
? ? ? ?<ComboBox x:Name="editableComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="270,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" IsEditable="True" TextBoxBase.TextChanged="editableComboBox_TextChanged">
? ? ? ? ? ?<ListBoxItem Content="1"/>
? ? ? ? ? ?<ListBoxItem Content="2"/>
? ? ? ? ? ?<ListBoxItem Content="3"/>
? ? ? ? ? ?<ListBoxItem Content="4"/>
? ? ? ? ? ?<ListBoxItem Content="5"/>
? ? ? ?</ComboBox>
? ? ? ?<Slider x:Name="smallSlider" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="120" AutoToolTipPlacement="TopLeft" ?Maximum="5" Minimum="1" ValueChanged="smallSlider_ValueChanged"/>
? ? ? ?<Slider Margin="10,0,142,0" Grid.Row="2" VerticalAlignment="Center" Grid.ColumnSpan="2" Minimum="11111111111" Maximum="99999999999" ValueChanged="Slider_ValueChanged"/>

? ?</Grid>
</Window>
using System;
using System.Collections.Generic;
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 ExperimentWithCOntrols
{
? ?/// <summary>
? ?/// Interaction logic for MainWindow.xaml
? ?/// </summary>
? ?public partial class MainWindow : Window
? ?{
? ? ? ?public MainWindow()
? ? ? ?{
? ? ? ? ? ?InitializeComponent();
? ? ? ?}

? ? ? ?private void numberTextBox_TextChanged(object sender, TextChangedEventArgs e)
? ? ? ?{
? ? ? ? ? ?number.Text = numberTextBox.Text;
? ? ? ?}

? ? ? ?private void numberTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
? ? ? ?{
? ? ? ? ? ?e.Handled = !int.TryParse(e.Text, out int result);
? ? ? ?}

? ? ? ?private void smallSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
? ? ? ?{
? ? ? ? ? ?number.Text = smallSlider.Value.ToString("0");
? ? ? ?}

? ? ? ?private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
? ? ? ?{
? ? ? ? ? ?Slider t = sender as Slider;//也可以直接指定bigSlider
? ? ? ? ? ?if (t != null)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?number.Text = t.Value.ToString("000-000-00000");
? ? ? ? ? ?}
? ? ? ?}

? ? ? ?private void RadioButton_Checked(object sender, RoutedEventArgs e)
? ? ? ?{
? ? ? ? ? ?RadioButton r = sender as RadioButton;
? ? ? ? ? ?if (r != null) {
? ? ? ? ? ? ? ?number.Text ?= r.Content.ToString();
? ? ? ? ? ?}
? ? ? ?}

? ? ? ?private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (myListBox.SelectedItem is ListBoxItem boxItem) {
? ? ? ? ? ? ? ?number.Text = boxItem.Content.ToString();
? ? ? ? ? ?}
? ? ? ?}

? ? ? ?private void readOnlyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (readOnlyComboBox.SelectedItem is ListBoxItem item) {
? ? ? ? ? ? ? ?number.Text = item.Content.ToString();
? ? ? ? ? ?}
? ? ? ?}

? ? ? ?private void editableComboBox_TextChanged(object sender, TextChangedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (sender is ComboBox combo) {
? ? ? ? ? ? ? ?number.Text = combo.Text;
? ? ? ? ? ?}
? ? ? ?}
? ?}
}


24天學(xué)完C#第二篇的評論 (共 條)

分享到微博請遵守國家法律
轮台县| 清河县| 商洛市| 丹东市| 德钦县| 涿鹿县| 阜城县| 黎城县| 忻州市| 兴城市| 灌云县| 扎囊县| 遵义市| 汉川市| 晋江市| 随州市| 聊城市| 曲阜市| 凤山县| 平乐县| 浮梁县| 佛山市| 宜黄县| 卢氏县| 兰西县| 潮安县| 逊克县| 西丰县| 宜川县| 修水县| 海安县| 临邑县| 南华县| 常宁市| 邯郸县| 深圳市| 馆陶县| 赤城县| 五莲县| 囊谦县| 泰州市|