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>
相关推荐
hhw19911218 分钟前
c#面试题整理8
c#
天道有情战天下26 分钟前
python flask
开发语言·python·flask
帅弟1501 小时前
Day4 C语言与画面显示练习
c语言·开发语言
qhs15731 小时前
Kotlin字符串操作在Android开发中的应用示例
android·开发语言·kotlin
Stack Overflow?Tan902 小时前
c++实现在同一台主机两个程序实现实时通信
开发语言·c++
MZWeiei2 小时前
Scala:case class(通俗易懂版)
开发语言·后端·scala
闯闯桑2 小时前
scala 中的@BeanProperty
大数据·开发语言·scala
计算机老学长2 小时前
基于Python的商品销量的数据分析及推荐系统
开发语言·python·数据分析
MZWeiei2 小时前
scala有关 类 的知识点与Java的比较
开发语言·scala
飞向星河2 小时前
SV学习笔记——数组、队列
笔记·学习·c#