Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择组中的下一个单选按钮_C#_Radio Button - Fatal编程技术网

C# 选择组中的下一个单选按钮

C# 选择组中的下一个单选按钮,c#,radio-button,C#,Radio Button,在一组单选按钮中,是否有标准的方式以编程方式选择/检查下一个单选按钮?我正在寻找的行为类似于容器中分组的单选按钮的默认箭头键按下事件:当我按下箭头键时,会自动选择并选中下一个(或上一个)单选按钮 每个容器只能选中一个单选按钮。这就是单选按钮的要点。如果要使用复选框,可以使用以下代码: foreach (CheckBox control in Controls.OfType<CheckBox>()) { control.Checked = true; } foreach(Co

在一组单选按钮中,是否有标准的方式以编程方式选择/检查下一个单选按钮?我正在寻找的行为类似于容器中分组的单选按钮的默认箭头键按下事件:当我按下箭头键时,会自动选择并选中下一个(或上一个)单选按钮

每个容器只能选中一个
单选按钮。这就是单选按钮的要点。如果要使用
复选框
,可以使用以下代码:

foreach (CheckBox control in Controls.OfType<CheckBox>())
{
    control.Checked = true;
}
foreach(Controls.OfType()中的复选框控件)
{
control.Checked=true;
}
如果你想依次检查控件,你可以这样做

new Thread(() =>
{
    foreach (CheckBox control in Controls.OfType<CheckBox>())
    {
        control.BeginInvoke((MethodInvoker) (() => control.Checked = true));
        Thread.Sleep(500);
    }
}).Start();
新线程(()=>
{
foreach(Controls.OfType()中的复选框控件)
{
control.BeginInvoke((MethodInvoker)(()=>control.Checked=true));
睡眠(500);
}
}).Start();

再看一次你原来的帖子,我很难理解你到底是什么意思。请您详细说明,以便我更新我的答复好吗?

每个容器只能检查一个
单选按钮。这就是单选按钮的要点。如果要使用
复选框
,可以使用以下代码:

foreach (CheckBox control in Controls.OfType<CheckBox>())
{
    control.Checked = true;
}
foreach(Controls.OfType()中的复选框控件)
{
control.Checked=true;
}
如果你想依次检查控件,你可以这样做

new Thread(() =>
{
    foreach (CheckBox control in Controls.OfType<CheckBox>())
    {
        control.BeginInvoke((MethodInvoker) (() => control.Checked = true));
        Thread.Sleep(500);
    }
}).Start();
新线程(()=>
{
foreach(Controls.OfType()中的复选框控件)
{
control.BeginInvoke((MethodInvoker)(()=>control.Checked=true));
睡眠(500);
}
}).Start();
再看一次你原来的帖子,我很难理解你到底是什么意思。请您详细说明,以便我更新我的答复好吗?

最快的解决方案:(如果您的应用程序有焦点…)

更困难的解决办法:

//为要操作的窗口获取窗口元素的Write方法
//打开记事本实例,窗口标题为:“无标题-记事本”
//您可以使用其他方式访问窗口元素。。。
AutomationElement windowElement=getWindowElement(“无标题-记事本”);
//使用System.Windows.Automation查找WindowElement中的所有单选按钮
//将窗口元素传递到此方法中
//此方法将返回传入的元素中的所有单选按钮
//但是,如果在窗口内有一个窗格,则按钮将包含在内
//在该窗格中,您必须到达该窗格,然后将该窗格传递到findradiobuttons方法中
AutomationElementCollection单选按钮=FindRadioButtons(windowElement);
//可以遍历单选按钮以确定所选的。。。
//然后选择下一个索引等。
//然后以编程方式选择单选按钮
//将选定的radioButton AutomationElement传递到调用Click等的方法中。
单击按钮使用InguiaAutomation(单选按钮[0]);
///  
///查找指定窗口元素中所有已启用的按钮。
///  
///应用程序或对话框窗口。
///满足条件的元素集合。
AutomationElement集合FindRadioButtons(AutomationElement元素WindowElement)
{
if(elementWindowElement==null)
{
抛出新ArgumentException();
}
条件条件=新的条件(
新属性条件(AutomationElement.IsEnabledProperty,true),
新属性条件(AutomationElement.ControlTypeProperty,
控件类型(单选按钮)
);
//查找与指定条件匹配的所有子项。
AutomationElementCollection元素集合=
elementWindowElement.FindAll(TreeScope.Children,conditions);
返回元素集合;
}
私有AutomationElement getWindowElement(字符串windowTitle)
{
AutomationElement根=AutomationElement.RootElement;
AutomationElement结果=空;
foreach(root.FindAll中的AutomationElement窗口(TreeScope.Children,新属性条件(AutomationElement.ControlTypeProperty,ControlType.window)))
{
尝试
{
if(window.Current.Name.Contains(windowTitle)和&window.Current.IsKeyboardFocusable)
{
结果=窗口;
打破
}
}
捕获(例外e)
{
投掷;
}
}
返回结果;
}
专用无效单击按钮使用自动生成(自动元素控制)
{
//测试此样本感兴趣的控制模式。
对象对象模式;
ExpandCollapsePattern expcolPattern;
if(true==control.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,out-objPattern))
{
expcolPattern=对象模式为ExpandCollapsePattern;
if(expcolPattern.Current.ExpandCollapseState!=ExpandCollapseState.LeafNode)
{
按钮expcolButton=新按钮();
//expcolButton.Margin=新厚度(0,0,0,5);
expcolButton.Height=20;
expcolButton.Width=100;
//expcolButton.Content=“ExpandCollapse”;
Tag=expcolPattern;
expcolPattern.Expand();
//SelectListItem(控件,“处理方法”);
//expcolButton.Click+=新建RoutedEventHandler(ExpandCollapse\u Click);
//clientTreeViews[treeviewIndex].Children.Add(expcolButton);
}
}
切换模式切换模式;
if(true==control.TryGetCurrentPattern(TogglePattern.Pattern,out-objPattern))
{
ToggPattern=对象模式为TogglePattern;
按钮togButton=新按钮();
//togButton.Margin=新厚度(0,0,0,5);
按钮,高度=20;
按钮。宽度=100;
//togButton.Content=“切换
    var rads = panel1.Controls.OfType<RadioButton>(); // Get all radioButtons of desired panel

    rads.OrderBy(r => r.Top); // sort them. Please specify what you mean "next" here. I assume that you need next one at the bottom

    // find first checked and set checked for next one
    for (int i = 0; i < rads.Count()-1; i++) 
    {
        if (rads.ElementAt(i).Checked)
        {
            rads.ElementAt(i + 1).Checked = true; 
            return;
        }
    }
public static void CheckNextInGroup(this RadioButton radioButton, bool forward) {
    var parent = radioButton.Parent;
    var radioButtons = parent.Controls.OfType<RadioButton>();  //get all RadioButtons in the relevant container
    var ordered = radioButtons.OrderBy(i => i.TabIndex).ThenBy(i => parent.Controls.GetChildIndex(i)).ToList();  //Sort them like Windows does
    var indexChecked = ordered.IndexOf(radioButtons.Single(i => i.Checked)); //Find the index of the one currently checked
    var indexDesired = (indexChecked + (forward ? 1 : -1)) % ordered.Count;  //This allows you to step forward and loop back to the first RadioButton
    if (indexDesired < 0) indexDesired += ordered.Count;  //Allows you to step backwards to loop to the last RadioButton
    ordered[indexDesired].Checked = true;
}
radioButton1.CheckNextInGroup(true); //Checks the next one in the collection
radioButton1.CheckNextInGroup(false); //Checks the previous one in the collection