How to remove all children in HtmlElement with Silverlight/C# - c#

So I have this Silverlight application that calls some reports(.rdlc) via HTML form submit.
This form is universal, so I use it to call all reports with it.
Now I want to clear the input elements in it everytime the form has been submited.
So I want a clear form everytime I want to call a new report with no children in it.
Can anyone explain why this is not working when there are more than 5(or any larger amount of inputs) inputs in the form?
public static void RemoveInputsFromForm(HtmlElement Form)
{
if (HtmlPage.Document.GetElementById(Form.Id) != null)
{
foreach (HtmlElement element in Form.Children)
{
if (element.Id != string.Empty)
{
element.Parent.RemoveChild(element);
}
}
}
}
The form has it's ID and every input has an ID, it removes some inputs randomly, others stay in the form.
This is the method that inserts the input elements into that form:
public static void AddInputToForm(HtmlElement Form, string Name, object Data)
{
if (Data != null && Data.ToString() != string.Empty)
{
var input = HtmlPage.Document.CreateElement("input");
input.SetProperty("type", "hidden");
input.SetProperty("value", Data.ToString());
input.SetProperty("name", Name);
input.SetProperty("id", Name);
Form.AppendChild(input);
}
}
This is the method that I use now to remove form children, it is the same as AddInputToForm method only that it removes inputs
public static void RemoveInputFromForm(HtmlElement Form, string Name)
{
if (HtmlPage.Document.GetElementById(Name) != null)
{
Form.RemoveChild(HtmlPage.Document.GetElementById(Name));
}
}
This is the method that gets my form
public static HtmlElement GetForm(string formUrl)
{
//in the html that holds the silverlight object, you must have a form named "reportform"
//<form id="reportform" action="" method="post" target="_blank" style="visibility: collapse" />
HtmlElement functionReturnValue = null;
//set form
functionReturnValue = HtmlPage.Document.GetElementById("reportform");
//set form action
functionReturnValue.SetProperty("action", formUrl);
return functionReturnValue;
}
To reproduce the code (SomeElement is an object with string properties):
//get form
var Form = GetForm(ApplicationUrl + #"Reports\Report.aspx");
//add parameters to form
AddInputToForm(Form, "id1", SomeElement.Id1Value);
AddInputToForm(Form, "id2", SomeElement.Id2Value);
AddInputToForm(Form, "id3", SomeElement.Id3Value);
AddInputToForm(Form, "id4", SomeElement.Id4Value);
AddInputToForm(Form, "id5", SomeElement.Id5Value);
AddInputToForm(Form, "id6", SomeElement.Id6Value);
AddInputToForm(Form, "id7", SomeElement.Id7Value);
AddInputToForm(Form, "id8", SomeElement.Id8Value);
AddInputToForm(Form, "id9", SomeElement.Id9Value);
//submit form
Form.Invoke("submit");
//clean report
RemoveInputFromForm(Form, "id1");
RemoveInputFromForm(Form, "id2");
RemoveInputFromForm(Form, "id3");
RemoveInputFromForm(Form, "id4");
RemoveInputFromForm(Form, "id5");
RemoveInputFromForm(Form, "id6");
RemoveInputFromForm(Form, "id7");
RemoveInputFromForm(Form, "id8");
RemoveInputFromForm(Form, "id9");
So you see I have to remove every child I added with as many calls as there are inputs in the form, I would like to have only one call and clean that form.
Thanks in advance to any given solution.

You should never add or remove elements while inside a foreach loop, as the collection changes and invalidates the loop. Rather add all items to be removed in a separate list and then remove them in a second loop. (I haven't tested it because I do not have relevant sample code, but this is a common mistake with foreach loops.)
List<HtmlElement> toRemove = new List<HtmlElement>();
foreach (HtmlElement element in Form.Children)
{
if (element.Id != string.Empty)
{
toRemove.Add(element);
}
}
foreach (HtmlElement element in toRemove)
{
Form.RemoveChild(element);
}
Alternatively, using your second approach, you can always add all your IDs to a list, and then add a RemoveAllFormInputs() method which loops over these elements.

Peet has provided the correct answer, however some may prefer a Linq based approach that slims the code a litte.
foreach (HtmlElement element in Form.Children
.OfType<HtmlElement>
.Where(e => !String.IsNullOrEmpty(e.Id))
.ToList() )
{
Form.RemoveChild(element);
}

Related

Is it possible to retrieve one hidden form and close another hidden form

I am using the following code to close and open the hidden form(by checking open appl). Is it possible to retrieve one hidden form and close another where both the forms corresponds to same one (say form1 hidden at different periods at runtime)
var frm = Application.OpenForms.OfType<Form>().FirstOrDefault(x => x.GetType() == typeof(FrmAdd_To_Cart));
if (frm != null)
{
frm.Close(); or frm.show();
}
You could use the Tag property to recognize one form from the other
FrmAdd_To_Cart formToClose = null;
var frmCartList = Application.OpenForms.OfType<FrmAdd_To_Cart>();
if (frmCartList != null)
{
foreach(FrmAdd_To_Cart frm in frmCartList)
{
// Your logic could be based on the value that you set
// in the Tag property when you create the form
// For example you could have a CustomerID stored in the Tag
// int customerID = Convert.ToInt32(frm.Tag);
// But probably it is better to have custom public property
// in the definition of your FrmAdd_To_Cart form class like
// if(frm.CustomerID == CurrentCustomer.ID)
// .....
// Or if you want to close the form that you identify with the tag
if (this.lblBil.Text == frm.Tag.ToString())
{
formToClose = frm;
break; // exit the loop and then close
// Can't do this here because this will change
// the iterating collection and this is not allowed
// frm.Close();
}
}
if (formToClose != null)
formToClose.Close();
}
Notice that you could use the OfType extension to get only the forms that you are interested in. Also this means that you have an IEnumerable returned and thus you need to loop using the foreach.

Loop through all controls of a Form, even those in GroupBoxes

I'd like to add an event to all TextBoxes on my Form:
foreach (Control C in this.Controls)
{
if (C.GetType() == typeof(System.Windows.Forms.TextBox))
{
C.TextChanged += new EventHandler(C_TextChanged);
}
}
The problem is that they are stored in several GroupBoxes and my loop doesn't see them. I could loop through controls of each GroupBox individually but is it possible to do it all in a simple way in one loop?
The Controls collection of Forms and container controls contains only the immediate children. In order to get all the controls, you need to traverse the controls tree and to apply this operation recursively
private void AddTextChangedHandler(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType() == typeof(TextBox)) {
c.TextChanged += new EventHandler(C_TextChanged);
} else {
AddTextChangedHandler(c);
}
}
}
Note: The form derives (indirectly) from Control as well and all controls have a Controls collection. So you can call the method like this in your form:
AddTextChangedHandler(this);
A more general solution would be to create an extension method that applies an action recursively to all controls. In a static class (e.g. WinFormsExtensions) add this method:
public static void ForAllControls(this Control parent, Action<Control> action)
{
foreach (Control c in parent.Controls) {
action(c);
ForAllControls(c, action);
}
}
The static classes namespace must be "visible", i.e., add an appropriate using declaration if it is in another namespace.
Then you can call it like this, where this is the form; you can also replace this by a form or control variable whose nested controls have to be affected:
this.ForAllControls(c =>
{
if (c.GetType() == typeof(TextBox)) {
c.TextChanged += C_TextChanged;
}
});
A few simple, general purpose tools make this problem very straightforward. We can create a simple method that will traverse an entire control's tree, returning a sequence of all of it's children, all of their children, and so on, covering all controls, not just to a fixed depth. We could use recursion, but by avoiding recursion it will perform better.
public static IEnumerable<Control> GetAllChildren(this Control root)
{
var stack = new Stack<Control>();
stack.Push(root);
while (stack.Any())
{
var next = stack.Pop();
foreach (Control child in next.Controls)
stack.Push(child);
yield return next;
}
}
Using this we can get all of the children, filter out those of the type we need, and then attach the handler very easily:
foreach(var textbox in GetAllChildren().OfType<Textbox>())
textbox.TextChanged += C_TextChanged;
Try this
AllSubControls(this).OfType<TextBox>().ToList()
.ForEach(o => o.TextChanged += C_TextChanged);
where AllSubControls is
private static IEnumerable<Control> AllSubControls(Control control)
=> Enumerable.Repeat(control, 1)
.Union(control.Controls.OfType<Control>()
.SelectMany(AllSubControls)
);
LINQ is great!
Haven't seen anyone using linq and/or yield so here goes:
public static class UtilitiesX {
public static IEnumerable<Control> GetEntireControlsTree(this Control rootControl)
{
yield return rootControl;
foreach (var childControl in rootControl.Controls.Cast<Control>().SelectMany(x => x.GetEntireControlsTree()))
{
yield return childControl;
}
}
public static void ForEach<T>(this IEnumerable<T> en, Action<T> action)
{
foreach (var obj in en) action(obj);
}
}
You may then use it to your heart's desire:
someControl.GetEntireControlsTree().OfType<TextBox>().ForEach(x => x.Click += someHandler);
As you have stated, you will have to go deeper than just cycling over each element in your form. This, unfortunately, implies the use of a nested loop.
In the first loop, cycle through each element. IF the element is of type GroupBox, then you know you'll need to cycle through each element inside the groupbox, before continuing; else add the event as normal.
You seem to have a decent grasp of C# so I won't give you any code; purely to ensure you develop all the important concepts that are involved in problem solving :)
you can only loop through open forms in windows forms using form collection for example to set windows start position for all open forms:
public static void setStartPosition()
{
FormCollection fc = Application.OpenForms;
foreach(Form f in fc)
{
f.StartPosition = FormStartPosition.CenterScreen;
}
}
I know that this is an older topic, but would say the code snippet from http://backstreet.ch/coding/code-snippets/mit-c-rekursiv-durch-form-controls-loopen/ is a clever solution for this problem.
It uses an extension method for ControlCollection.
public static void ApplyToAll<T>(this Control.ControlCollection controlCollection, string tagFilter, Action action)
{
foreach (Control control in controlCollection)
{
if (!string.IsNullOrEmpty(tagFilter))
{
if (control.Tag == null)
{
control.Tag = "";
}
if (!string.IsNullOrEmpty(tagFilter) && control.Tag.ToString() == tagFilter && control is T)
{
action(control);
}
}
else
{
if (control is T)
{
action(control);
}
}
if (control.Controls != null && control.Controls.Count > 0)
{
ApplyToAll(control.Controls, tagFilter, action);
}
}
}
Now, to assign an event to all the TextBox controls you can write a statement like (where 'this' is the form):
this.Controls.ApplyToAll<TextBox>("", control =>
{
control.TextChanged += SomeEvent
});
Optionally you can filter the controls by their tags.
Since the Question regarding "Adding an Event to your TextBoxes"; was already answered; I'm providing some explanation and adding an iteration alternative using a for loop instead.
Problem:
Being Unable to Get Controls Inside a Container.
Solution:
In order to retrieve the Controls inside a Container you have to specify the Container that Contains the Controls you wish to access to.
Therefore your loop must check the Controls inside a Container.
Otherwise your loop will not find the Controls inside a Container.
i.e:
foreach (Control control in myContainer.Controls)
{
if (control is TextBox) { /* Do Something */ }
}
In case you have several Containers:
Initially iterate the Containers.
Then iterate over the controls inside the container (the container found in the initial iteration).
Pseudo Code Example on How to use a for Loop Instead:
/// <summary> Iterate Controls Inside a Container using a for Loop. </summary>
public void IterateOverControlsIncontainer()
{
// Iterate Controls Inside a Container (i.e: a Panel Container)
for (int i = 0; i < myContainer.Controls.Count; i++)
{
// Get Container Control by Current Iteration Index
// Note:
// You don't need to dispose or set a variable to null.
// The ".NET" GabageCollector (GC); will clear up any unreferenced classes when a method ends in it's own time.
Control control = myContainer.Controls[i];
// Perform your Comparison
if (control is TextBox)
{
// Control Iteration Test.
// Shall Display a MessageBox for Each Matching Control in Specified Container.
MessageBox.Show("Control Name: " + control.Name);
}
}
}
Updated answer:
I needed to disable all the controls in a form, including groupboxes. This code worked:
private void AlterControlsEnable(bool ControlEnabled)
{
foreach (Control i in Controls)
i.Enabled = ControlEnabled;
}

