从表单元素中获取价值? [英] Get value from forms element?

查看:81
本文介绍了从表单元素中获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我从表单上的所有文本框中获取值。

  string  name = Name.Text; 
string secondname = SecondName.Text;
string telephone = Telephone.Text;
string document = Document.Text;
string dateborn = DateBorn.Text;
bool bValidName = ValidName();







我想从其值中创建一个对象或列表并将其传送给函数

public Add(obj){} insted public添加(secondname,telephone,document)

怎么做?

解决方案

定义你的自定义类,例如:

 < span class =code-keyword>使用系统; 
使用 System.Collections.Generic;
使用 System.Text;

class MyFirstClass
{
private string sfname = string .Empty;
private string ssname = string .Empty;
private string stelephone = string .Empty;
private string sdocument = string .Empty;
DateTime ddateborn = DateTime.MinValue;
private bool bValidName = false ;

public string FirstName
{
get { return sfname;}
set {sfname = value ; }
}

public string SecondName
{
get { return ssname; }
set {ssname = value ; }
}

public string 电话
{
获取 {返回 stelephone; }
set {stelephone = value ; }
}

public string 文件
{
get { return sdocument; }
set {sdocument = value ; }
}

public DateTime DateBorn
{
得到 {返回 ddateborn; }
set {ddateborn = value ; }
}

public bool ValidName
{
get { return bValidName; }
set {bValidName = value ; }
}
}





用法:

 MyFirstClass mfc =  new  MyFirstClass(); 
mfc.FirstName = FirstName.Text;
mfc.SecondName = SecondName.Text;
mfc.DateBorn = DateTime.Parse(DateBorn.Text);
mfc.Document = Document.Text;
mfc.Telephone = Telephone.Text;
mfc.ValidName = true ;





更多关于你会在这里找到的课程: class(C#Reference) [ ^ ]

类(C#编程指南) [ ^ ]


创建一个新类来保存对象:

  public   class  Person 
{
public string 名称{ get ; set ; }
public string SecondName { get ; set ; }
public string 电话{ get ; set ; }
public string 文档{ get ; set ; }
public DateTime DOB { get ; set ; }
public bool IsValidName
{
get { return false ; }
}
public Person( string name, string secondName, string telephone, string document,DateTime dob)
{
Name = name;
SecondName = secondName;
电话=电话;
文件=文件;
DOB = dob;
}
public 覆盖 string ToString()
{
return string .Format(< span class =code-string>
{0} {1},Name,SecondName);
}
}



然后,您可以构造Person的实例,并将其传递给您的处理方法,或将它们存储在列表中:

列表< Person> guestList =  new  List< Person>(); 
...
guestList.Add( new Person(Name.Text,SecondName.Text,Telephone.Text,Document.Text,DateTime。解析(DateBorn.Text)));


Hello!
I get values from all textbox on form.

string name = Name.Text;
          string secondname = SecondName.Text;
          string telephone = Telephone.Text;
          string document = Document.Text;
          string dateborn = DateBorn.Text;
          bool bValidName = ValidName();




I want create one object or list from its values and transmit this to function
public Add(obj) {} insted public Add(secondname, telephone, document)
How it make?

解决方案

Define your custom class, for example:

using System;
using System.Collections.Generic;
using System.Text;

    class MyFirstClass
    {
        private string sfname = string.Empty;
        private string ssname = string.Empty;
        private string stelephone = string.Empty;
        private string sdocument = string.Empty;
        DateTime ddateborn = DateTime.MinValue;
        private bool bValidName = false;

        public string FirstName
        {
            get { return sfname;}
            set { sfname = value; }
        }

        public string SecondName
        {
            get { return ssname; }
            set { ssname = value; }
        }

        public string Telephone
        {
            get { return stelephone ; }
            set { stelephone  = value; }
        }

        public string Document
        {
            get { return sdocument; }
            set { sdocument  = value; }
        }

        public DateTime DateBorn
        {
            get { return ddateborn ; }
            set { ddateborn  = value; }
        }

        public bool ValidName
        {
            get { return bValidName ; }
            set { bValidName = value; }
        }
}



Usage:

MyFirstClass mfc = new MyFirstClass();
mfc.FirstName = FirstName.Text;
mfc.SecondName = SecondName.Text;
mfc.DateBorn = DateTime.Parse(DateBorn.Text);
mfc.Document = Document.Text;
mfc.Telephone = Telephone.Text;
mfc.ValidName = true;



More about classes you''ll find here: class (C# Reference)[^]
Classes (C# Programming Guide)[^]


Create a new class to hold the objects:

public class Person
    {
    public string Name { get; set; }
    public string SecondName { get; set; }
    public string Telephone { get; set; }
    public string Document { get; set; }
    public DateTime DOB { get; set; }
    public bool IsValidName
        {
        get { return false; }
        }
    public Person(string name, string secondName, string telephone, string document, DateTime dob)
        {
        Name = name;
        SecondName = secondName;
        Telephone = telephone;
        Document = document;
        DOB = dob;
        }
    public override string ToString()
        {
        return string.Format("{0} {1}", Name, SecondName);
        }
    }


You can then construct an instance of a Person, and pass it through to your processing methods, or store them in a list:

List<Person> guestList = new List<Person>();
...
guestList.Add(new Person(Name.Text, SecondName.Text, Telephone.Text, Document.Text, DateTime.Parse(DateBorn.Text)));


这篇关于从表单元素中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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