Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have a question about the mentioned problem in the title.

I have a main window where I have plenty of labels that I use as buttons. Each label has its own name, like apple, orange, stone, pencil...anything. Each label have a lot of values assigned to it.

So, when I click one of these, a new textbox is being generated. The name of the textbox will be the name of the layer +"txtbox1". So, for example: appletxtbox1. Let's say, I create a second textbox at the same time with the name appletxtbox2. Also, let's say, that the textbox1 contains the weight of the apple in kg, and the textbox2 in lbs. Then, I want to manually refresh the value of the textbox2 which contains the weight in lbs and based on that I want to refresh the value in the textbox1, by using the TextBoxChanged event handler. I try to describe it better by showing the actual part of the code.

What I have tried:

C#
TextBox txtbxAt;
Label labi = sender as Label;
private void setLabelClick(object sender, EventArgs e)
{
txtbxAt = new TextBox();
txtbxAt.Name = labi.Text+"AtBox"; //labi is the name of the clicked label and I add the "atBox" to it, so I create an ID that I can follow later.
txtbxAt.Text = element.SomeValue.ToString(); //I put some value in the textbox; element is from a class I created
txtbxAt.TextChanged += new EventHandler(txtbxAt_TextChanged); //create eventhandler
groupBox3.Controls.Add(txtbxAt); //add textbox to the form

}


So, based on the above code, I can create the textbox dynamically and I can write in it. But, then if there is a similar textbox, I cannot make them "crosstalk". So, when I am in the eventhandler, I cannot refresh the other textbox by writing in it. I tested the eventhandler by just making a pop up messagebox and printing some of the values I use. That was successful, so the eventhandler can see the values I want to work with.

In the textbox's textchanged eventhandler, I want to do something like

C#
(f.Symbol+"AtBox").Text = f.SomeValue.ToString();
, where f is just an element which I know by the labels I click. So when I type in the box1, I want to update box2 and vice-versa.

But of course the compiler will look at me funnily when I try to make the above -obviously- wrong code work.

Can you tell me any suggestions about how to reach a dynamically created textbox and refresh its value based on the value of another dynamically created textbox?

Thank you in advance!
Posted
Updated 10-Aug-19 22:20pm

To do that isn't complicated, but it's going to be made so by what you are trying to do: all you have to do is "find" the right control. That's not complicated in itself: it's in the groupBox3.Controls collection so it's easily available.

The problem is identifying exactly which of the controls in that collection you want to update!
C#
Control[] matched = Controls.Find(f.Symbol + "AtBox", true);
if (matched.Length > 0 && matched[0] is TextBox tb)
    {
    tb.Text = ...
    }
Should do it, I think.
 
Share this answer
 
Comments
Bice90 11-Aug-19 4:57am    
Thank you very much, you can't imagine how much this helped me! It works, as I expected! Seems like I have to dig deeper in this topic about the controls.
Have a great day!
OriginalGriff 11-Aug-19 5:08am    
You're welcome!
Another option would be to use Data Binding, see CodeProject article: Understanding Simple Data Binding[^]
 
Share this answer
 
Comments
Bice90 11-Aug-19 4:58am    
Thank you very much! This article that you suggested was very useful in understanding the deeper details!
Have a great day!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900