FORMAN_prueba /ControlesForm/GestionForm.cs

Language C# Lines 194
MD5 Hash a323c2ae29e8ca585d1268492012db3c
Repository https://github.com/Prozid/FORMAN_prueba.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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.Linq;
using System.IO;

namespace ControlesForm
{
    public partial class GestionForm : Form
    {
        private DataTable genero;

        public GestionForm()
        {
            InitializeComponent();
            genero = new DataTable("Género");
            genero.Columns.Add("Texto");
            genero.Columns.Add("Valor");
            DataColumn[] keys = new DataColumn[1];
            keys[0] = genero.Columns[1];
            genero.PrimaryKey = keys;

        }

        private bool validateFields()
        {
            return (txtName.Text != "") &&
                    (txtLastName.Text != "") &&
                    (txtPass.Text != "") &&
                    (pkrBornDate.Value != null);
        }

        private Person.TipoPago getPayTypeFromRadioButton()
        {
            return (Person.TipoPago)System.Enum.Parse(
                    typeof(Person.TipoPago),
                    gbxPay
                        .Controls
                            .OfType<RadioButton>()
                                .FirstOrDefault(r => r.Checked)
                                    .Text
                );
        }

        private void btnMostrar_Click(object sender, EventArgs e)
        {
            if (validateFields())
            {
                Person p = getPersonFromForm();

                detailsPerson details = new detailsPerson(p);
                DialogResult resp = details.ShowDialog();
                
                MessageBox.Show(
                    "Edad",
                    resp.ToString(),
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);                
            }
            else
            {
                MessageBox.Show(
                    "Faltan campos",
                    "Rellene todos los campos",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
        }

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

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                Person p;
                String path = Directory.GetCurrentDirectory().ToString() + "\\xml.xml";
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Person));

                FileStream reader = new FileStream(path, FileMode.Open);
                p = (Person)x.Deserialize(reader);
                reader.Close();

                txtName.Text = p.name;
                txtLastName.Text = p.lastName;
                txtDescriptions.Text = p.descriptions;
                txtPass.Text = p.pass;
                cbxGenre.SelectedIndex = genero.Rows.IndexOf(genero.Rows.Find(p.sexo.ToString()));
                gbxPay.Controls.OfType<RadioButton>().Single<RadioButton>(r => r.Text.Equals(p.pago.ToString())).Checked = true; 
            }
            catch (FileNotFoundException ex)
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error:");
            }
            

            DataRow dr = genero.NewRow();
            dr[0] = "Mujer";
            dr[1] = 0;
            genero.Rows.Add(dr);

            dr = genero.NewRow();
            dr[0] = "Hombre";
            dr[1] = 1;
            genero.Rows.Add(dr);


            cbxGenre.DataSource = genero;
            cbxGenre.DisplayMember = genero.Columns[0].ToString();
            cbxGenre.ValueMember = genero.Columns[1].ToString();

            
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (validateFields())
            {
                Person p = getPersonFromForm();

                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Person));
                FileStream s = new FileStream(Directory.GetCurrentDirectory() + "\\xml.xml", FileMode.Create);
                x.Serialize(s, p);
                s.Close();
            }
        }

        private Person getPersonFromForm()
        {
            Person p;
            if (validateFields())
            {
                p = new Person
                {
                    name = txtName.Text,
                    lastName = txtLastName.Text,
                    pass = txtPass.Text,
                    descriptions = txtDescriptions.Text,
                    bornDate = pkrBornDate.Value,
                    pago = getPayTypeFromRadioButton()
                };
            }
            else
            {
                p = new Person();
            }
            return p;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            ListViewItem c;


            for(int i = 1; i < 5; i++)
            {
                c = new ListViewItem();
                c.Text = i.ToString();
                c.SubItems.Add("García");
                c.SubItems.Add("Pepito");

                String s = "";

                for(int j = 0; j<9; j++)
                {
                    s += (new Random().NextDouble()*10).ToString();
                }

                
                c.SubItems.Add(s);

                listView1.Items.Add(c);
            }                
        }

        private void btnResetListView_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }
    }
}
Back to Top