C# (UWP) - Access object dynamically from string-variable - c#

So I've written some code that creates TextBlocks from a list of strings by calling a for loop:
List<string> menuPages = new List<string>() { "Home", "Media", "Settings" };
//method called from constructor:
private void createHeaders ()
{
for (int i=0; i<menuPages.Count; i++)
{
TextBlock iheader = new TextBlock();
iheader.Name = menuPages[i];
iheader.Text = menuPages[i];
if (i==pageIndex)
{ iheader.FontSize = 36; }
else
{ iheader.FontSize = 32; }
stacky.Children.Add(iheader); //Adding button to stack panel
}
}
Now I've been writing another method that would cycle through each TextBlock in a loop and change the text to whatever I intend. I'd gotten a foreach loop working for the stackPanel children: (TextBlock tBlock in stacky.Children)
but I need to work with an indexed for loop. The code below is how I WANT to achieve this:
//Re-render headers
for (int i = 0; i < menuPages.Count; i++)
{
//TextBlock menuPages[i].text = "foo";
}
Now of course the syntax above doesn't work so my question is, how can I address the TextBlocks from the strings in a list?

Just have your textblocks created in a list. So, you can manipulate easily with indexed forloop.
List<string> menuPages = new List<string>() { "Home", "Media", "Settings" };
List<TextBlock> textBlocks = new List<TextBlock>();
public MainPage()
{
this.InitializeComponent();
createHeaders();
}
private void createHeaders()
{
for (int i = 0; i < menuPages.Count; i++)
{
TextBlock iheader = new TextBlock();
iheader.Name = menuPages[i];
iheader.Text = menuPages[i];
iheader.FontSize = 32;
textBlocks.Add(iheader);
Stacky.Children.Add(iheader);
}
}
private void change_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < textBlocks.Count; i++)
{
textBlocks[i].Text = "foo";
}
}

If the StackPanel only contains the TextBlock elements you are adding dynamically, you can access them this way as well:
foreach (var textBlock in Stacky.Children.OfType<TextBlock>())
{
textBlock.Text = "something";
}
This approach uses the OfType<T> LINQ extension method which filters the input collection by the specified type, so it only returns those children of Stacky that are a TextBlock.
If you have more content, in the StackPanel, then #Vignesh G's answer is the way to go.

Related

Winforms insert image into ListView / ImageList at index

Winforms, C#, VS2017
ImageList does not have an Insert method (however ListViewItemCollection does). I have tried a few different ways to insert a new image into the middle of a ListView and it's LargeImageList, but not getting it to work quite properly.
Anyone have any tried and true code that works properly?
This is what I have, but the images don't get synced properly to the items in the list.
protected void InsertThumbnail(string key, string keySelected)
{
var newImageList = new ImageList()
{
ImageSize = new Size(thumbWidth, thumbHeight)
};
var itemNew = new ListViewItem();
var foundSelected = false;
//lvAllPages.BeginUpdate();
for (int i = 0; i < lvAllPages.Items.Count; i++)
{
var item = lvAllPages.Items[i];
newImageList.Images.Add(item.Tag.ToString(), lvAllPages.LargeImageList.Images[i]);
if (item.Tag.ToString() == keySelected)
{
var image = batch.GetThumbnail(key);
newImageList.Images.Add(key, image);
itemNew = new ListViewItem()
{
BackColor = Color.Aquamarine,
ImageIndex = i,
Tag = key,
};
if (isLocal)
itemNew.Text = $"{GetFileName(key)} (insert) - {itemNew.ImageIndex}";
foundSelected = true;
}
if (foundSelected)
{
item.ImageIndex = item.ImageIndex + 1;
if (isLocal)
item.Text = $"{GetFileName(item.Tag.ToString())} - {item.ImageIndex}";
}
}
lvAllPages.LargeImageList.Dispose();
lvAllPages.LargeImageList = newImageList;
lvAllPages.Items.Insert(itemNew.ImageIndex, itemNew);
}
One more related thing, but not pertinent to the problems I am having. For anyone looking at this question and having similar issues, this helped with the issue of sorting items after inserting a new one. Default behavior when you insert a new ListViewItem at a given index, it will appear at the bottom of the list. I found this handy class to keep items sorted by index, which solved that problem:
class CompareByIndex : IComparer
{
private readonly ListView _listView;
public CompareByIndex(ListView listView)
{
this._listView = listView;
}
public int Compare(object x, object y)
{
int i = this._listView.Items.IndexOf((ListViewItem)x);
int j = this._listView.Items.IndexOf((ListViewItem)y);
return i - j;
}
}
And in the form load:
lvAllPages.ListViewItemSorter = new CompareByIndex(lvAllPages);
Obviously, that's a design decision. ImageList.Images is a ImageCollection and as such, it implements the IList interface.
Unfortunately, the Insert() method is allowed to throw a NotSupportedException. And that's what the list will do when used like a IList:
((IList)imageList.Images).Insert(5, new Bitmap(10,10));
System.NotSupportedException: 'Specified method is not supported.'
In order to have the images shown in a specific order, use the Add() method which takes the key:
imageList.Images.Add("1", new Bitmap(100,100));
That should also enable you to replace the image:
imageList.Images.RemoveByKey("1");
imageList.Images.Add("1", new Bitmap(200,200));
For that to work, you need to set the Sorting property:
listView1.Sorting = SortOrder.Ascending;
For storing additional information like path etc. use anotther data structure with the same key.
Here's the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ImageList imageList = new ImageList();
Dictionary<string, Metadata> metadata = new Dictionary<string, Metadata>();
private string dir = #"H:\temp";
private void button1_Click(object sender, EventArgs e)
{
// You would set this in the designer, probably
listView1.Sorting = SortOrder.Ascending;
listView1.View = View.LargeIcon;
listView1.LargeImageList = imageList;
// Make sure we start from the beginning
listView1.Items.Clear();
imageList.Images.Clear();
metadata.Clear();
// Add items
for (int i = 0; i < 10; i++)
{
var filename = "1 ("+(i+1)+").png"; // Just strange names I have
var fullFileName = Path.Combine(dir, filename);
imageList.Images.Add(i.ToString(), Bitmap.FromFile(fullFileName));
metadata.Add(i.ToString(), new Metadata{Path = fullFileName});
listView1.Items.Add(i.ToString(), "Image " + i, i.ToString());
}
// Update view
listView1.Refresh();
listView1.Invalidate();
}
private void button2_Click(object sender, EventArgs e)
{
for (int i = 3; i < 6; i++)
{
var filename = "1 ("+(i+2)+").png";
var fullFileName = Path.Combine(dir, filename);
// Change image
imageList.Images.RemoveByKey(i.ToString());
imageList.Images.Add(i.ToString(), Bitmap.FromFile(fullFileName));
// Match metadata and image
metadata[i.ToString()] = new Metadata{Path = fullFileName};
}
listView1.Refresh();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
var key = listView1.SelectedItems[0].ImageKey;
label1.Text = metadata[key].Path;
}
else
{
label1.Text = "No image selected";
}
}
}
internal class Metadata
{
internal string Path;
}

