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>
相关推荐
软件开发技术深度爱好者4 分钟前
Python库/包/模块管理工具
开发语言·python
bubiyoushang88811 分钟前
基于MATLAB的自然图像梯度分布重尾特性验证方案
开发语言·matlab
MSTcheng.30 分钟前
【C++STL】priority_queue 模拟实现与仿函数实战
开发语言·c++
小年糕是糕手1 小时前
【C++】C++入门 -- inline、nullptr
linux·开发语言·jvm·数据结构·c++·算法·排序算法
郝学胜-神的一滴1 小时前
Python中一切皆对象:深入理解Python的对象模型
开发语言·python·程序人生·个人开发
csbysj20201 小时前
JSP 隐式对象
开发语言
星期天21 小时前
3.2联合体和枚举enum,还有动态内存malloc,free,calloc,realloc
c语言·开发语言·算法·联合体·动态内存·初学者入门·枚举enum
梵克之泪2 小时前
【号码分离】从Excel表格、文本、word文档混乱文字中提取分离11位手机号出来,基于WPF的实现方案
开发语言·ui·c#
charlie1145141912 小时前
面向C++程序员的JavaScript 语法实战学习4
开发语言·前端·javascript·学习·函数
夫唯不争,故无尤也2 小时前
Python广播机制:张量的影分身术
开发语言·python