66.两组RadioButton的使用 C#例子 WPF例子

通过两组按钮,实现一个简单的乘法运算。RadioButton一组三个按钮一次只能点击一个。

通过对名称进行Switch实现不同的赋值

完整代码:

cs 复制代码
using System.Text;
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 两组按钮2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private double firstnum = 0;
        private double secondnum = 0;

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb != null)
            {
                if (rb.GroupName == "FirstGroup")
                {
                    switch (rb.Content.ToString())
                    {
                        case "接口1": firstnum = 12.5; break;
                        case "接口2": firstnum = 13; break;
                        case "接口3": firstnum = 19; break;
                    }
                }
                else if (rb.GroupName == "SecondGroup")
                {
                    switch (rb.Content.ToString())
                    {
                        case "信号1": secondnum = 1; break;
                        case "信号2": secondnum = 2; break;
                        case "信号3": secondnum = 10; break;
                    }
                }
                UpdateResult();
            }
        }

        private void UpdateResult()
        {
            TextBlock1.Text = (firstnum * secondnum).ToString();
        }

    }
}
XML 复制代码
<Window x:Class="两组按钮2.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:两组按钮2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="100 100 200 100">

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="80"/>
        </Grid.ColumnDefinitions>

        <RadioButton GroupName="FirstGroup" Click="RadioButton_Click" Content="接口1" Grid.Column="0" Grid.Row="0"/>
        <RadioButton GroupName="FirstGroup" Click="RadioButton_Click" Content="接口2" Grid.Column="1" Grid.Row="0"/>
        <RadioButton GroupName="FirstGroup" Click="RadioButton_Click" Content="接口3" Grid.Column="2" Grid.Row="0"/>
        <RadioButton GroupName="SecondGroup" Click="RadioButton_Click" Content="信号1" Grid.Column="0" Grid.Row="1"/>
        <RadioButton GroupName="SecondGroup" Click="RadioButton_Click" Content="信号2" Grid.Column="1" Grid.Row="1"/>
        <RadioButton GroupName="SecondGroup" Click="RadioButton_Click" Content="信号3" Grid.Column="2" Grid.Row="1"/>
        <TextBlock x:Name="TextBlock1" Grid.Row="0" Grid.Column="3" Text="输出中"/>

    </Grid>

</Window>
相关推荐
xiaoyaohou1115 分钟前
023、数据增强改进(二):自适应数据增强与AutoAugment策略
开发语言·python
鬼圣16 分钟前
Python 上下文管理器
开发语言·python
星空椰30 分钟前
JavaScript 基础进阶:分支、循环与数组实战总结
开发语言·javascript·ecmascript
雨浓YN30 分钟前
WPF MVVM 模式(无调库)项目创建笔记
笔记·wpf
yong999036 分钟前
IHAOAVOA:天鹰优化算法与非洲秃鹫优化算法的混合算法(Matlab实现)
开发语言·算法·matlab
t***5441 小时前
有哪些常见的架构设计模式在现代C++中应用
开发语言·c++
人间打气筒(Ada)1 小时前
「码动四季·开源同行」python语言:用户交互
开发语言·python·基本数据类型·注释·变量·常量·文件头
kaikaile19952 小时前
C# 文件编码转换工具
开发语言·c#
沐雪轻挽萤2 小时前
10. C++17新特性-保证的拷贝消除 (Guaranteed Copy Elision / RVO)
开发语言·c++
河阿里3 小时前
Java-JWT令牌技术深度指南
java·开发语言