Looping through labels with similar name C#

I want to change the background of some labels depending on what is written on a text file:
private void Form3_Load(object sender, EventArgs e)
{
string[] words = new string[7];
StreamReader read = new StreamReader(path);
while(!read.EndOfStream)
{
string line = read.ReadLine();
words = line.Split(';');
if(words[6] == "no")
{
//-----What I have to write here---
}
}
read.Close();
}
There are over 50 labels named "lbl101","lbl102","....","lbl150"
try it:
if(words[6] == "no")
{
int count = 150;
for (int a = 1 ; a < count; a++)
{
Label currentLabel = (Label)this.Controls.Find("lbl"+a,true)[0];
//change color of currentLabel
}
}
There's the working solution:
private void Form3_Load(object sender, EventArgs e)
{
int count = 101;
string[] words = new string[7];
StreamReader read = new StreamReader(pathRooms);
while(!read.EndOfStream)
{
string line = read.ReadLine();
words = line.Split(';');
if (words[6] == "no")
{
Label currentLabel = (Label)this.Controls.Find("lbl" + count, true)[0];
currentLabel.BackColor = Color.Yellow;
}
count = count + 1;
}
read.Close();
}
You can iterate all over them using OfType<T>() method on Controls collection of form like:
if(words[6] == "no")
{
foreach(var label in this.Controls.OfType<Label>().Where(x=>x.Name.Contains("lbl")))
{
label.Text = "Some Text";
}
}
This will only work on the labels that are direct child of form, labels nested inside other user controls or nested panels will not be affected, for that you have to do it recursively.
Loop through the Controls collection of the form checking for Label objects. Then, amend accordingly as per the specified value.
1.) Create a List with all the labels.
Label lbl101 = new Label();
Label lbl102 = new Label();
...
List<Label> labels = new List<Label>()
{
lbl101,
lbl102
...
};
2.) If your words[] string is the name of the color you can write:
if(words[6] == "no")
{
System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml(words[..]);
foreach(Label l in Labels)
{
l.BackColor = myColor;
}
}

C# Create Tracks Parents which Depend on Children