Filter ListBox with TextBox in realtime

I am trying to filter an listbox with text from a textbox, realTime.
Here is the code:
private void SrchBox_TextChanged_1(object sender, EventArgs e)
{
var registrationsList = registrationListBox.Items.Cast<String>().ToList();
registrationListBox.BeginUpdate();
registrationListBox.Items.Clear();
foreach (string str in registrationsList)
{
if (str.Contains(SrchBox.Text))
{
registrationListBox.Items.Add(str);
}
}
registrationListBox.EndUpdate();
}
Here are the issues:
When I run the program i get this error: Object reference not set to an instance of an object
If I hit backspace, my initial list is not shown anymore. This is because my actual list of items is now reduced, but how can I achieve this?
Can you point me in the right direction?
It's hard to deduct just from the code, but I presume your filtering problem born from the different aspects:
a) You need a Model of the data shown on ListBox. You need a colleciton of "Items" which you hold somewhere (Dictionary, DataBase, XML, BinaryFile, Collection), some kind of Store in short.
To show the data on UI you always pick the data from that Store, filter it and put it on UI.
b) After the first point your filtering code can look like this (a pseudocode)
var registrationsList = DataStore.ToList(); //return original data from Store
registrationListBox.BeginUpdate();
registrationListBox.Items.Clear();
if(!string.IsNullOrEmpty(SrchBox.Text))
{
foreach (string str in registrationsList)
{
if (str.Contains(SrchBox.Text))
{
registrationListBox.Items.Add(str);
}
}
}
else
registrationListBox.Items.AddRange(registrationsList); //there is no any filter string, so add all data we have in Store
registrationListBox.EndUpdate();
Hope this helps.
Something like this might work for you:
var itemList = registrationListBox.Items.Cast<string>().ToList();
if (itemList.Count > 0)
{
//clear the items from the list
registrationListBox.Items.Clear();
//filter the items and add them to the list
registrationListBox.Items.AddRange(
itemList.Where(i => i.Contains(SrchBox.Text)).ToArray());
}
Yes that was the answer to filtering. (modified a bit). I had the info in a text file. This is what worked for me
FileInfo registrationsText = new FileInfo(#"name_temp.txt");
StreamReader registrationsSR = registrationsText.OpenText();
var registrationsList = registrationListBox.Items.Cast<string>().ToList();
registrationListBox.BeginUpdate();
registrationListBox.Items.Clear();
if (!string.IsNullOrEmpty(SrchBox.Text))
{
foreach (string str in registrationsList)
{
if (str.Contains(SrchBox.Text))
{
registrationListBox.Items.Add(str);
}
}
}
else
while (!registrationsSR.EndOfStream)
{
registrationListBox.Items.Add(registrationsSR.ReadLine());
}
registrationListBox.EndUpdate();
It seems that the error:
Object reference not set to an instance of an object
is from somewhere else in my code, can't put my finger on it.
If able, store everything in a dictionary and just populate it from there.
public partial class myForm : Form
{
private Dictionary<string, string> myDictionary = new Dictionary<string, string>();
//constructor. populates the items. Assumes there is a listbox (myListbox) and a textbox (myTextbox), named respectively
public myForm()
{
InitializeComponent();
myDictionary.Add("key1", "item1");
myDictionary.Add("key2", "My Item");
myDictionary.Add("key3", "A Thing");
//populate the listbox with everything in the dictionary
foreach (string s in myDictionary.Values)
myListbox.Add(s);
}
//make sure to connect this to the textbox change event
private void myTextBox_TextChanged(object sender, EventArgs e)
{
myListbox.BeginUpdate();
myListbox.Items.Clear();
foreach (string s in myDictionary.Values)
{
if (s.Contains(myListbox.Text))
myListbox.Items.Add(s);
}
myListbox.EndUpdate();
}
}
I would do it like this:
private List<string> registrationsList;
private void SrchBox_TextChanged_1(object sender, EventArgs e)
{
registrationListBox.BeginUpdate();
registrationListBox.Items.Clear();
var filteredList = registrationList.Where(rl => rl.Contains(SrchBox.Text))
registrationListBox.Items.AddRange();
registrationListBox.EndUpdate();
}
Just remember to populate registrationsList the first time you fill your listbox.
Hope this helps.
it was a very hard issue for me, but I found a workaround (not so simple) that works fine for me.
on aspx page:
<input id="ss" type="text" oninput="writeFilterValue()"/>
<asp:HiddenField ID="hf1" runat="server" Value="" ClientIDMode="Static" />
I need HTML input type because of "oninput" function, that is not availiable on classic asp.net controls. The writeFilterValue() function causes a postback that filters values of a given ListBox (in code-behind).
I've defined this two javascript function:
<script type="text/javascript">
function writeFilterValue() {
var bla = document.getElementById("ss").value;
$("#hf1").val(bla)
__doPostBack();
}
function setTboxValue(s) {
document.getElementById('ss').value = s;
document.getElementById('ss').focus();
}
</script>
You can now use postback on code-behind to capture hf1 value, every time some single Character is typed on inputbox.
On code-behind:
If IsPostBack Then
FiltraLbox(hf1.Value)
End If
The function FiltraLbox(hf1.Value) changes datasource of Listbox, and rebind it:
Public Sub FiltraLbox(ByVal hf As String)
If hf <> "" Then
' change datasource here, that depends on hf value,
ListBox1.DataBind()
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "text", setTboxValue('" + hf + "');", True)
End If
End Sub
At the end I call the function setTboxValue(), that rewrites the input text value lost on postback, and puts the focus on it.
Enjoy it.

Filling in Text of a VBulliten Private message

Im trying to submit a private message in a webforum through C#.
I can fill in every component except for the message box itself:
nk
The usual method of setting the innertext element of the textarea does nothing at all, it works for the rest of the page but not this?? I have no idea why and i can confirm the code is correctly identifying this area.
I can only imagine something else truly controls the display and submission value.
I have found :
When I set the value to anything but 0, the message will post but the text is missing each time.
Any ideas?
I don't know what is your favorite method to access a web page, but you can create a dummy WebBrowser form, then use a function like it:
void SetText(string attribute, string attName, string value)
{
HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement currentTag in tagsCollection)
{
if (currentTag.GetAttribute(attribute).Equals(attName))
currentTag.SetAttribute("value", value);
}
}
void CheckBox(string attribute, string attName, string value)
{
// Get a collection of all the tags with name "input";
HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement currentTag in tagsCollection)
{
if (currentTag.GetAttribute(attribute).Equals(attName))
currentTag.SetAttribute("checked", value);
}
}
void ClickButton(string attribute, string attName)
{
webBrowser1.Document.GetElementsByTagName("input");
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement element in col)
{
if (element.GetAttribute(attribute).Equals(attName))
{
element.InvokeMember("click");
}
}
}

