943,747 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1847
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2009
0

C# Find and Relate to Runtime Textbox

Expand Post »
Hi. I'm new to c# and have ran into a problem, I am using the following code to generate textboxes within a panel.
C# Syntax (Toggle Plain Text)
  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.
C# Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
wingers1290 is offline Offline
89 posts
since May 2009
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

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
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

Thanks, it now looks like this.
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
wingers1290 is offline Offline
89 posts
since May 2009
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

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)
c# Syntax (Toggle Plain Text)
  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...
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

No, sorry but whatever i do to that It dosent work, i might add that i'm fairly new to c#...
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
wingers1290 is offline Offline
89 posts
since May 2009
Jul 18th, 2009
1

Re: C# Find and Relate to Runtime Textbox

C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

Why make it difficult when it is so easy!
Thanks for the clarification Ramy
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jul 18th, 2009
1

Re: C# Find and Relate to Runtime Textbox

You're welcome my friend, Danny
I Hope it solves their problem.
Last edited by Ramy Mahrous; Jul 18th, 2009 at 8:01 am.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

Thanks thats brilliant but it dosent solve my original problem, is their a way to get it specific to one textbox only?
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
wingers1290 is offline Offline
89 posts
since May 2009
Jul 18th, 2009
0

Re: C# Find and Relate to Runtime Textbox

If you know anything about it. Its Text or its Name you can add a condition
C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Replace the Contents of the Array
Next Thread in C# Forum Timeline: HOW To Setup Project with SQL?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC