I wrote textbox names to a textfile with values.
In another program im reading in those value with the textboxes, but the txtbox.name property is not doing what i though it would( Not printing anything)
textfile row--> Where oneLayer is the name of the textbox.
oneLayer = moveRight
private
void writeToApp(string readIn){
//MessageBox.Show(readIn); TextBox txtBox = new TextBox(); int index = readIn.IndexOf(@"="); string name = readIn.Substring(0, index) + "_text";txtBox.Name = name;
txtBox.Text = "This is a test";
Help!!! In debug mode, the txtbox.name property is getting set, but not printing to the program.
Thanks.
AlanPosted Oct 19, 2007, 4:52 PM
There's no easier way to my knowledge in .NET 1.0 / 1.1.
In .NET 2.0 it's easy because they provided an overload of the Controls property which takes the Name of the control rather than its index.
Unless you have a lot of controls on the form it should still work pretty quick.
j_sen21Posted Oct 19, 2007, 4:43 PM
But it has to cycle through all the controls everytime. Theres not a straight shot way?
AlanPosted Oct 19, 2007, 2:37 PM
The code which I posted earlier (for .NET 1.0/1.1) does in fact do it individually:
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Name == name)
{
TextBox txtBox = (TextBox)c;
txtBox.Text = "This is a test";
break;
}
}
It looks through all the controls on the form till it finds a TextBox with a Name property which is the same as the name which you've just parsed out from the argument to the method.
It then casts that control to a TextBox, sets its Text property and breaks from the foreach loop which effectively ends the method. The method can then be called again with another argument.
I take it that the method is defined inside the form class and not in some other class?
j_sen21Posted Oct 19, 2007, 1:43 PM
The textboxes are public and im looping outside of the function call so I need to do it individually.
So:
private void writeToApp(string readIn)
{
int index = readIn.IndexOf(@"=");
string name = readIn.Substring(0, index) + "_text";
Control c = TextBox or something;
// if (c is TextBox && c.Name == name)
//{
TextBox txtBox = (TextBox)c;
txtBox.Text = "This is a test";
// }
thanks
AlanPosted Oct 19, 2007, 12:45 PM
Ah, in that case , you need something like this.
// .net 2.0 or gretaer
private void writeToApp(string readIn)
{
int index = readIn.IndexOf(@"=");
string name = readIn.Substring(0, index) + "_text";
TextBox txtBox = (TextBox)this.Controls(name);
txtBox.Text = "This is a test";
}
// .net 1.0 or 1.1
private void writeToApp(string readIn)
{
int index = readIn.IndexOf(@"=");
string name = readIn.Substring(0, index) + "_text";
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Name == name)
{
TextBox txtBox = (TextBox)c;
txtBox.Text = "This is a test";
break;
}
}
}
The code assumes the TextBoxes have been placed directly on the form. If they're in some other container such as a panel, then replace 'this' with the container object reference so you can index into, or iterate through, the Controls collection for the container.
j_sen21Posted Oct 19, 2007, 11:03 AM
Sorry, wasnt specific enough. The textbox already exists. The name of the textbox is oneLayer.
So I want oneLayer.Text = "this is a test" in the form.
I know that I could do a case statement for all the text boxes, but that defeats the dynamic purpose.
Thanks.
AlanPosted Oct 19, 2007, 10:46 AM
When you're adding controls at runtime, you need to set properties such as Size and Location and add them to the form's Controls collection before they can be displayed. Something like this:
txtBox.Location = new System.Drawing.Point(200, 50); // or whatever txtBox.Size = new System.Drawing.Size(100, 20);this.Controls.Add(txtBox);