Iterating Through All DropDownList In an ASP.NET WebForm C#

What is the object that I would have to reference in order to iterate through all the DropDownList on a web page. I have a web page with several drop down list on it. I want a piece of code that will do the following:
foreach (DropDownList d in xxx)
{
someFunction(d, d.ID);
}
Thanks.
If you don't need to worry about nested controls in which case you would need recursion, something like below should work.
foreach(DropDownList list in Controls.OfType<DropDownList>())
{
//TODO: Something with list
}
If recursion is required you could make a method like below..
public static IEnumerable<Control> GetAllControls(Control parent)
{
if(null == parent) return null;
return new Control[] { parent }.Union(parent.Controls.OfType<Control>().SelectMany(child => GetAllControls(child));
}
And then modify your loop...
foreach(DropDownList list in GetAllControls(this).OfType<DropDownList>())
{
//TODO: Something with list
}
foreach (var dropDownList in Page.Controls.OfType<DropDownList>())
{
}
There is no magical all control container. You're going to have to recursively traverse your control tree and find all the drop downs.
public void DoSomethingForAllControlsOf<T>(Control thisControl, Action<T> method)
where T: class
{
if(thisControl.Controls == null)
return;
foreach(var control in thisControl.Controls)
{
if(control is T)
method(control as T);
DoSomethingForAllControlsOf<T>(control, method);
}
}
That should recursively walk down the control tree and invoke the method on all elements of type T. Example:
DoSomethingForAllControlsOf<DropDownList>(this, someFunction);
You can't run a foreach loop on that because although you have numerous DropDownLists, they are not part of an iterable collection. You could, however, store each DropDownList into an array, and iterate through that array.
To get all of the dropdown controls, you'll probably need to loop through recursively. You can use this function to do it:
public Control DisableDropDowns(Control root)
{
foreach (Control ctrl in root.Controls)
{
if (ctrl is DropDownList)
((DropDownList)ctrl).Enabled = false;
DisableDropDowns(ctrl);
}
}
The LINQ way:
First you need an extension method to grab all the controls of the type you're interested in:
//Recursively get all the formControls
public static IEnumerable<Control> GetAllControls(this Control parent)
{
foreach (Control control in parent.Controls)
{
yield return control;
foreach (Control descendant in control.GetAllControls())
{
yield return descendant;
}
}
}`
Then you can iterate as you wanted:
var formCtls = this.GetAllControls().OfType<DropDownList>();`
foreach(DropDownList ddl in formCtls){
//do what you gotta do ;)
}
while(dropdownlist1.SelectedIndex++ < dropdownlist1.Items.Count)
{
if (dropdownlist1.SelectedValue == textBox1.text)
{
// do stuff here.
}
}
//resetting to 0th index(optional)
dropdownlist1.SelectedIndex = 0;

Categories