J
john.otten
Ok, so I am new to C# development and am working on a Windows Mobile 5
project with Visual Studio 2005.
Among the several forms that my application has is one that displays
11 TextBoxes. These TextBoxes represent 11 values of a 66 member
array of ints, and I only display 11 values at a time (I change the 11
values using a NumericUpDown box with a range from 1 to 6, so if the
UpDown box is set to 1, I display values at indexes 0..10 in the
array, if it is set to 1, I display values at indexes 11..21 in the
array, etc).
When I display the values on the form, I define a TextBox array as
follows:
TextBox[] arrTextBox = new TextBox[11] { textBox1, textBox2, ...
TextBox11 };
This way, I can initialize all the textboxes with pertinent values
from my 66 member array using a loop.
However, when I (and ultimately the user) wish to change a value in
one of the textboxes and store the new value in the 66 member array, I
am stuck creating a function to handle the TextChanged event for
*each* TextBox which creates 11 nearly identical functions
(contributing to spaghetti code). There must be a better way (using
only one function), but my lack of experience is not helping me find
it.
In the old days (C/C++ with MFC), each TextBox had a numerical value
associated with it, and (assuming the 11 textboxes were defined in
numerical order) I could figure out which textbox triggered the event
by subtracting the current textbox's numerical value from the first
textbox's numerical value. However, I do not see a corresponding way
to do this with C#. Something tells me that I might be able to do
this using the Name property of the TextBox by subtracting the values
of the substrings of the last two characters (textBox[01] &
textBox[04] becomes 04 - 01 = 3, which would be the correct index for
textBox4), but I am hoping there is a more elegant solution. Plus, I
do not know how I can get a handle to the textbox that caused the
event (which I assume is the sender object that is a parameter in the
Visual Studio generated function. Assuming that the sender object is
actually the TextBox that triggered the event, is there some way to
cast this sender object to become a TextBox?).
Any suggestions would be greatly appreciated!
John Otten
project with Visual Studio 2005.
Among the several forms that my application has is one that displays
11 TextBoxes. These TextBoxes represent 11 values of a 66 member
array of ints, and I only display 11 values at a time (I change the 11
values using a NumericUpDown box with a range from 1 to 6, so if the
UpDown box is set to 1, I display values at indexes 0..10 in the
array, if it is set to 1, I display values at indexes 11..21 in the
array, etc).
When I display the values on the form, I define a TextBox array as
follows:
TextBox[] arrTextBox = new TextBox[11] { textBox1, textBox2, ...
TextBox11 };
This way, I can initialize all the textboxes with pertinent values
from my 66 member array using a loop.
However, when I (and ultimately the user) wish to change a value in
one of the textboxes and store the new value in the 66 member array, I
am stuck creating a function to handle the TextChanged event for
*each* TextBox which creates 11 nearly identical functions
(contributing to spaghetti code). There must be a better way (using
only one function), but my lack of experience is not helping me find
it.
In the old days (C/C++ with MFC), each TextBox had a numerical value
associated with it, and (assuming the 11 textboxes were defined in
numerical order) I could figure out which textbox triggered the event
by subtracting the current textbox's numerical value from the first
textbox's numerical value. However, I do not see a corresponding way
to do this with C#. Something tells me that I might be able to do
this using the Name property of the TextBox by subtracting the values
of the substrings of the last two characters (textBox[01] &
textBox[04] becomes 04 - 01 = 3, which would be the correct index for
textBox4), but I am hoping there is a more elegant solution. Plus, I
do not know how I can get a handle to the textbox that caused the
event (which I assume is the sender object that is a parameter in the
Visual Studio generated function. Assuming that the sender object is
actually the TextBox that triggered the event, is there some way to
cast this sender object to become a TextBox?).
Any suggestions would be greatly appreciated!
John Otten