C# TextBox waarde geven wordt genegeerd

Pagina: 1
Acties:
  • 173 views sinds 30-01-2008
  • Reageer

  • Norjee
  • Registratie: April 2000
  • Niet online
C# textbox waarde geven vanuit script...

ik heb dit als soort van textbox
code:
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
// waarschijnlijk teveel, maar ik heb nog nooit met c# geknoeit, weet nog niet wat voor wat is, en mij 
// progammaatje is ook iets uigebreider

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Threading;

public class textInput : GroupBox
    {
    private string initVal;     
    public TextBox TIVal = new TextBox();   
    
    public textInput()
        {}
        
    public textInput(string text , string value)
        {
        Label label1= new Label();          
        label1.Text = text+":";
        label1.Location = new Point(10,17);
        label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight); 
        
        TIVal = new TextBox();
        TIVal.Location = new Point(100, 15);
        TIVal.Size = new Size(200, 50);
        TIVal.Text = value;
                    
        this.Controls.Add(label1);
        this.Controls.Add(TIVal);   
        this.FlatStyle = FlatStyle.System;
        this.Size = new Size(310 , 44); 
        
        }   
    
    public string getVal()
        {
        return TIVal.Text;  
        }
    
    public void setVal(string aap)
        {
        TIVal.Text = aap;
        initVal = aap;
        
        Console.WriteLine("Debug shit from TBOX: "+getVal());   
        Console.WriteLine("SETVAL from TBOX: "+TIVal.Text);             
        }
        
    public void reset(string value)
        {
        TIVal.Text = initVal;   
        }
    }
        
public class TestForm : Form
    {
    public textInput textServer = new textInput();
    
    public TestForm ()
        {
        InitializeComponent();   
        }
        
    
    
    /*
    private void InitializeComponent() 
        {
        textInput textServer = new textInput("Server" , "jhjhggj");
        textServer.Location = new Point(0 , 0);
        this.Controls.Add(textServer);      
        }
    */
    
    
    private void InitializeComponent() 
        {
        textInput textServer = new textInput("Server" , "jhjhggj");
        textServer.Location = new Point(0 , 0);
        this.Controls.Add(textServer);
        // nu mij setVal gebruiken, dit gaat prima  
        textServer.setVal("een waarde door setVal");
        }
    }
        
class main
    {
    
        
    public static void Main(string[] args)
        {
        TestForm tf = new TestForm();
        // nu mij setVal gebruiken, dit gaat fout    
        tf.textServer.setVal("En dit werkt niet");
        // ook rechtstreeks aanroepen wordt niet weergegeven
        tf.textServer.TIVal.Text = "hjhjgfjgf";
        Application.Run(tf);    
        
        // nu getval.. en help, het is de waarde van de vorige setval..
        // dit moet dus de waarde van die textbox wezen...
        // doe ik bovestaande setVal niet, dan is dit leeg.. niet goed dus
        Console.WriteLine(tf.textServer.TIVal.Text);
        }
    }

De vraag,
hoe krijg ik textInput.setVal() , textInput.getVal() en textInput.reset() werkend als ik ze in de main aanroep.

Verwijderd

Kan het niet zijn dat voor App.Run(...) je textbox nog niet bestaat en pas dan aangemaakt wordt?

Verder zal ik je afraden om innerclasses te gebruiken, maak er gewoon een aparte class van, da's wel zo netjes en overzichtelijk :). Zie ook hier voor coding guidelines :).

  • tomato
  • Registratie: November 1999
  • Niet online
IMHO heb je innerclasses maar zeer zelden echt nodig. Als je ze gebruikt, wees er dan zeker van dat je een goede reden hebt.

  • Norjee
  • Registratie: April 2000
  • Niet online
Die innerclass is er vanzelf ingekomen omdat ik er eerst helemaal geen class van gemaakt had maar naar 4 keer hetzelfde nodig te hebben vond ik een class toch makkelijker :)

Weggehaald, kzal het nooit meer doen :)

ik heb ook nog een soortgelijke class gemaakt met true - false buttons..

hier werken getVal en setVal wel... vreemd he...

code met truefalse group
code:
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
194
195
196
// waarschijnlijk teveel, maar ik heb nog nooit met c# geknoeit, weet nog niet wat voor wat is, en mij 
// progammaatje is ook iets uigebreider

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Threading;

public class trueFalseGroup : GroupBox
        {
        private RadioButton trueButton = new RadioButton();
        private RadioButton falseButton = new RadioButton();        
        private bool initVal;
        
        public trueFalseGroup()
            {}
            
        public trueFalseGroup(string text , bool def)
            {
            Label label1= new Label();          
            label1.Text = text+":";
            label1.Location = new Point(10,15);
            label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight); 
            
            
            Label label2= new Label();          
            label2.Text = "Enabled";
            label2.Location = new Point(35,39);
            label2.Size = new Size (label2.PreferredWidth, label2.PreferredHeight);     
        
            
            Label label3= new Label();          
            label3.Text = "Disabled";
            label3.Location = new Point(35,59);
            label3.Size = new Size (label3.PreferredWidth, label3.PreferredHeight);
            
            trueButton = new RadioButton();
            falseButton = new RadioButton();
            trueButton.Location = new Point(10 , 35);
            trueButton.Checked = def;
            falseButton.Location = new Point(10 , 55);
            falseButton.Checked = !def;
               // Set the FlatStyle of the GroupBox.
            this.FlatStyle = FlatStyle.System;
         
               // Add the RadioButtons to the GroupBox.
               this.Controls.Add(label1);
               this.Controls.Add(label3);
               this.Controls.Add(label2);
               
            
            this.Controls.Add(trueButton);
            this.Controls.Add(falseButton); 
            
            this.Size = new Size(150 , 85);     
            }
        
        public bool getVal()
            {
            return(trueButton.Checked);
            }
        
        public void setVal(bool value)
            {
            initVal = value;
            trueButton.Checked = value;
            falseButton.Checked = !value;
            }
        
        public void rest()
            {
            trueButton.Checked = initVal;
            falseButton.Checked = initVal;  
            }
            
        }

