How to access all the elements within PhoneApplicationPage? - c#

I'm wondering how can I access an IEnumerable of all the elements in a page derived from PhoneApplicationPage?
If it was ASP.Net WebForms, it would be something like below:
foreach (Control control in this.Controls)
{
//Do something
}
But cannot find the equivalent in Windows Phone 7.5!

You can probably achieve this using the VisualTreeHelper class
Do something like:
LoopThroughControls(LayoutRoot);
private void LoopThroughControls(UIElement parent)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
string childTypeName = child.GetType().ToString();
LoopThroughControls(child);
}
}
}

Related

Populate TreeView from listbox without using SelectedNode C#

I need to populate a TreeView without using SelectedNode property.
This TreeView must contain the items (in most cases - strings) that are in a listbox. These items should be sorted like in a simple binary tree. The root node is the first item. If some of the items is repeated, the count of this item should be increased and the output should be like that ->
-- item 2
| -- private
| - apple
or something like that. In this example, item is the root, apple should be the left child and private - the right.
I found a possible solution here but i have no idea how to do the whole thing :/
Here is my code:
public void populateBaseNodes()
{
int i;
treeView1.Nodes.Clear();
treeView1.BeginUpdate();
for (i = 0; i < L.Count(); i++)
{
if (L[i].level == 0)
{
treeView1.Nodes.Add(L[i].NodeText, L[i].NodeText);
treeView1.Nodes[treeView1.Nodes.Count - 1].Tag = L[i];
}
}
for (i = 0; i < treeView1.Nodes.Count; i++)
populateChilds(treeView1.Nodes[i]);
treeView1.EndUpdate();
treeView1.Refresh();
}
public void populateChilds(TreeNode parentNode)
{
kData parentRed = (kData)parentNode.Tag;
for (int i = parentRed.index + 1; i < L.Count; i++)
{
if (L[i].level == (parentRed.level + 1))
{
parentNode.Nodes.Add(L[i].NodeText, L[i].NodeText);
parentNode.Nodes[parentNode.Nodes.Count - 1].Tag = L[i];
populateChilds(parentNode.Nodes[parentNode.Nodes.Count - 1]);
}
if (L[i].level <= parentRed.level) break;
}
}
And this is my code for the button that will populate the treeview ->
string line;
L = new List<kData>();
for (int i = 0; i < listBox1.Items.Count; i++)
{
line = listBox1.Items[i].ToString();
L.Add(new kData());
L[i].index = i;
L[i].level = i;
ind = line.IndexOf(':');
L[i].NodeText = line.Substring(0, ind);
}
populateBaseNodes();
This is the kData class ->
public class kData
{
public int level;
public int index;
public string NodeText;
};
List<kData> L;
I will appreciate any kind of help :)

Get Text from multiple dynamic generatet Textbox in WPF

I dynamicly generate some Textboxes and put them into a canvas.
private void create_textbox(int number)
{
for(int i = 1; i < number; i++)
{
TextBox MyBox = new TextBox();
darsteller_feld.Name = Convert.ToString("MyBox" + number);
MyBox.Height = 25;
MyBox.Width = 250;
MyBox.Text = "New Box" ;
MyBox_canvas.Children.Add(MyBox);
}
}
So i thought i could pt all Texts into a string [] like this:
for(int i = 0; i < number; i++)
{
ARR[i] = ?Unknown_Textbox_Name?.Text;
}
How can i replace the "?Unknown_Textbox_Name?" Part?
Either you save all your textbox in a list when you create them and the just add them to your array.
Or if you want something more generic
U need to find all the childreen inside your canvas using that method.
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
Then you enumerate over the controls like so
foreach (TextBox tb in FindVisualChildren<TextBox>(MyBox_canvas))
{
ARR.Add(tb.Text);
}
Either use #Moez Rebai solution, or, if your TextBoxes are direct children of your Canvas:
var textboxes= MyBox_canvas.Children.OfType<TextBox>();
Also, you may want to give each TextBox a nema, to distinguish them later:
MyBox.Name = "MyTextBox"+i;

How can i add an icon/image near specific nodes in a treeView1?

