0

After I create a textBlock in loop

TextBlock tb = new TextBlock();
tb.Text = dataShows.name[i];
tb.TextAlignment = TextAlignment.Center;
tb.Foreground = new SolidColorBrush(Colors.Black);
tb.Name = "tb" + i;
tb.Visibility = Visibility.Hidden;

then i want to show textBlock that name is tb3 same code tb3.Visibility= Visibility.Visible How can i do this with automatic find object name in code?

edited : sorry for my bad gramma. I want to know what code in C# that same this code document.getElementById('#name') in javascript

KPTH
  • 91
  • 1
  • 2
  • 12
  • `then i want to show textBlock that name is tb3 same code` what? I honestly have no clue what you mean by this sentance – EpicKip Oct 19 '17 at 13:10
  • yes, please specify – casiosmu Oct 19 '17 at 13:14
  • I cannot explain you understand with my weak gramma. I want to know command in c# same document.getElementById('name') in javascript – KPTH Oct 19 '17 at 13:14
  • @KittinunPongsukjai Actually from your comment it understand now, you just want to find a control on a form by name thats it. – EpicKip Oct 19 '17 at 13:16
  • @EpicKip I am using wpf window – KPTH Oct 19 '17 at 13:19
  • @KittinunPongsukjai I had an answer but for winforms, for wpf it should be simple too just google how to get control by name wpf – EpicKip Oct 19 '17 at 13:20
  • @EpicKip Thank you for keyword I found it.Thank you very much – KPTH Oct 19 '17 at 13:23
  • No problem ^^ glad to have helped. If the other question essentially asks the same, consider flagging your own question as duplicate of the other – EpicKip Oct 19 '17 at 13:24
  • Possible duplicate of [Find WPF control by Name](https://stackoverflow.com/questions/12238599/find-wpf-control-by-name) – Peter Bons Oct 19 '17 at 13:31

2 Answers2

1

for example store your TextBlock objects in a list: List<TextBlock> which you can iterate

Update:

I think you start from the wrong direction: you can do sth. like javascripts document.getElementByID(), its called Reflection; but if you are a beginner I cannot recommend this.

You can get all Controls in a Form/Control by searching through myControl.Controls array, and check their names/types.

But the easiest way would be a list to hold the TextBlock objects:

List<TextBlock> a = new List<textBlock>();

// in the creating loop:
a.Add(tb);

// access using Linq:
textBlock res = a.Find(c=>c.Name=="thename");
// is roughly the same as
foreach(TextBlock b in a)
    if(b.Name=="thename") {
        res = b;
        break;
    }

Another way

casiosmu
  • 797
  • 1
  • 8
  • 22
0

C# is not like JavaScript

You used the code document.getElementById('#name') to show your intent. Let us analyze what exactly JS does when it it executes this line. It searches your entire DOM for an object with the ID name and returns a reference to it.

C# does not operate like that. There is no easily searchable pile of objects somewhere, where you just need to perform a query and get the object you want. If you want to keep a list of objects, you have to make that list yourself.

I assume you are creating several TextBlock-objects. When you do that, you need to add them to a list, which you can then query. Here is a sample implementation:

List<TextBlock> textBlocks = new List<TextBlock>();

private TextBlock GetTextBlockByName(string name)
{
    if(name == null) return null; //I assume that a TextBlock needs to have a name.
    // Returns the TextBlock if it was found or null if not.
    // Throws an Exception if more than one TextBlock has the same name
    return this.textBlocks.Find(t => t.Name == name).SingleOrDefault();

}

A friendly hint for you, since I assume you are not extremely proficient with C# yet: When you come to a new programming language, don't assume that it follows the exact same paradigms that other languages you know follow, even if they occassionally have similarities. The whole reason we have so many different languages is that some things are easy in one, and difficult in another.

MechMK1
  • 3,278
  • 7
  • 37
  • 55
  • You can find a control by name, don't make it harder than it is by interpreting the question even more complicated – EpicKip Oct 19 '17 at 13:33
  • Ahh, I did not know that, since I am not working with WinForms too often. Feel free to edit my answer and suggest an easier example – MechMK1 Oct 19 '17 at 13:34
  • Its not winforms, its wpf and the suggested answer (https://stackoverflow.com/a/46830707/2885376) by Casiosmu is fine, I would edit if I had more knowledge of WPF (and could answer better then current answer). But OP already moved on by googling: " how to get control by name wpf" – EpicKip Oct 19 '17 at 13:36