I'm trying to create tracks of Parent that have more than a child and put theme in dynamic ListBox
i have this ParentActivity table"
tblParentActivity
and I want to make tracks depend on ParentActivity table to be in ListBox like this:
Tracks in ListBox
the code so far:
private void TrackingActivity(long ParentActivityID)
{
DataTable dtActiveChild = objA.SelectActivityChild(ParentActivityID);
ListBox lstBox = new ListBox();
lstBox.ID = "lstTrack" + ParentActivityID.ToString();
lstBox.Width = 200;
pnlTrack.Controls.Add(lstBox);
for (int i = 0; i < dtActiveChild.Rows.Count; i++)
{
long ActivityChildID = Convert.ToInt64(dtActiveChild.Rows[i]["ActivityID"].ToString());
string ActivityChildName = dtActiveChild.Rows[i]["ActivityName"].ToString();
lstBox.Items.Add(new ListItem (ActivityChildName.ToString(),ActivityChildID.ToString()));
DataTable dtBrotherActivity = objA.selectBrotherActivity(ActivityChildID);
if (dtBrotherActivity.Rows.Count > 0)
{
TrackingActivity(ActivityChildID);
}
}
}
for example ParentActivityID=1;
selectBrotherActivity is query to get another child of it parent
it gave me distribution like this:
|1|2 3|4 5|7|9|10|7|9|10|6|8|9|10|
which || means ListBox
Ok thanks to everyone I found it:
protected void btnCreateTrack_Click(object sender, EventArgs e)
{
ListBox lstBoxParent = new ListBox();
lstBoxParent.ID = "lstTrack1";
lstBoxParent.Width = 200;
lstBoxParent.Height = 200;
pnlTrack.Controls.Add(lstBoxParent);
TrackingActivity(1, lstBoxParent );
}
private void TrackingActivity(long ParentActivityID, ListBox lstBoxParent)
{
chk:
DataTable dtActiveChild = objWFAI.SelectActivityChild(ParentActivityID);
string ActivityName = objWFA.CurrentActivityName(ParentActivityID);
lstBoxParent.Items.Add(new ListItem(ActivityName, ParentActivityID.ToString()));
for (int i = dtActiveChild .Rows.Count - 1; i >= 0; i--)
{
long ActivityChildID = Convert.ToInt64(dtActiveChild .Rows[i][" ActivityID"].ToString());
if (i != 0)
{
ListBox lstBoxChild = new ListBox();
lstBoxChild.ID = "lstTrack" + ActivityChildID.ToString();
lstBoxChild.Width = 200;
lstBoxChild.Height = 200;
pnlTrack.Controls.Add(lstBoxChild);
for (int p = 0; p < lstBoxParent.Items.Count; p++)
{
lstBoxChild.Items.Add(new ListItem(lstBoxParent.Items[p].Text, lstBoxParent.Items[p].Value));
}
TrackingActivity(ActivityChildID, lstBoxChild);
}
else
{
ParentActivityID = ActivityChildID ;
goto chk;
}
}

Iterating through fields?

First of all, I'm not entirely sure how to phrase this or what to search for if someone asked before.
Say I have multiple labels: label1, label2, label3, label4, etc...
Now, I know this works in PHP so I'm wondering if there is a way to do this in C# -
Can I somehow iterate through these labels to set their values in a loop?
i.e.
string[] something = new string[3] { "text", "text", "text" };
for (int i = 0; i < something.length; i++)
{
labels(i).Text = something[i];
}
You can use Controls.Find() method for finding the Label Control by its Control Name.
Try This:
for (int i = 0; i < something.length; i++)
{
((Label) Controls.Find("lablel"+i,true)[0]).Text = something[i];
}
You can get an enumerable list of labels from the Controls collection by using .OfType<>
e.g.
foreach(Label l in this.Controls.OfType<Label>())
{
...
}
Or simply find all the labels without needing to know their names:
foreach (Control ctrl in this.Controls)
{
Label label = ctrl as Label;
if (label != null)
{
label.Text = "";
}
}
You could use Controls.Find to find all references:
for (int i = 0; i < something.length; i++)
{
var lbl = this.Controls.Find("lable" + i, true);
if(lbl.Length > 0)
((Label)lbl[0]).Text = something[i];
}
Another approach using LINQ (which doesn't search recursively as opposed to Find):
for (int i = 0; i < something.Length; i++)
{
Label lbl = this.Controls.Cast<Label>()
.FirstOrDefault(l => l.Name.Equals("lable" + i, StringComparison.InvariantCultureIgnoreCase));
if(lbl != null) lbl.Text = something[i];
}
However, i would not call this good practise. Your array and the labels are directly related to each other. What hapens if you change the array but forget to change the labels?
You should use a different control like ListBox or DataGridView or create the labels dynamically according to the size of the array.

How to loop through listBox and remove words before a certain character?

How would I use a for loop to go through a listBox and remove words before a certain character?
For example if my listBox contains items resembling the following:
','ae5e87df42fa5921
And I want to remove everything before the ',' how would I do so with each item in the listBox?
Thanks!
In a for loop you can simply call item = item.Substring(item.LastIndexOf("','"))
Like so:
ListBox lb = new ListBox();
lb.Items.Add("12341','2341");
lb.Items.Add("123415','112341");
lb.Items.Add("543225','11234134");
for (int i = 0; i < lb.Items.Count; i++) {
string item = lb.Items[i] as string;
item = item.Substring(item.LastIndexOf("','"));
lb.Items[i] = item;
}
Edit:
Here you have full example, which works. Just add the ListBox and the button, and assign the event to it's Click handler:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
lb.Items.Add("12341','2341");
lb.Items.Add("123415','112341");
lb.Items.Add("543225','11234134");
}
private void button1_Click(object sender, EventArgs e) {
for (int i = 0; i < lb.Items.Count; i++) {
string item = lb.Items[i] as string;
item = item.Substring(item.LastIndexOf("','"));
lb.Items[i] = item;
}
}
}
It IS working.

Categories