我有一个问题,关于如何检查是否在C#中以编程方式检查了一个复选框我现在正忙于为船员和乘客编写C#中的航天器操作系统,这不是发布它的意思但是我建了它用于测试复杂的资源和设备网络,以及未来的航天器操作系统,但现在不是。
计划是检查是否选中了复选框,以便正确的工作人员获得对语音识别引擎的正确访问权限并获得登录到正确表单的权限!
我是通过编写这段代码来完成的:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
namespace HyperPod_OS
{
public partial class Login_Form : Form
{
// SETS THE INTS FOR THE AUTO TEXTBOX //
private int a = 0;
private int b = 0;
private int c = 0;
private int d = 0;
private int f = 0;
private int g = 0;
private int h = 0;
private int i = 0;
private int j = 0;
private int k = 0;
private int l = 0;
private int m = 0;
// SPEECH ENGINE AND SYNTHESIZER //
SpeechSynthesizer HelpAssistant = new SpeechSynthesizer();
SpeechRecognitionEngine ReconEngine_Programmer = new SpeechRecognitionEngine();
SpeechRecognitionEngine ReconEngine_Data_Specialist = new SpeechRecognitionEngine();
SpeechRecognitionEngine ReconEngine_Communication_Specialist = new SpeechRecognitionEngine();
SpeechRecognitionEngine ReconEngine_System_Manager = new SpeechRecognitionEngine();
SpeechRecognitionEngine ReconEngine_Captain = new SpeechRecognitionEngine();
SpeechRecognitionEngine ReconEngine_Doctor = new SpeechRecognitionEngine();
// LOADS THE FORM2 FOR BLENDING //
Login_Form_Empty loginformempty;
public Login_Form()
{
InitializeComponent();
// LOADS THE GRAMMAR OBJECT FOR ALL CREW CLASSES //
ReconEngine_Programmer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(ReconEngine_Programmer_SpeechRecognized);
LoadGrammar_Programmer();
ReconEngine_Programmer.SetInputToDefaultAudioDevice();
ReconEngine_Data_Specialist.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(ReconEngine_Data_Specialist_SpeechRecognized);
LoadGrammar_Data_Specialist();
ReconEngine_Data_Specialist.SetInputToDefaultAudioDevice();
ReconEngine_Communication_Specialist.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(ReconEngine_Communication_Specialist_SpeechRecognized);
LoadGrammar_Communication_Specialist();
ReconEngine_Communication_Specialist.SetInputToDefaultAudioDevice();
ReconEngine_System_Manager.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(ReconEngine_System_Manager_SpeechRecognized);
LoadGrammar_System_Manager();
ReconEngine_System_Manager.SetInputToDefaultAudioDevice();
ReconEngine_Captain.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(ReconEngine_Captain_SpeechRecognized);
LoadGrammar_Captain();
ReconEngine_Captain.SetInputToDefaultAudioDevice();
ReconEngine_Doctor.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(ReconEngine_Doctor_SpeechRecognized);
LoadGrammar_Doctor();
ReconEngine_Doctor.SetInputToDefaultAudioDevice();
// SETS THE SCREEN TO SEMI-TRANSPARENT //
this.BackColor = Color.FromArgb(0, 190, 255);
this.TransparencyKey = Color.FromArgb(0, 190, 255);
this.StartPosition = FormStartPosition.Manual;
// OPENS FORM4 AND SETS THE POSITION OF IT //
loginformempty = new Login_Form_Empty();
loginformempty.StartPosition = FormStartPosition.Manual;
loginformempty.Location = new Point(200, 200);
loginformempty.Show();
// SETS THE METHOD OBJECTS //
this.LocationChanged += new EventHandler(Login_Form_LocationChanged);
this.SizeChanged += new EventHandler(Login_Form_SizeChanged);
this.FormClosed += new FormClosedEventHandler(Login_Form_FormClosed);
// LOADS THE SPEECH ENGINE ON CHECKBOX //
if (checkBox1.Checked == true)
{
ReconEngine_Programmer.RecognizeAsync(RecognizeMode.Multiple);
}
if (checkBox2.Checked == true)
{
ReconEngine_Data_Specialist.RecognizeAsync(RecognizeMode.Multiple);
}
if (checkBox3.Checked == true)
{
ReconEngine_Communication_Specialist.RecognizeAsync(RecognizeMode.Multiple);
}
if (checkBox4.Checked == true)
{
ReconEngine_System_Manager.RecognizeAsync(RecognizeMode.Multiple);
}
if (checkBox5.Checked == true)
{
ReconEngine_Captain.RecognizeAsync(RecognizeMode.Multiple);
}
if (checkBox6.Checked == true)
{
ReconEngine_Doctor.RecognizeAsync(RecognizeMode.Multiple);
}
}
它不起作用,我不明白为什么。
答案 0 :(得分:0)
您现在正在做的是在创建框之后立即选中框。用户没有机会改变它们。
你可以这样做:
在构造函数中向center
checkBox.CheckedChanged += DoSomething;
让它调用方法
public void DoSomethning(object sender, EventArgs e)
{
//do stuff
}
另一种方法是向表单添加一个按钮,并在changed event of each Checkbox
时调用方法答案 1 :(得分:0)
Checked属性本身就是一个布尔值,所以你可以这样做:
if (checkBox1.Checked)
{
ReconEngine_Programmer.RecognizeAsync(RecognizeMode.Multiple);
}
else
{
//not checked
}
if (checkBox2.Checked)
{
ReconEngine_Data_Specialist.RecognizeAsync(RecognizeMode.Multiple);
}
else
{
//not checked
}
这是一个非常简单的比较本身,它不能失败。如果您在检查值方面遇到问题,可以查看一下如何设置它。
您还可以检查CheckedChanged
,看看管理该事件是否更简单,并在处理程序中执行业务逻辑。
答案 2 :(得分:0)
我做了类似这样的事情,我的黑客攻击方法是从选中的单选按钮中获取文本并将其传递给switch语句:
/**
* Unified way to get text from a selected FlowLayoutPanel.
*/
private string GetTextCheckedButton(FlowLayoutPanel flp)
{
string text = flp.Controls.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked).Text;
return text;
}
如您所见,它从流布局面板中找到所选的单选按钮并返回该按钮。你可以这样做,它返回一个数组/传递的字符串列表。
您还需要在按钮本身上使用Checked事件处理程序,我觉得您可能会丢失它。
答案 3 :(得分:0)
首先,您将复选框放在与初始化程序相同的方法中。
您的代码有效(尽管可以缩短为:if(checkBox1.checked)
)。
如果你想同时检查所有功能,可以在某个地方设置检查功能(并在需要时调用)
或者,如果您想随时检查分配事件处理程序:
checkBox1.CheckedChanged += CheckBox1Changed;
public void CheckBox1Changed(object sender, EventArgs e)
{
if(checkbox1.checked)
{
}
}