通过窗体C#Visual Studio 2010检查单选按钮 [英] Keep checked radio button checked through forms C# Visual Studio 2010

查看:249
本文介绍了通过窗体C#Visual Studio 2010检查单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10个单选按钮(Form2)的表单供用户检查(全部在同一组中)。然后一个按钮进入下一个表单(Form3)。

在Form3上,我有一个返回按钮返回Form2来根据需要更改单选按钮。

p>

当后退按钮被按下时,它会与所有单选按钮一起进入Form2,但它不显示先前选中的单选按钮。

示例代码:

  string SchoolName =; 

if(radioButton1.Checked)
{
SchoolName = radioButton1.Text;
}

if(radioButton2.Checked)
{
SchoolName = radioButton2.Text;
}

然后使用后退按钮回到以前的表单:

  private void button3_Click(object sender,EventArgs e)
{
this.Close();

th = new Thread(opennewform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}

private void opennewform(object obj)
{
Application.Run(new Form2());


解决方案

您不会回到表单的同一个实例,您正在创建一个新表单。看看我的两个表单项目



表格1

  using System; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1:Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);


private void button1_Click(object sender,EventArgs e)
{
form2.Show();
字符串结果= form2.GetData();
}
}
}

表格2

  using System; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form2:Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();

this.FormClosing + = new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();

private void Form2_FormClosing(对象发送者,FormClosingEventArgs e)
{
//从关闭
停止表单e.Cancel = true;
this.Hide();

public string GetData()
{
return快速的棕色狐狸跳过懒狗;
}

}
}


I have a form with 10 radio buttons (Form2)for a user to check (all in same group). Then a button to go to the next form (Form3).

On Form3 I have a back button to go back to Form2 to change the radio button if needed.

When the back button is pressed, it goes to Form2 with all of the radio buttons, but it doesn't show the previously checked radio button.

Example code:

string SchoolName = "";

if (radioButton1.Checked)
{
    SchoolName = radioButton1.Text;
}

if (radioButton2.Checked)
{
    SchoolName = radioButton2.Text;
}

and then going back to the previous form using back button:

private void button3_Click(object sender, EventArgs e)
{         
    this.Close();

    th = new Thread(opennewform);
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}

private void opennewform(object obj)
{
    Application.Run(new Form2());
}

解决方案

You are not going back to the same instance of the form, you are creating a new form. See my two form project below

Form 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}

Form 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();

            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }

    }
}

这篇关于通过窗体C#Visual Studio 2010检查单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