This is how i'm adding the TreeNodes and Nodes to the treeView1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace ScrollLabelTest
{
public partial class DisplayResponses : Form
{
private int count;
private List<string> nodesNames = new List<string>();
private List<TreeNode> CurrentNodeMatches = new List<TreeNode>();
public DisplayResponses()
{
InitializeComponent();
label11.Text = "0";
count = 0;
label9.Text = InputLanguage.CurrentInputLanguage.LayoutName;
InputLanguageChanged += DisplayResponses_InputLanguageChanged;
button4.Enabled = false;
addmore();
}
void DisplayResponses_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
{
label9.Text = InputLanguage.CurrentInputLanguage.LayoutName;
}
public void addmore()
{
for (int i = 0; i < ListsExtractions.text1.Count; i++)
{
}
for (int i = 0; i < ListsExtractions.responsers.Count; i++)
{
if (ListsExtractions.responsers[i].Count == 0)
{
ListsExtractions.responsers.RemoveAt(i);
}
}
foreach (List<string> l_branch in ListsExtractions.responsers)
{
TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
{
l_node.Nodes.Add(l_branch[l_count]);
}
}
}
responsers is a List>
What i see in the treeView1 is this:
Now i have another List
for (int i = 0; i < ListsExtractions.text1.Count; i++)
{
}
It's inside the addmore method
I want to compare each item in text1 to a node text and if they identical add near the node the icon
The icon is in my project resources.
For example in the screenshot i added here if the first node is identical/exist in the text1 add near it on the right the icon fro the resources.
EDIT
This is what i tried to do:
public void addmore()
{
for (int i = 0; i < ListsExtractions.responsers.Count; i++)
{
if (ListsExtractions.responsers[i].Count == 0)
{
ListsExtractions.responsers.RemoveAt(i);
}
}
foreach (List<string> l_branch in ListsExtractions.responsers)
{
TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
{
l_node.Nodes.Add(l_branch[l_count]);
}
}
//AddIcons(l_node);
}
And the AddIcons method:
private void AddIcons(TreeNode l_node)
{
for (int i = 0; i < ListsExtractions.text1.Count; i++)
{
for (int x = 0; x < l_node.Nodes.Count; x++)
{
if (l_node.Nodes[x].Text == ListsExtractions.text1[i])
{
//l_node.ImageIndex = 0;
l_node.Nodes[i].ImageIndex = 0;
}
}
}
}
First thing is how do i pass the l_node variable to the AddIcons method ?
Second i tried to move the l_node variable to the top of the form then inside AddIcons it never got inside the comparison:
if (l_node.Nodes[x].Text == ListsExtractions.text1[i])
And i checked some of the items in text1 exist also in the Nodes Text
I added the screenshot before what i want to do is for example if the first top node text == to one of the items in text1 then add icon.
Then move to the next node and compare it to the items in text1
And so on all the nodes only the main nodes not the child nodes.
For example if the first node is:
+-- hello world
And if hello world is in text1 then add the icon to the node hello world
also the format of items in text1 is like this:
In index 0 text: hello world
In index 1 empty: ""
In index 2 text: hello
In index 3 empty: ""
This is how text1 built
EDIT
This is what i tried now:
In the form1 constructor:
InitializeComponent();
il = new ImageList();
il.Images.Add(Properties.Resources.stock_lock);
treeView1.ImageList = il;
label11.Text = "0";
count = 0;
label9.Text = InputLanguage.CurrentInputLanguage.LayoutName;
InputLanguageChanged += DisplayResponses_InputLanguageChanged;
button4.Enabled = false;
addmore();
il is ImageList variable
And the addmore method:
public void addmore()
{
for (int i = 0; i < ListsExtractions.responsers.Count; i++)
{
if (ListsExtractions.responsers[i].Count == 0)
{
ListsExtractions.responsers.RemoveAt(i);
}
}
foreach (List<string> l_branch in ListsExtractions.responsers)
{
TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
if (ListsExtractions.text1.Contains(l_node.Text)) l_node.ImageIndex = 0;
for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
{
TreeNode l_subnode = l_node.Nodes.Add(l_branch[l_count]);
if (ListsExtractions.text1.Contains(l_subnode.Text)) l_subnode.ImageIndex = 0;
}
}
}
But what i get is that every node have the icon near him and not only those who exist by text/name in the List text1.
This is what i get:
Not quite sure if that's what you want, but if you want to do the decoration during the creation of the tree you simply need to do it at the right moment, that is while you have the reference to the new node:
treeView1.ImageList = il;
treeView1.ImageIndex= 999;
treeView1.SelectedImageIndex= 999;
foreach (List<string> l_branch in ListsExtractions.responsers)
{
TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
if (ListsExtractions.Contains (l_node.Text) ) l_node.ImageIndex = 0;
for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
{
TreeNode l_subnode = l_node.Nodes.Add(l_branch[l_count]);
if (ListsExtractions.Contains (l_subnode.Text) ) l_subnode.ImageIndex = 0;
}
}
I make use of the Contains method of List<T> and I grab a handle to the new subnodes when I create them..No need for the AddIcons method.
Edit: There was an error in the original version: If you activate the ImageList by setting one ImageIndex you need to follow a few rules: All nodes will show the first image (Index 0) in the list as both default SelectedImageIndex and default ImageIndex. Therefore you must either include one blank image in the list, all transparent or in the Tree's BackGroundColor on index position 0. Consequently you must set the decorated nodes to show the Image with index 1, both as SelectedImageIndexand as ImageIndex..
..Or you set the default indices (SelectedImageIndexand the ImageIndex) to a number you don't use in your ImageIndex.
Note the gap in the lines where there is no image set! You can try to patch it by creating a default image that fills the gap..:
< === Here is one that works for me.

How to point to control?

i've 6 label controls in a form: label1, label2...label6.
How to 'refer' to the control in a loop like this:
for (i=1;i<=6;i++) {
label[i].text = ...;
}
Thank you
Try,
Label []labels={Label1,Label2,Label3};
Here's another way:
for (int n = 1; n < 4; n++)
{
Control[] Temp = Controls.Find("Label" + n, false);
Temp[0].Text = n.ToString();
}
Let's assume this is WinForms and that your "labels" are controls - the Form has a Controls property, which is a collection of controls associated with that container, so, we ought to be able to use Linq to query this, get the controls of the type we want, then iterate them, as such:
using System.Linq;
var labels = from control in Controls where control is Label select control;
for (i = 1; i <= controls.Count; i++)
{
labels[i].text = i.ToString();
}
A little rough, but you aren't very specific - it should be a decent starting point if nothing else.
EDIT:
OK, I thought I'd take the time to look into it, and Form.Controls doesn't like being used in Linq (in that straightforward way, at least), so as an alternative, this should help:
private List<Label> GetLabels()
{
var result = new List<Label>();
foreach (var control in Controls)
{
if (control is Label)
{
result.Add(control as Label);
}
}
return result;
}
The above method could even be factored in a genericised way rather simply; but then you can proceed:
var labels = GetLabels();
for (int i = 0; i <= labels.Count; i++)
{
labels[i].Text = i.ToString();
}
You can implement something like this:-
int y = 0;
int index = 0;
Label[] labels = new Label[6];
foreach (Student std in StudentList)
{
labels[index] = new Label();
labels[index].Text = std.Name;
labels[index].ForeColor = Color.Red;
labels[index].Location = new Point(0, y);
labels[index].Size = new Size(50, 12);
y = y + 10;
++index;
}
// Add the Label control to the form.
mPanel.Controls.AddRange(labels);

Use a variable to reference a web server control in C#

In C#, I want to assign all 10 Checklistboxes on my page with a value (e.g. 1). What is the syntax so that I can use a variable? I.e. checklistbox(i).selectedIndex = 1;
for (int i = 1; i < 11; i++)
{
checklistbox1.selectedIndex = 1;
checklistbox2.selectedIndex = 1;
checklistbox3.selectedIndex = 1;
...
checklistbox10.selectedIndex = 1;
}
I guess you should use "FindControl" method inorder to do that as shown below.
for (int i = 1; i <= 10; i++)
{
(Page.FindControl("checklistbox" + i) as CheckBox).SelectedIndex = 1;
}
"checklistbox" is assumed as "ID" that is prefixed for all the checkboxes.
Hope this helps!!
You can loop over all the controls on the page and pick out the ones you need as described in this blog post by Kris Steele:
foreach (Control masterControl in Page.Controls)
{
if (masterControl is MasterPage)
{
foreach (Control formControl in masterControl.Controls)
{
if (formControl is System.Web.UI.HtmlControls.HtmlForm)
{
foreach (Control contentControl in formControl.Controls)
{
if (contentControl is ContentPlaceHolder)
{
foreach (Control childControl in contentControl.Controls)
{
if(childControl is CheckBoxList)
{
((CheckBoxList)childControl).SelectedIndex = 1;
}
}
}
}
}
}
}
}
This may not be a good idea if you have a lot of controls on the page.
You should create a List<CheckBoxList>:
var cbs = new List<CheckBoxList> { thingy, otherThingy, ... };
foreach (var cb in cbs)
cb.SelectedIndex = 0;

Categories