0

So I have 70 "nodes" which are all textboxes in WPF and I'm trying to change the value in the textbox from a function call.

I have a function called:

private void changeNode(int row, int column, int cost)  
{    
       int nodeNumber= row * 10 + column;     
       call node"nodeNumber".Text = Convert.String(cost); 
       //example node0.Text = Convert.String(cost); 
}

I determine what node I want to change then call nodeX.Text to change it however I want X to be a variable that I can rather than having to create 70 cases where I call the appropriate textbox.

I saw a couple of ways of doing this with reflection however it seemed to only work if the function had no parameters and also was within the function not a textbox in XAML.

Let me know if there is a simple way to convert say a string "node37" to call node37.Text = cost or something like that.

Timlankey
  • 1
  • 2
  • To a first approximation, if you ever write code that pushes data out to WPF controls you're doing it wrong. You should almost certainly be using data binding for this. – Robert Rossney Feb 22 '11 at 22:26

4 Answers4

1

Sounds like your approach is wrong. Why do you have a set of strings which represent the names of the textboxes? You should instead have in-memory references to TextBox objects. If you have more than one, and you don't know how many there will be, then use an array of TextBox objects instead. You can index into the array with the number that represents the textbox you're looking to interact with.

Avoid the use of reflection, it is completely unnecessary here.

OJ.
  • 28,944
  • 5
  • 56
  • 71
  • +1 - There is no reason to have 70 text boxes manually named. They should have been dynamically created with references saved in an appropriate data structure (an array or `Dictionary`) instead. – Justin Feb 22 '11 at 20:42
  • I would have added them all at runtime but I had created them all within a grid manually. I could create an array with references to it but that will still require me to set all 70 still correct? The way I am currently doing it is I have a function that just uses a switch to return the correct textbox then I can get a reference to that textbox at any time. I was just wondering if instead of creating a function that does that to just have maybe a quick call that would allow me to convert a string into a textbox call. Sorry if this doesn't make sense I figured that it would be the best way. – Timlankey Feb 22 '11 at 21:06
  • Btw the reason why I created all the textboxes manually was because I wanted to do a graphical representation of a problem and creating them manually was neat and easier to do than programmatically. – Timlankey Feb 22 '11 at 21:12
  • I can't see how that would be neat, and certainly not easier than doing it programmatically. I don't envy you if you ever want to refactor that code or change it in some way. I would argue that this should be data-driven with databinding, not programmed in code-behind (and certainly not ever created manually). – OJ. Feb 22 '11 at 23:25
0

I assume you have put names for all your textboxes (you can do this dynamically if you haven't). Then you can use the answers for this question to find the appropriate control by name.

Community
  • 1
  • 1
Ceco
  • 1,586
  • 3
  • 16
  • 23
  • I did name them all already but I feel like searching for the appropriate textbox recursively would be expensive and not worth it. – Timlankey Feb 22 '11 at 21:10
  • I think the answers provided by OJ and Justin are the best you can do given the information available. Maybe if their parent is the same and they are the only children you can search by index from the parent's children collection. – Ceco Feb 22 '11 at 22:08
0

Are all your textboxes children of the same canvas or other control? Loop through the children and add the controls to a dictionary. Parse the name to get the number and use that as the key.

dwidel
  • 1,264
  • 1
  • 12
  • 22
  • That's basically like what I am doing now but instead of having a dictionary I am simply just checking what number it is and returning the Textbox object that I wanted. – Timlankey Feb 22 '11 at 21:08
  • Right, this would have saved you writing that mapping function. I've done it both ways, I prefer creating a dictionary dynamically. That way it never gets out of sync when you suddenly have to have 71 textboxes a year from now and you've forgotten all about that mapping function. – dwidel Feb 22 '11 at 21:42
0

It is always better to use List when you are dealing with Data. Create an ObservableCollection with the DataObjects which you want to load, and now deal with the Data object rather than actual Controls.

In WPF, if you follow the rules, you should not point to the actual object. Check the sample application here : http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource-in.html

I think you will get the approach.

abhishek
  • 2,975
  • 23
  • 38