| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 99426 | 2009-05-01 11:45:00 | C# help! | WarNox (8772) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 769982 | 2009-05-01 11:45:00 | Hey! Ok this is driving me crazy, I've spent a good 2 hours trying to research it but came up with nothing. I'm trying to use a String to refer to a TextBox, kind of a dynamic name. eg. Say I have: Textbox1 Textbox2 Textbox3 ...on a form and then I want to call a method with the text from each of those textboxes. So I want a loop to do: String a = "Textbox" + counter.ToString(); methodToCall(a.Text); Of course that doesn't work. But how can I convert String A into something that the compiler will recognise as a TextBox object!? Hope this makes sense. ps. I tried using a Control mycontrol = FindControl.... -> but my visual studio will cannot find 'FindControl'. Thanks, Gregor |
WarNox (8772) | ||
| 769983 | 2009-05-01 20:27:00 | I haven't used c#, but drawing on some parallels with MSAccess... Instead of myControl, what about currentControl or currentObject obviously move the 'focus' to the text box before applying this. |
Paul.Cov (425) | ||
| 769984 | 2009-05-01 20:42:00 | Hey! Ok this is driving me crazy, I've spent a good 2 hours trying to research it but came up with nothing. I'm trying to use a String to refer to a TextBox, kind of a dynamic name. eg. Say I have: Textbox1 Textbox2 Textbox3 ...on a form and then I want to call a method with the text from each of those textboxes. So I want a loop to do: String a = "Textbox" + counter.ToString(); methodToCall(a.Text); Of course that doesn't work. But how can I convert String A into something that the compiler will recognise as a TextBox object!? Hope this makes sense. ps. I tried using a Control mycontrol = FindControl.... -> but my visual studio will cannot find 'FindControl'. Thanks, Gregor The easiest way is to use the FindControl(). So: for (int i = 1; i < 4; i++){ TextBox t = (TextBox)this.FindControl("Textbox" + i); methodToCall(t.Text); } Note: You'll need to add some checks to make sure the returned object isn't null. You could also try accessing the Controls collection directly: www.csharpfriends.com |
somebody (208) | ||
| 769985 | 2009-05-02 05:07:00 | Cool, thanks for the replies guys! I'll give this a try and see how I go! Gregor |
WarNox (8772) | ||
| 769986 | 2009-05-02 05:20:00 | Cool, thanks for the replies guys! I'll give this a try and see how I go! Gregor Out of curiosity, why are you wanting to dynamically reference the textbox controls at all? You should really give the textboxes more meaningful names - for example "phoneNumberTextBox" or "txtPhoneNum" (depending on what naming convention you prefer) - so that when you come back to read your code in 6 months time, you actually know what it is referring to. |
somebody (208) | ||
| 769987 | 2009-05-02 05:40:00 | Because I want the program to move from one text box to another and do stuff, rather than referring to each one individually. --- Hmm, well I got it to work using: this.Controls[whichPc].Text but I cannot use the same sort of method for an OvalShape or a ProgressBar. I've also tried doing it the FindControl way but my VS2008 does not recognize FindControl as a valid method!? I've added these two lines at the top but still no luck: using System.Windows.Forms; using System.Web.UI; |
WarNox (8772) | ||
| 769988 | 2009-05-02 05:51:00 | Because I want the program to move from one text box to another and do stuff, rather than referring to each one individually. --- Hmm, well I got it to work using: this.Controls[whichPc].Text but I cannot use the same sort of method for an OvalShape or a ProgressBar. I've also tried doing it the FindControl way but my VS2008 does not recognize FindControl as a valid method!? I've added these two lines at the top but still no luck: using System.Windows.Forms; using System.Web.UI; When you say you can't do the same thing with an OvalShape or a ProgressBar, are you casting the returned object to the right datatype? For example: ProgressBar pg = this.Controls["Progressbar1"] as ProgressBar; pg.Width = 100; |
somebody (208) | ||
| 769989 | 2009-05-02 06:19:00 | Well I tried to get an OvalShape to turn green so I did: this.Controls["ovalShape" + counter.toString()].FillColor = Color.Green; Fails on 'FillColor'. this.Controls["progressBar" + counter.toString()].Value = '100'; This fails on 'Value' --- But I can't get the FindControl to work in any way or form :S |
WarNox (8772) | ||
| 769990 | 2009-05-02 06:43:00 | Well I tried to get an OvalShape to turn green so I did: this.Controls["ovalShape" + counter.toString()].FillColor = Color.Green; Fails on 'FillColor'. this.Controls["progressBar" + counter.toString()].Value = '100'; This fails on 'Value' --- But I can't get the FindControl to work in any way or form :S The problem is that the objects in the Controls collection are of the supertype "Control" - and therefore do not have FillColor or Value properties. You need to cast the returned object to the appropriate datatype first - so for example: ProgressBar pg = this.Controls["progressBar" + counter.toString()] as ProgressBar; pg.Value=100; Note the "as ProgressBar" at the end of that first line - what that is doing is it's casting the "Control" returned from the collection into a "ProgressBar" datatype, which you can then access the .Value property with. Just a quick question so I know how much detail/explanation to give next time: how much programming experience have you had (with C#, and in general)? |
somebody (208) | ||
| 769991 | 2009-05-02 06:54:00 | Oh I've done a fair bit of work with c# but it's been a year or two since i last touched it so yeah. I've moved towards networking/servers lately but I like to do a little project every now and then. --- Ok that makes sense. I'm bound to come back here with further issues :) Thanks for all the help. |
WarNox (8772) | ||
| 1 | |||||