I'm using following code to clear all text boxes in a form.
protected static IEnumerable<Control> GetAllChildren(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;
}
}
internal static void ResetTextBoxes(Control root, string resetWith = "", params TextBox[] except)
{
try
{
foreach (TextBox txt in GetAllChildren(root).OfType<TextBox>())
{
foreach (TextBox txtException in except)
{
if (txtException.Name != txt.Name)
{
txt.Text = resetWith == "" ? string.Empty : resetWith;
}
}
}
}
catch (Exception ex) { throw ex; }
}
I tried to separate some special text boxes those I don't want to be cleared by using params, but still it clears all the boxes. Need help, please.
Shorter version of GetAllChildren:
protected static IEnumerable<Control> GetAllChildren(Control root) {
return new Control[] { root }
.Concat(root.Controls
.OfType<Control>()
.SelectMany(item => GetAllChildren(item)));
}
And shorter Linq:
var source = GetAllChildren(root)
.OfType<TextBox>()
.Where(ctrl => !except.Contains(ctrl));
foreach (var textBox in source)
textBox.Text = resetWith;
The problem with your current implmentation is in the inner loop:
foreach (TextBox txtException in except)
if (txtException.Name != txt.Name)
txt.Text = resetWith == "" ? string.Empty : resetWith;
if you have at least two exceptions with different names that condition
txtException.Name != txt.Name
will be inevitably satisfied (any txt.Name either not equal 1st exception or 2nd one)
This is happening since you are testing all the elements the first collection against all the elements of the second collection, so even if a textbox exists in the except array, it's name will not match the other textboxes there.
Use Linq's Any extension method instead:
internal static void ResetTextBoxes(Control root, string resetWith = "", params TextBox[] except)
{
foreach (TextBox txt in GetAllChildren(root).OfType<TextBox>())
{
if(!except.Any(t => t.Name == txt.Name))
{
txt.Text = resetWith == "" ? string.Empty : resetWith;
}
}
}
}
Here is the function I use:
public void ClearAllFields(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox && c != null)
{
if (c.Name != "specialtxtbox1name" && c.Name != "specialtxtbox2name" && c.Name != "nameoftextbox") // just put in the name of special textBoxes between double quotes
c.Text = "";
}
else
{
ClearAllFields(c);
}
}
}
Simply call the function wherever needed
Example:
private void InsertDatathenClearForm_Click(object sender, EventArgs e)
{
//Code
//.
//.
File.Create(filepath);
MessageBox.Show("All Textboxes Data saved successfully");
//Clear fxn
ClearAllFields(this);
}
.
.
}
Related
I want to check controls if they are empty and make exception for some Controls for example i want to make exception for these Textboxes Notes_txtbox and Job_txt
. This is my function but it doesn't give me correct result . It gives Notes_txtbox,Job_txt are empty :
public bool Check_Inputs_Empty_Exception(List<Control> Exception_Control_List,
TableLayoutPanel TableLayoutPanel)
{
bool is_empy = false;
foreach (Control Control in TableLayoutPanel.Controls)
{
for (int i = 0; i < Exception_Control_List.Count; i++)
{
if (Control.Name != Exception_Control_List[i].Name &&
Control.GetType() == Exception_Control_List[i].GetType())
{
if (String.IsNullOrWhiteSpace(Control.Text))
{
MessageBox.Show(Control.Name + " is empty");
is_empy = true;
}
}
}
}
return is_empy;
}
}
And this is Function Call :
if (Check_Inputs_Empty_Exception(
new List<Control> {Notes_txtbox,Job_txt}, TableLayoutPanel) == false)
{
// My Add Function
}
This is a simple and common logic flaw. Your loops are set up with the wrong logic gates. You are simply searching for any non-match, which you will always find (for every control in the control list there is a control in the exception list that is not the same control, unless the exception list has exactly one element).
Instead, search for any single match and set a flag if found. When the flag is set you move the outer loop on to the next iteration.
public bool Check_Inputs_Empty_Exception(List<Control> Exception_Control_List, TableLayoutPanel TableLayoutPanel)
{
bool is_empy = false;
foreach (Control Control in TableLayoutPanel.Controls)
{
bool found = false;
for (int i = 0; i < Exception_Control_List.Count; i++)
{
if (Control == Exception_Control_List[i])
{
found = true;
break;
}
}
if (found) continue;
if (String.IsNullOrWhiteSpace(Control.Text))
{
MessageBox.Show(Control.Name + " is empty");
is_empy = true;
}
}
return is_empy;
}
With a little LINQ you can simplify this quite a bit too. Also, if you just want textboxes, you can filter out everything else using OfType<>().
public bool Check_Inputs_Empty_Exception(List<Control> Exception_Control_List, TableLayoutPanel TableLayoutPanel)
{
bool result = false;
var emptyTextboxes = TableLayoutPanel.Controls
.OfType<TextBox>()
.Where( c => string.IsNullOrEmpty(c.Text) )
.ToList();
foreach (var control in emptyTextboxes)
{
if (!Exception_Control_List.Any( c => c == control ))
{
MessageBox.Show(control.Name + " is empty");
result = true;
}
}
return result;
}
I suggest using Linq instead of for and query the controls
// signature: more natural have "parent, excluded":
// "scan parent without excluded"
// IEnumerable<Control> - now I can pass almost any collection, say array
public static bool Check_Inputs_Empty_Exception<T>(Control parent,
IEnumerable<T> excluded)
where T : Control {
if (null == parent)
throw new ArgumentNullException(nameof(parent));
// HashSet is more efficient (and convenient) for Contains then List
HashSet<T> exceptions = excluded == null
? new HashSet<T>()
: new HashSet<T>(excluded);
// array of Controls of type T on parent with empty Text and not within exceptions
var empty = parent
.Controls
.OfType<T>()
.Where(control => !exceptions.Contains(control))
.Where(control => string.IsNullOrEmpty(control.Text))
// .OrderBy(control => control.Name) //TODO: you may want to sort the controls
.ToArray();
foreach (T ctrl in empty)
MessageBox.Show($"{ctrl.Name} is empty");
return empty.Any();
}
// If we don't have controls to exclude
public static bool Check_Inputs_Empty_Exception<T>(Control parent)
where T : Control {
return Check_Inputs_Empty_Exception<T>(parent, new T[0]);
}
usage
if (!Check_Inputs_Empty_Exception<TextBox>(TableLayoutPanel,
new TextBox[] {Notes_txtbox, Job_txt})) {
//TODO: relevant code here
}
Edit: If you want to test, say, TextBoxs only (exclude all other Controls like Panels, Buttons) you can use generics
private bool LoopOverControls(bool reset, bool checkIfEmpty)
{
bool results;
foreach (Control ctrl in this.Controls)
{
if ((ctrl as TextBox) != null)
{
if (reset == true)
(ctrl as TextBox).Text = "";
if (checkIfEmpty == true)
results = true;
}
}
return results;
}
I want to use the method in some places in the code. Instead making loop over the controls over again each time i want to make a method i can call.
The method was before:
private void LoopOvercontrols(bool reset, bool checkIfEmpty)
{
foreach (Control ctrl in this.Controls)
{
if ((ctrl as TextBox) != null)
{
if (reset == true)
(ctrl as TextBox).Text = "";
if (checkIfEmpty == true)
}
}
}
And this is the places i'm using the loop over the controls in my code the first place is in the constructor: I check if the textBoxes are not empty then do something in this case change btnReset enable true.
foreach (Control ctrl in this.Controls)
{
if ((ctrl as TextBox) != null)
{
(ctrl as TextBox).TextChanged += on_TextChanged;
if ((ctrl as TextBox).Text != "")
{
btnReset.Enabled = true;
}
}
}
Then inside another event this time i check if the textboxes are empty and set the btnReset enable to false:
foreach (Control ctrl in this.Controls)
{
if ((ctrl as TextBox) != null)
{
(ctrl as TextBox).TextChanged += on_TextChanged;
if ((ctrl as TextBox).Text == "")
{
btnReset.Enabled = false;
}
}
}
So far i'm looping over the textBoxes in two places but i might want to loop over them again later in other places. The problem is how to make the method LoopOverControls so i can decide with a bool and maybe other properties some cases and using buttons or other controls within the decitions ?
You can write a method that receive the action to be executed as parameter.
The work for this new method is to enumerate all the textboxes and call the action method for each one.
public void TextBoxEnumerateAndAction(Action<TextBox> execute)
{
// Get just the TextBoxes, no need of ugly casts...
foreach(TextBox t in this.Controls.OfType<TextBox>())
{
execute?.Invoke(t);
// This part is common to every textbox, so it can stay inside the
// enumeration loop....
btnReset.Enabled = !string.IsNullOrEmpty(t.Text)
}
}
Now define the Action methods to pass to TextBoxEnumerateAndAction
void AddTextChangedHandler(TextBox t)
{
t.TextChanged += on_TextChanged;
}
void RemoveTextChangedHandler(TextBox t)
{
t.TextChanged -= on_TextChanged;
}
So, everywhere you need to add or remove the TextChanged handler you could call
TextBoxEnumerateAndAction(AddTextChangedHandler);
Or if you have more fancy situations, you could simply define another action to pass to TextBoxEnumerateAndAction
I suggest you just add some comfort methods to contain the looping and type selection logic and allow callers to pass in delegate function to control the applied logic.
for example:
public void ActOn<TControl>(Action<TControl> applyFunction)
where TControl : Control
{
if (applyFunction == null) { throw new ArgumentNullException(nameof(applyFunction)); }
var controlsOfChosenType = this
.Controls
.OfType<TControl>();
foreach (var control in controlsOfChosenType)
{
applyFunction(control);
}
}
Then you can use it like this:
ActOn<TextBox>(textbox => textbox.Text = DateTime.Now.ToString());
I have a bit of a pickle. There are a list of images I want to grab on a website. I know how to do that much, but I have to filter out the location of the images.
Such as I'd want to grab the images in a div tag with an id "theseImages", but there are another set of images within another div tag with an id called "notTheseImages". Looping through every tag into ah HtmlElementCollection with the tag "img" would ignore the divs, because it'd also grab the images from "notTheseImages."
Is there a way I could loop through the images while doing a check to see where those images are located in the div tags?
This could help you to do the selection of your current HTML and maybe for future occassions :)
protected HtmlElement[] GetElementsByParent(HtmlDocument document, HtmlElement baseElement = null, params string[] singleSelectors)
{
if (singleSelectors == null || singleSelectors.Length == 0)
{
throw new Exception("Please give at least 1 selector!");
}
IList<HtmlElement> result = new List<HtmlElement>();
bool last = singleSelectors.Length == 1;
string singleSelector = singleSelectors[0];
if (string.IsNullOrWhiteSpace(singleSelector) || string.IsNullOrWhiteSpace(singleSelector.Trim()))
{
return null;
}
singleSelector = singleSelector.Trim();
if (singleSelector.StartsWith("#"))
{
var item = document.GetElementById(singleSelector.Substring(1));
if (item == null)
{
return null;
}
if (last)
{
result.Add(item);
}
else
{
var results = GetElementsByParent(document, item, singleSelectors.Skip(1).ToArray());
if (results != null && results.Length > 0)
{
foreach (var res in results)
{
result.Add(res);
}
}
}
}
else if (singleSelector.StartsWith("."))
{
if (baseElement == null)
{
baseElement = document.Body;
}
foreach (HtmlElement child in baseElement.Children)
{
string cls;
if (!string.IsNullOrWhiteSpace((cls = child.GetAttribute("class"))))
{
if (cls.Split(' ').Contains(singleSelector.Substring(1)))
{
if (last)
{
result.Add(child);
}
else
{
var results = GetElementsByParent(document, child, singleSelectors.Skip(1).ToArray());
if (results != null && results.Length > 0)
{
foreach (var res in results)
{
result.Add(res);
}
}
}
}
}
}
}
else
{
HtmlElementCollection elements = null;
if (baseElement != null)
{
elements = baseElement.GetElementsByTagName(singleSelector);
}
else
{
elements = document.GetElementsByTagName(singleSelector);
}
foreach (HtmlElement item in elements)
{
if (last)
{
result.Add(item);
}
else
{
var results = GetElementsByParent(document, item, singleSelectors.Skip(1).ToArray());
if (results != null && results.Length > 0)
{
foreach (var res in results)
{
result.Add(res);
}
}
}
}
}
return result.ToArray();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// here we can query
var result = GetElementsByParent(webBrowser1.Document, null, "#theseImages", "img");
}
result would then contain the images that are under #theseImages
Mind you the GetElementsByParent is fairly untested, I just tested it for your use case and it seemed to be ok.
Don't forget to only start the query once you are sure the document is completed ;)
I have a listview which binds the customers informations. I highlight the listview items by typing in a textbox. For example, when I type "PET" into the textbox then it highlights the "PET"s in the listview items. It works and highlights.
But after that when I click on the highlighted item it gives an error. But it is interesting when I click on a free place in the listview item it works. For example, it highlighted PETER HEINZ. IF I click on to PETER or HEINZ it gives error. But, if I click on the space between PETER HEINZ it works. What a error is this? The error message is
System.InvalidOperationException was unhandled
Message="'System.Windows.Documents.Run' is not a Visual or Visual3D."
The source code is below:
private void HighlightText(Object itx)
{
if (itx != null)
{
if (itx is TextBlock)
{
Regex regex = new Regex("(" + textBox1.Text + ")", RegexOptions.IgnoreCase);
TextBlock tb = itx as TextBlock;
if (textBox1.Text.Length == 0)
{
string str = tb.Text;
tb.Inlines.Clear();
tb.Inlines.Add(str);
return;
}
string[] substrings = regex.Split(tb.Text);
tb.Inlines.Clear();
foreach (var item in substrings)
{
if (regex.Match(item).Success)
{
Run runx = new Run(item);
runx.Background = Brushes.LightGray;
tb.Inlines.Add(runx);
}
else
{
tb.Inlines.Add(item);
}
}
return;
}
else
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
{
HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
}
}
}
}
Here's a revisement. Can you paste it over yours and see if it works better in your application?
key points:
Added protection to not let non-visual objects make it to the GetChild recursion
Added check for textbox1 being empty
trivial: moved tb.inlines.clear to reduce code repitition
trivial: inverted itx null check to reduce nesting
.
private void HighlightText(Object itx)
{
//safety checks
if (itx == null)
return;
if (String.isNullOrEmpty(textBox1.Text)
return; //just in case the box is empty
if (! (itx is Visual || itx is System.Windows.Media.Media3D.Visual3D)
return; //prevent from getting children on non-visual element
if (itx is TextBlock)
{
Regex regex = new Regex("(" + textBox1.Text + ")", RegexOptions.IgnoreCase);
TextBlock tb = itx as TextBlock;
tb.Inlines.Clear();
if (textBox1.Text.Length == 0)
{
string str = tb.Text;
tb.Inlines.Add(str);
return;
}
string[] substrings = regex.Split(tb.Text);
foreach (var item in substrings)
{
if (!regex.Match(item).Success)
{
Run runx = new Run(item);
runx.Background = Brushes.LightGray;
tb.Inlines.Add(runx);
}
else
{
tb.Inlines.Add(item);
}
}
return;
}
else
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
{
HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
}
}
}
I have a method that finds all the controls, iterates through them, determines if they are a textbox,drop down list, etc.. retrieves their ID name, and depending on the ID name it will set a boolean statement (thus I would know if that section of the form is complete, and will email to a certain group of people) unfortunetly this is done with too many if statements and was wondering if I could get some help making this more manageable
protected void getEmailGroup()
{
Control[] allControls = FlattenHierachy(Page);
foreach (Control control in allControls)
{
if (control.ID != null)
{
if (control is TextBox)
{
TextBox txt = control as TextBox;
if (txt.Text != "")
{
if (control.ID.StartsWith("GenInfo_"))
{
GenInfo = true;
}
if (control.ID.StartsWith("EmpInfo_"))
{
EmpInfo = true;
}
}
}
if (control is DropDownList)
{
DropDownList lb = control as DropDownList;
if (lb.SelectedIndex != -1)
{
if (control.ID.StartsWith("GenInfo_"))
{
GenInfo = true;
}
if (control.ID.StartsWith("EmpInfo_"))
{
EmpInfo = true;
}
}
}
}
}
}
Why not just use the Control.FindControl(string) method?
private void Button1_Click(object sender, EventArgs MyEventArgs)
{
// Find control on page.
Control myControl1 = FindControl("TextBox2");
if(myControl1!=null)
{
// Get control's parent.
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " + myControl2.ID);
}
else
{
Response.Write("Control not found");
}
}
from: https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol
It is hard to understand the logic behind your code, but I'm sure it can be written easier. For example you can do something like this:
DropDownBox box = FlattenHierachy(Page)
.Where(c => c is DropDownList)
.Cast<DropDownList>()
.Where(d => d.SelectedIndex != -1)
.FirstOrDefault();
if (box != null)
{
if (box.ID.StartsWith("GenInfo_"))
{
GenInfo = true;
}
if (box.ID.StartsWith("EmpInfo_"))
{
EmpInfo = true;
}
}
Obviously you can make this generic if you extract the lambda expression from the seconde Where call. So you could reuse it for different types. That's the solution which is as close to your code as possible, but I guess it would be a better idea to use a recursive method traversing the page and giving that method your predicates as lambda expressions.
Cleaned up you code a little to only include each check once.
protected void getEmailGroup()
{
Control[] allControls = FlattenHierachy(Page);
foreach (Control control in allControls)
{
if (control.ID != null &&
((control is TextBox && ((TextBox)control).Text = "" )
|| (control is DropDownList && ((DropDownList)control).SelectedIndex != -1 ))
{
if (control.ID.StartsWith("GenInfo_"))
GenInfo = true;
if (control.ID.StartsWith("EmpInfo_"))
EmpInfo = true;
}
}
}
}
Instead of using the Lambda expression I have created a method that handles the control for me, and depending on the name of the control, it sets that section to be true
public bool setGroup(Control ctrl)
{
isAControl = false;
//set a section to true, so it will pull the html
if (ctrl.ID.StartsWith("GenInfo_"))
{
GenInfo = true;
lstControls.Add(ctrl.ID.Replace("GenInfo_", ""));
isAControl = true;
return isAControl;
}
here is a small snippet of my code I only want to check for certain controls(to speed things up) and I go through each control as each control has a different way to get the value (textbox would use .text where dropdownlist would use .selectedValue)
if(control is TextBox || control is DropDownList || control is RadioButton || control is RadioButtonList
|| control is CheckBox || control is CheckBoxList)
{
if (control is TextBox)
{
TextBox txt = control as TextBox;
if (txt.Text != "" && txt.Text != "YYYY/MM/DD")
{
setGroup(control);
if (isAControl)
{
string controlNoGroup = lstControls.Last();
strHtml = strHtml.Replace("#" + (controlNoGroup.ToString()) + "#", txt.Text);
}
}
}