繁体   English   中英

从文本框接收数据,并从单选按钮C#接收值

[英]receive data from textbox and value from radiobutton c#

在此处输入图片说明

如上图所示,有3种单选按钮和一个文本框。 为了让用户搜索数据,用户需要填写文本框并选择要搜索的类型。 该数据将以其他形式出现。

这是我在搜索表单中的代码。

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;    
using System.Xml;
using System.Xml.Linq;

namespace SliceLink
{
    public partial class SearchForm : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        string _radio;
        public string radio 
        {
            get { return this._radio;  }
            set { this._radio = value; }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            //RadioButton rb = (RadioButton)sender;
            if (textBox1.Text == "")
                MessageBox.Show("Please enter keyword to search");
            else 
            {                              
                Form3 form3 = new Form3(textBox1.Text);
                form3.Show();   
            }        
        }
    }
}

这是我在viewForm中查看的代码。

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;    
using System.Xml;
using System.Xml.Linq;

namespace SliceLink
{
    public partial class ViewForm : Form
    {       
        public Form3(string sTEXT)
        {
            InitializeComponent();
            XmlDocument xml = new XmlDocument();            
            xml.Load("C:\\Users\\HDAdmin\\Documents\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
            XmlNodeList xnList = xml.SelectNodes("/Patient/Patient/Name");
            //XmlNodeList xnList = xml.SelectNodes(sTEXT);
            foreach (XmlNode xn in xnList)
            {
                string name = xn.InnerText;                
                textBox1.Text = name;
            }
        }
    }
}

我可以在textbox收到用户输入的内容,但我不知道如何检索用户选择的类型。 有办法吗?

我将以下面的方式组织检查选中的单选按钮:

  1. 对于每个单选按钮,使用数字值(0、1、2等)填充Tag属性。
  2. 用相应的值创建枚举。
  3. 所有单选按钮的Click事件设置相同的事件处理程序。
  4. 在事件处理程序中检查sender单选按钮的Tag值。

因此,这是表单的示例代码:

internal enum SearchType
{
    All = 0, Date = 1, Id = 2
}

public partial class MainForm : Form
{
    private SearchType _selectedSearchType = SearchType.All;

    private void searchButton_Click(object sender, EventArgs e)
    {
        // Use _selectedSearchType to do search.
    }

    private void radioButton_Click(object sender, EventArgs e)
    {
        _selectedSearchType = (SearchType)Enum.Parse(typeof(SearchType), ((Control)sender).Tag.ToString());
    }
}

好处:您可以轻松添加任意数量的单选按钮,对其进行更改等,而无需更改后备代码。 而且,您可以在不了解单选按钮的情况下使用选定的值。

您可以为不同的选项创建一个enum类型,然后在包含单选按钮的窗体上创建一个公共property ,并根据选择的哪个返回相应的enum值。

因此,您将获得类似以下内容的信息:

public enum FindType { DATE, ID, ALL }

在您的SearchForm

public FindType FindGrouping
{
   get
   {
      if (radioButtonDate.Checked)
         return FindType.DATE;
      // ... etc.
}

并使用ViewFormViewForm检索值searchFormInstance.FindGrouping;

暂无
暂无

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

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