简体   繁体   English

如何将单选按钮的值传递给另一种形式?

[英]How to pass the value of a radio button to another form?

I have put Two date Time pickers (dtpStartdate and dtpEndDate), Two Two Combo boxes (cmbStartDate and cmbEndDate) and a radio button(rbStartHalfDay) to a windows form. 我将两个日期时间选择器(dtpStartdate和dtpEndDate),两个两个组合框(cmbStartDate和cmbEndDate)和一个单选按钮(rbStartHalfDay)放到了Windows窗体中。 Then I wanted to pass values of those items when click "Apply" button. 然后,我想在单击“应用”按钮时传递这些项目的值。 I've written codes for that like this. 我已经为此编写了代码。

    private void btnApply_Click(object sender, EventArgs e)
    {
        frmLeaveRequestConfirmation frm = new frmLeaveRequestConfirmation(dtpStartdate.Value, dtpEndDate.Value, cmbStartDate.SelectedIndex.ToString(), cmbEndDate.SelectedIndex.ToString() );
        frm.ShowDialog();

    }

In next form (frmLeaveRequestConfirmation) I've written following code get values wich passing by previous form) 在下一种形式中(frmLeaveRequestConfirmation),我编写了以下代码,以获取前一种形式传递的值)

    public frmLeaveRequestConfirmation(DateTime SDate, DateTime EDate, string SDFH, string EDFH)
    {
        InitializeComponent();
        lblStartDateInfo.Text = SDate.ToString("dddd, dd MMMM yyyy"); ;
        if (SDate == EDate)
        {
            //some codes are here               

        }

This codes worked fine for dateTime pickers and combo boxes. 此代码对于dateTime选择器和组合框都适用。 Then I tried to pass the value of radio button also, using the same method. 然后,我也尝试使用相同的方法传递单选按钮的值。

//form 1 (frmLeaveRequest) //表格1(frmLeaveRequest)

    private void btnApply_Click(object sender, EventArgs e)
    {
        frmLeaveRequestConfirmation frm = new frmLeaveRequestConfirmation(dtpStartdate.Value, dtpEndDate.Value, cmbStartDate.SelectedIndex.ToString(), cmbEndDate.SelectedIndex.ToString(), rbStartHalfDay.Checked.ToString() );
        frm.ShowDialog();

    }

//form 2 (frmLeaveRequestConfirmation) //表格2(frmLeaveRequestConfirmation)

    public frmLeaveRequestConfirmation(DateTime SDate, DateTime EDate, string SDFH, string EDFH, string RBHD)
    {
        InitializeComponent();
        lblStartDateInfo.Text = SDate.ToString("dddd, dd MMMM yyyy"); ;
        if (SDate == EDate)
        {
         //some codes are here                
        }

} }