public class textInput : GroupBox
    {
    private string initVal;     
    public TextBox TIVal = new TextBox();   
    
    public textInput()
        {}
        
    public textInput(string text , string value)
        {
        Label label1= new Label();          
        label1.Text = text+":";
        label1.Location = new Point(10,17);
        label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight); 
        
        TIVal = new TextBox();
        TIVal.Location = new Point(100, 15);
        TIVal.Size = new Size(200, 50);
        TIVal.Text = value;
                    
        this.Controls.Add(label1);
        this.Controls.Add(TIVal);   
        this.FlatStyle = FlatStyle.System;
        this.Size = new Size(310 , 44); 
        
        }   
    
    public string getVal()
        {
        return TIVal.Text;  
        }
    
    public void setVal(string aap)
        {
        TIVal.Text = aap;
        initVal = aap;
        
        Console.WriteLine("Debug shit from TBOX: "+getVal());   
        Console.WriteLine("SETVAL from TBOX: "+TIVal.Text);             
        }
        
    public void reset(string value)
        {
        TIVal.Text = initVal;   
        }
    }
        
public class TestForm : Form
    {
    public textInput textServer = new textInput();
    public trueFalseGroup tfgroep = new trueFalseGroup();
    
    public TestForm ()
        {
        InitializeComponent();   
        }
        
    
    
    /*
    private void InitializeComponent() 
        {
        textInput textServer = new textInput("Server" , "jhjhggj");
        textServer.Location = new Point(0 , 0);
        this.Controls.Add(textServer);      
        }
    */
    
    
    private void InitializeComponent() 
        {
        textInput textServer = new textInput("Server" , "jhjhggj");
        textServer.Location = new Point(10 , 0);
        this.Controls.Add(textServer);
        
        
        
        tfgroep = new trueFalseGroup("My tf groep" , false);        
        tfgroep.Location = new Point(10 , 50);      
        this.Controls.Add(tfgroep);
        
        // nu mij setVal gebruiken, dit gaat prima  
        textServer.setVal("een waarde door setVal");
        }
    }
        
class main
    {
    
        
    public static void Main(string[] args)
        {
        TestForm tf = new TestForm();
        // nu mij setVal gebruiken, dit gaat fout    
        tf.textServer.setVal("En dit werkt niet");
        // ook rechtstreeks aanroepen wordt niet weergegeven
        tf.textServer.TIVal.Text = "hjhjgfjgf";
        
        // met trueFalsegroup gaat het prima...
        // hij was geinitialseerd met false en wordt nu true
        tf.tfgroep.setVal(true);
        
        Application.Run(tf);    
        
        // nu getval.. en help, het is de waarde van de vorige setval..
        // dit moet dus de waarde van die textbox wezen...
        // doe ik bovestaande setVal niet, dan is dit leeg.. niet goed dus
        Console.WriteLine(tf.textServer.getVal());
        
        // met trueFalsegroup gaat het prima...
        Console.WriteLine("Waarder True False groep: " + tf.tfgroep.getVal());
        }
    }

  • Norjee
  • Registratie: April 2000
  • Niet online
Hij doet het :)

in TestForm:
code:
1
textInput textServer = new textInput("Server" , "jhjhggj");

wordt
code:
1
textServer = new textInput("Server" , "jhjhggj");

en ook in TestForm:
code:
1
public textInput textServer = new textInput();

wordt
code:
1
public textInput textServer; // ken ook private wezen, maar dan werkt de main niet meer in dit voorbeeld

class is dit geworden, om getVal te laten werken, setVal deed het al prima met bovenstaande wijziging..
code:
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
public class textInput : GroupBox
    {
    private string initVal; 
    private string curVal;  
    private TextBox TIVal = new TextBox();  
    
    public textInput()
        {}
        
    public textInput(string text , string value)
        {
        Label label1= new Label();          
        label1.Text = text+":";
        label1.Location = new Point(10,17);
        label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight); 
        
        //TIVal = new TextBox();
        TIVal.Location = new Point(100, 15);
        TIVal.Size = new Size(200, 50);
        //TIVal.Text = initVal;                 
        EventHandler handler = new EventHandler(c_keyUp);
        TIVal.TextChanged   += handler;
        
        this.Controls.Add(label1);
        this.Controls.Add(TIVal);   
        this.FlatStyle = FlatStyle.System;
        this.Size = new Size(310 , 44); 
        
        }
    
    private void c_keyUp (object sender, EventArgs e)
        {
        curVal = TIVal.Text;        
        }   
    
    public string getVal()
        {
        return curVal;//TIVal.Text; 
        }
    
    public void setVal(string value)
        {
        TIVal.Text = value;
        initVal = value;
        curVal = value;     
        }
        
    public void reset()
        {
        TIVal.Text = initVal;   
        }
    }