C# Find and Relate to Runtime Textbox

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 54
Reputation: wingers1290 is an unknown quantity at this point 
Solved Threads: 0
wingers1290 wingers1290 is offline Offline
Junior Poster in Training

C# Find and Relate to Runtime Textbox

 
0
  #1
Jul 18th, 2009
Hi. I'm new to c# and have ran into a problem, I am using the following code to generate textboxes within a panel.
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. AddFields();
  4. }
  5.  
  6. private void AddFields()
  7. {
  8. if (textBoxIndex != 10)
  9. {
  10. TextBox field = new TextBox();
  11. field.Name = "field" + textBoxIndex.ToString();
  12. textBoxIndex++;
  13. field.Size = new Size(200, 20);
  14. panel1.Controls.Add(field);
  15. }
  16. else
  17. {
  18. MessageBox.Show("A Maximum of 10 Areas is Supported");
  19. }
This also caps the textboxes to a maximum of 10, this code works fine for me it is the next bit that I am stuck on. I need to store values entered into each one of the textbox into diffrent variables which I can't do correctly, I am using the following code to find the textbox control.
  1. if (textBoxIndex != 1)
  2. {
  3.  
  4. }
  5. else
  6. {
  7. Control[] ctrls = Controls.Find("field1", false);
  8. for (int i = 0; i < ctrls.Length; i++)
  9. {
  10. TextBox field1 = (TextBox)ctrls[0];
  11. field1.Text = Variable1;
  12. field1.Text = "";
  13. }
  14.  
  15. }

I think this is were I have gone wrong and I need help please.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: C# Find and Relate to Runtime Textbox

 
0
  #2
Jul 18th, 2009
I think you have to look in the Controls collection of panel1, now you are looking in the Controls collection of the form.
So instead of Controls.Find use panel1.Controls.Find
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: wingers1290 is an unknown quantity at this point 
Solved Threads: 0
wingers1290 wingers1290 is offline Offline
Junior Poster in Training

Re: C# Find and Relate to Runtime Textbox

 
0
  #3
Jul 18th, 2009
Thanks, it now looks like this.
  1. private void FieldStore()
  2. {
  3. if (textBoxIndex != 1)
  4. {
  5.  
  6. }
  7. else
  8. {
  9. Control[] ctrls = panel1.Controls.Find("field1", false);
  10. for (int i = 0; i < ctrls.Length; i++)
  11. {
  12. TextBox field1 = (TextBox)ctrls[0];
  13. field1.Text = Area.Room1;
  14. field1.Text = "";
  15. }
  16. }
  17.  
  18. }

do my if statements look ok, I wasn't sure whether to go != or == I think what I've got is wrong because it's still not working with panel1.Controls.Find.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: C# Find and Relate to Runtime Textbox

 
0
  #4
Jul 18th, 2009
You don't have to define an array of controls here. You have them ready in your panel1 Controls collection.
Perhaps you should use something like this (I have not tested it, I leave that to you)
  1. string Tboxname = string.Empty();
  2. for (int i = 0; i < Numberoftextboxes; i++)
  3. {
  4. Tboxname = "field" + i.ToString();
  5. TextBox field1 = panel1.Controls.Find(Tboxname, false);
  6. field1.Text = Area.Room1;
  7. field1.Text = "";
  8. }
Also the last two statements look wierd. You assign a value to Text and then you make Text empty...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: wingers1290 is an unknown quantity at this point 
Solved Threads: 0
wingers1290 wingers1290 is offline Offline
Junior Poster in Training

Re: C# Find and Relate to Runtime Textbox

 
0
  #5
Jul 18th, 2009
No, sorry but whatever i do to that It dosent work, i might add that i'm fairly new to c#...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: C# Find and Relate to Runtime Textbox

 
1
  #6
Jul 18th, 2009
  1. foreach(Control c in Panel1.Controls)
  2. {
  3. if(c is TextBox)
  4. MessageBox.Show(string.Format("TextBox Name: {0} , TextBox Text {1}",c.Name, c.Text));
  5. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: C# Find and Relate to Runtime Textbox

 
0
  #7
Jul 18th, 2009
Why make it difficult when it is so easy!
Thanks for the clarification Ramy
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: C# Find and Relate to Runtime Textbox

 
1
  #8
Jul 18th, 2009
You're welcome my friend, Danny
I Hope it solves their problem.
Last edited by Ramy Mahrous; Jul 18th, 2009 at 8:01 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: wingers1290 is an unknown quantity at this point 
Solved Threads: 0
wingers1290 wingers1290 is offline Offline
Junior Poster in Training

Re: C# Find and Relate to Runtime Textbox

 
0
  #9
Jul 18th, 2009
Thanks thats brilliant but it dosent solve my original problem, is their a way to get it specific to one textbox only?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: C# Find and Relate to Runtime Textbox

 
0
  #10
Jul 18th, 2009
If you know anything about it. Its Text or its Name you can add a condition
  1. foreach(Control c in Panel1.Controls)
  2. {
  3. if(c is TextBox)
  4. if(c.Name = "field1") //or any property c.Text = "" or c.....
  5. MessageBox.Show(string.Format("TextBox Name: {0} , TextBox Text {1}",c.Name, c.Text));
  6. }
Last edited by Ramy Mahrous; Jul 18th, 2009 at 8:33 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC