Toon posts:

form items dynamisch benaderen door een array

Pagina: 1
Acties:

Verwijderd

Topicstarter
Hallo,

Ik ben net begonnen met C#. Ik heb enige programmeerkennis, maar ik loop vast bij het volgende:

Ik heb een array met daarin nummers. Die nummers wil ik vervolgens later gebruiken om in de applicatie (forms application) om een form item te benaderen en de Text() property ervan te wijzigen. Mijn vraag is, hoe kan ik een string converteren naar een objectnaam en zodoende ermee werken?

bijvoorbeeld

code:
1
2
3
4
5
double[] units = double[] { 500, 200, 100, 50, 20, 10, 5, 2, 1};
foreach (unit in units){
    string name = "unit_" + unit;
    name.Text = (unit * 2).ToString(); // ik wil op deze manier dus properties kunnen setten van onderdelen van het formulier
}

[ Voor 26% gewijzigd door Verwijderd op 10-09-2014 09:36 ]


Verwijderd

Ik ga even uit dat je het volgende bedoelt:

Jij hebt een Winform waarop negen textboxes staan, en laten we zeggen dat de namen van deze textboxes "textBox1" tot en met "textBox9" zijn?
Jij wilt in deze textboxes de waardes uit jouw array met double waardes in deze textboxes plaatsen, van top tot bodem?

Dan zou je het kunnen doen op deze manier:

Formulier met controls:
C#:
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
namespace Testing
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.textBox6 = new System.Windows.Forms.TextBox();
            this.textBox7 = new System.Windows.Forms.TextBox();
            this.textBox8 = new System.Windows.Forms.TextBox();
            this.textBox9 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(472, 20);
            this.textBox1.TabIndex = 0;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(12, 38);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(472, 20);
            this.textBox2.TabIndex = 0;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(12, 64);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(472, 20);
            this.textBox3.TabIndex = 0;
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(12, 90);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(472, 20);
            this.textBox4.TabIndex = 0;
            // 
            // textBox5
            // 
            this.textBox5.Location = new System.Drawing.Point(12, 116);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(472, 20);
            this.textBox5.TabIndex = 0;
            // 
            // textBox6
            // 
            this.textBox6.Location = new System.Drawing.Point(12, 142);
            this.textBox6.Name = "textBox6";
            this.textBox6.Size = new System.Drawing.Size(472, 20);
            this.textBox6.TabIndex = 0;
            // 
            // textBox7
            // 
            this.textBox7.Location = new System.Drawing.Point(12, 168);
            this.textBox7.Name = "textBox7";
            this.textBox7.Size = new System.Drawing.Size(472, 20);
            this.textBox7.TabIndex = 0;
            // 
            // textBox8
            // 
            this.textBox8.Location = new System.Drawing.Point(12, 194);
            this.textBox8.Name = "textBox8";
            this.textBox8.Size = new System.Drawing.Size(472, 20);
            this.textBox8.TabIndex = 0;
            // 
            // textBox9
            // 
            this.textBox9.Location = new System.Drawing.Point(12, 220);
            this.textBox9.Name = "textBox9";
            this.textBox9.Size = new System.Drawing.Size(472, 20);
            this.textBox9.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(878, 411);
            this.Controls.Add(this.textBox9);
            this.Controls.Add(this.textBox8);
            this.Controls.Add(this.textBox7);
            this.Controls.Add(this.textBox6);
            this.Controls.Add(this.textBox5);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.TextBox textBox5;
        private System.Windows.Forms.TextBox textBox6;
        private System.Windows.Forms.TextBox textBox7;
        private System.Windows.Forms.TextBox textBox8;
        private System.Windows.Forms.TextBox textBox9;
    }
}


Formulier Code:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Windows.Forms;

namespace Testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetTextBoxes();
        }

        private void SetTextBoxes()
        {
            double[] units = new double[] { 500, 200, 100, 50, 20, 10, 5, 2, 1};
            int i = 1;
            foreach (double u in units)
            {
                this.Controls[("txtboxname" + i++)].Text = string.Format("{0}", u * 2);
            }
        }
    }
}

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
Verwijderd schreef op woensdag 10 september 2014 @ 09:20:
Mijn vraag is, hoe kan ik een string converteren naar een objectnaam en zodoende ermee werken?
Het simpele antwoord op deze vraag is eigenlijk: Niet.

Je wil iets in de trant van Eval doen, maar over het algemeen is dat Evil. Er zijn wel mogelijkheden zoals Reflection, maar zeker in dit geval wil je dat eigenlijk helemaal niet.

Er zijn twee makkelijke oplossingen voor het probleem waar je tegenaan loopt

1. Zoals Mostrow laat zien worden controls in Winforms al netjes in een collectie bijgehouden met hun naam erbij. ( Let wel op dat deze naam niet perse overeen hoeft te komen met de variabele, al is dat wel de default van de designer )
2. Je Controls zelf in een collectie ( Array/List/Dictionary/etc. ) zetten zodat je er bij kunt komen, eventueel als samengesteld object waar je ook meteen de extra informatie bij stopt.

“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”