But now it shows an error. 但是现在它显示了一个错误。 please somebody give me the correct code for passing the value of radio button to 2nd form. 请有人给我正确的代码,以将单选按钮的值传递给第二形式。 (My 2nd problem is I can't called to the radio button from my 2nd form, even I set the modifier as public.) (我的第二个问题是,即使我将修饰符设置为public,也无法从第二个表单调用单选按钮。)

My suggestion , you should pass your radio button value as Boolean . 我的建议是,您应该将单选按钮值传递为Boolean

frmLeaveRequestConfirmation frm = 
new frmLeaveRequestConfirmation(dtpStartdate.Value, dtpEndDate.Value,   
cmbStartDate.SelectedIndex.ToString(), cmbEndDate.SelectedIndex.ToString(),   
rbStartHalfDay.Checked);
frm.ShowDialog();

and in your second form , 在第二种形式中

public frmLeaveRequestConfirmation(  
DateTime SDate, DateTime EDate, string SDFH, string EDFH, Boolean RBHD)
{
   ......

first of all why do you want to parse the checked flag of the radio button from bool to string? 首先,为什么要解析单选按钮的选中标志(从bool到string)? Is there any reason? 有什么理由吗 If the radio button is not null you shout get the correct value of it. 如果单选按钮不为null,则您会大喊它的正确值。

So what you can do is something like that: 因此,您可以执行以下操作:

private void btnApply_Click(object sender, EventArgs e)
{
    frmLeaveRequestConfirmation frm = new frmLeaveRequestConfirmation(dtpStartdate.Value, dtpEndDate.Value, cmbStartDate.SelectedIndex.ToString(), cmbEndDate.SelectedIndex.ToString(), rbStartHalfDay.Checked );
    frm.ShowDialog();

}

and in the second form: 第二种形式:

public frmLeaveRequestConfirmation(DateTime SDate, DateTime EDate, string SDFH, string EDFH, bool RBHD)
{
    InitializeComponent();
    lblStartDateInfo.Text = SDate.ToString("dddd, dd MMMM yyyy"); ;
    if (SDate == EDate)
    {
     //some codes are here                
    }
}

To your second question, do you need to set the radio button from the other form? 第二个问题,您是否需要从其他表格中设置单选按钮? Why don't you add a property to get the information after the frm.ShowDialog() from the frmLeayveRequestConfirmation? 为什么不从frmLeayveRequestConfirmation添加属性以在frm.ShowDialog()之后获取信息? Like that: 像那样:

public GetButtonIsChecked { get; private set; }
// something like constructor
private void someMethod(bool isChecked) 
{
    GetButtonIsChecked = isChecked;
}

And int the other Class wherer you call the form you do this: 然后将另一个Class插入您要调用的表单,就可以这样做:

    private void btnApply_Click(object sender, EventArgs e)
{
    frmLeaveRequestConfirmation frm = new frmLeaveRequestConfirmation(dtpStartdate.Value, dtpEndDate.Value, cmbStartDate.SelectedIndex.ToString(), cmbEndDate.SelectedIndex.ToString(), rbStartHalfDay.Checked );
    frm.ShowDialog();
    this.rbStartHalfDay.Checked = frm.GetButtonIsChecked;
}

Maybe this will solve your problem at all :) 也许这将彻底解决您的问题:)

Do not use strings to pass integer and boolean values. 不要使用字符串传递整数和布尔值。 Make your second form raising events if you want to notify first form that something happened: 如果您想通知第一个表单发生了某些事情,请进行第二个表单引发事件

public event EventHandler SomethingHappened;

public frmLeaveRequestConfirmation(DateTime startDate, DateTime endDate, 
                                   int startDayIndex, int endDayIndex, 
                                   bool isHalfDayStart)
{
    InitializeComponent();
    lblStartDateInfo.Text = startDate.ToString("dddd, dd MMMM yyyy");

    if (startDate == endDate)
    {
       // some codes are here
    }
}

// When something happened (e.g. user clicked a button)
private void SomeButton_Click(object sender, EventArgs e)
{
    if (SomethingHappened != null)
       SomethingHappened(this, EventArgs.Empty);
}

And create form this way: 并以这种方式创建表单:

void btnApply_Click(object sender, EventArgs e)
{
    frmLeaveRequestConfirmation frm = 
      new frmLeaveRequestConfirmation(dtpStartdate.Value, dtpEndDate.Value, 
                                      cmbStartDate.SelectedIndex, 
                                      cmbEndDate.SelectedIndex,
                                      rbStartHalfDay.Checked);

    frm.SomethingHappened += RequestConfirmation_SomethingHappened;
    frm.ShowDialog();
}

void RequestConfirmation_SomethingHappened(object sender, EventArgs e)
{
    // check radiobutton
    rbStartHalfDay.Checked = true;
}    

NOTE: Use PascalNames for class names and methods. 注意:使用PascalNames作为类名和方法。 Use camelCase for method parameter names. 使用camelCase作为方法参数名称。

To transfering values between Forms you can use Form's Constructor. 要在窗体之间传输值,可以使用窗体的构造函数。 For example I want to send the boolean value of RadioBtn1 from Form1 to Form2. 例如,我想将RadioBtn1的布尔值从Form1发送到Form2。

    public partial class Form2 (bool Value) 
    {
        //Codes
    }

    public partial class Form1 () 
    {
        //Codes

        new Form2(RadioBtn1.Checeked).ShowDialog();
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM