Can't locate control from string - c#

I have a custom extension method that (is supposed to) find a control via a string, and perform a click to that control. I've set up a break point and it nevers into the if (c is ToolStripMenuItem) Anyone have any idea where I'm going wrong? This is on WinForms.
private void PerformClickfromString()
{
string item = File.ReadAllText(#"C:\controltest.txt");
foreach (var c in this.Controls)
{
if (c is ToolStripMenuItem)
{
var x = (ToolStripMenuItem)c;
if (x.Name == item)
{
x.PerformClick();
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
PerformClickfromString();
}
Any help is appreciated.

To find all nested items you need a recursive search. Here is an example; it collects all items in a List<ToolStripMenuItem> and the checks for the searched name. I have added that string to the signature of your call..:
private void PerformClickfromString(string s)
{
foreach (var c in this.Controls)
{
List<ToolStripMenuItem> items = new List<ToolStripMenuItem>();
if (c is MenuStrip)
{
foreach (ToolStripMenuItem tsItem in ((MenuStrip)c).Items)
{
GetAllMenuItems(items, tsItem);
}
}
ToolStripMenuItem found = items.Find(x => x.Name == s);
if (found != null) found.PerformClick();
}
}
void GetAllMenuItems(List<ToolStripMenuItem> items, ToolStripMenuItem menu)
{
items.Add(menu);
foreach(ToolStripMenuItem m in menu.DropDownItems)
GetAllMenuItems(items, m);
}

The ToolStripMenuItem is not considered a control, you must instead use its container
if(c is MenuStrip)
{
foreach(ToolStripMenuItem tsItem in ((MenuStrip)c).Items)
{
if (tsItem.Name == item)
{
tsItem.PerformClick();
}
}
}

Related

Loop for all the bunifu Controls and one for the bunifu Textbox

I normaly use this code to empty all the textbox of my form,it
doesn't work when using bunifu controls
```
public void Clear(Control c)
{
foreach(Control ct in c.Controls)
{
if(ct.GetType() == typeof(TextBox) || ct.GetType() == typeof(ComboBox))
{
ct.Text = "";
}
if(ct.Controls.Count != 0)
{
Clear(ct);
}
}
}
i have use this code but already not work.
public void Clear(Control c)
{
foreach (Control ct in c.Controls.OfType<BunifuTextBox>())
{
ct.Text = "";
if(ct.Controls.Count != 0)
{
Clear(ct);
}
}
}

Set the special property of multiple controls to the same value

How can I Set the special property of multiple controls to the same value?
For example set the visible property of all labels in the form to true.
I use this code but labels appear to have null values but they have values.
protected void Page_Load(object sender, EventArgs e)
{
foreach ( Label lbl in this.Controls.OfType<Label>()) {
if (lbl == null) continue;
lbl.Visible = false;
}
}
I should mention that I use master page.But I don't want to set the properties of my nested master pages. I want to set the properties of just current ASP page.
You may have some controls within others, so you need to call it recusrively....Here's a similat method I use..............
Notice at the end, I call it from within itself if the Control in question has controls of its' own....
Hope this helps.....
private void ClearControls(ControlCollection controlCollection, bool ignoreddlNewOrExisting = false)
{
foreach (Control control in controlCollection)
{
if (ignoreddlNewOrExisting)
{
if (control.ID != null)
{
if (control.ID.ToUpper() == "DDLNEWOREXISTING")
{
continue;
}
}
}
if (control is TextBox)
{
((TextBox)control).Text = "";
((TextBox)control).Font.Size = 10;
}
if (control is DropDownList)
{
((DropDownList)control).SelectedIndex = 0;
((DropDownList)control).Font.Size = 10;
}
if (control is CheckBox)
{
((CheckBox)control).Checked = false;
}
//A bit of recursion
if (control.Controls != null)
{
this.ClearControls(control.Controls, ignoreddlNewOrExisting);
}
}
}
Note that you can use following to avoid this ugly type check.:
foreach(Label lbl in this.Controls.OfType<Label>())
lbl.Visible= false;
But neither your nor my approach will enumerate all controls recursively. Only the controls which are on top of the page. So you won't find labels in nested controls(f.e. in a GridView) or which are in the MasterPage. Therefore you need a recursive method.
You could use this handy extension method:
public static class ControlExtensions
{
public static IEnumerable<Control> GetControlsRecursively(this Control parent)
{
foreach (Control c in parent.Controls)
{
yield return c;
if (c.HasControls())
{
foreach (Control control in c.GetControlsRecursively())
{
yield return control;
}
}
}
}
}
Then this readable code should hide all labels on the page and in the MasterPage:
var allLabels = this.GetControlsRecursively()
.Concat(this.Master.GetControlsRecursively())
.OfType<Label>();
foreach (Label label in allLabels)
label.Visible = false;
protected void Page_Load(object sender, EventArgs e)
{
SetAllLabelValue(this.Controls);
}
private void SetAllLabelValue(ControlCollection controls)
{
foreach (Control item in controls)
{
if (item.HasControls())
{
SetAllLabelValue(item.Controls);
}
Label lb = item as Label;
if (lb != null)
{
lb.Visible = false;
}
}
}

Check more than one checkbox upon clicking a button c#

I have for example:
Checkbox1.IsChecked = true;
Checkbox2.IsChecked = true;
Checkbox3.IsChecked = true;
I have this 32 times. Is there a way to have concat string? For example:
i = 1
while i < 32:
("Checkbox"+ (i)).IsChecked = true;
i++
Thanks
While you cannot do exactly what you intend, you can check or uncheck all checkboxes that are in a given container. For example, let's say you have a Panel that contains a number of checkboxes, called pnlChecks. You could do something like
foreach (var chkBox in pnlChecks.Controls.OfType<CheckBox>())
{
chkBox.IsChecked = true;
}
There are multiple methods to achieve this.
Add all of them to a generic List<> and iterate through them like the for you mentioned.
Use reflection and get the checkbox controls and set their value.
Sample WinForms Code
private List<CheckBox> checkboxes = new List<CheckBox>();
public Form1()
{
InitializeComponent();
FillCheckboxes();
}
private void CheckAll()
{
foreach (var chk in checkboxes)
{
chk.Checked = true;
}
}
private void FillCheckboxes()
{
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
checkboxes.Add(c as CheckBox);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
CheckAll();
}
Sample WPF Code
private List<CheckBox> checkboxes = new List<CheckBox>();
public Window1()
{
InitializeComponent();
checkboxes = FindVisualChildren<CheckBox>(main).ToList();
CheckAll();
}
private void CheckAll()
{
foreach (var chk in checkboxes)
{
chk.IsChecked = true;
}
}
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;
}
}
}
}
Important Note
For WPF the suggested method is to use data binding instead of iterating through controls and manually checking/unchecking them. Just bind the IsChecked property to the desired value and change it. You can find more info regarding this on numerous articles on the Internet.

give same property to all textbox controls

how to give same property to all textboxes present in the same form.
foreach (var textbox in this.Controls.OfType<TextBox>())
{
textbox.ContextMenu = new ContextMenu();
}
The above code works only if the textboxes are not in nested format.
In my project I have multiple tabpages in tabcontrol. so i cant implement the above code. but i can implement the below code:
foreach (TextBox textbox in this.Controls.OfType<TabControl>().SelectMany(tc => tc.Controls.OfType<TabPage>().SelectMany(page => page.Controls.OfType<TextBox>())))
{
textbox.ContextMenu = new ContextMenu();
}
foreach (var textbox in this.tabCarInsurance.Controls.OfType<TextBox>())
{
textbox.ContextMenu = new ContextMenu();
}
foreach (var textbox in this.tabHomeLoans.Controls.OfType<TextBox>())
{
textbox.ContextMenu = new ContextMenu();
}
foreach (var textbox in this.tabRetirement.Controls.OfType<TextBox>())
{
textbox.ContextMenu = new ContextMenu();
}
Here I am implementing for each tabControl. which still i dont like (because I have more tab pages to take care of). Is there anyway to reduce the above code.
I tried the below code: (not working)
foreach (var textbox in this.Controls.OfType<TabControl>().OfType<TextBox>())
{
textbox.ContextMenu = new ContextMenu();
}
I got the above code knowledge from my previous question.
Please Help
Thanks in Advance.
private void SetProperty(Control ctr)
{
foreach(Control control in ctr.Controls)
{
if (control is TextBox)
{
control.ContextMenu = new ContextMenu();
}
else
{
if (control.HasChildren)
{
SetProperty(control);
}
}
}
}
How about an extension method to do it, called from your tabcontrol container...
public static class ControlExtensions
{
public static void SetContextMenuOnChildTextBoxes(this Control control)
{
if (control is TextBox)
{
control.ContextMenu = new ContextMenu();
}
if (control.Controls != null)
{
foreach (Control controlChild in control.Controls)
{
controlChild.SetContextMenuOnChildTextBoxes();
}
}
}
}
This could be put in a common area of code so that it could be called from any parents that wanted this functionality.
Just use the recursion to go through all controls subcollections:
void SetControl(ContextMenu menu, Control control)
{
if (control is TextBox)
control.ContextMenu = menu;
else
{
foreach (Control c in control.Controls)
SetControl(menu, c);
}
}
It will find all the textboxes and set one and the same context menu to all of them.
You mal call it,say, from form's OnLoad event handler. While it's assumed that you have yourContextMenu defined for the form.
private void Form1_Load(object sender, EventArgs e)
{
SetControl(yourContextMenu, this);
}
Try:
private void CtxMenu(Control parent)
{
foreach (Control child in parent.Controls)
{
if (child is TextBox)
{
(child as TextBox).ContextMenu = new ContextMenu();
}
}

Is a List<> of variables possible?

This is probably a long shot, but I'm trying to minimize the repition in the program I'm working on, and have run into a snag. As can be seen in the ClearTextBoxes() method below, I have a very repetitive bit of code that I would prefer to place inside a foreach loop for succinctness. (Originally the foreach (object box in customBoxes) loop was not there). I tried to do this with the following List, but to no avail. I'm not sure if this is just not possible to do, or if I'm simply doing it wrong. I would appreciate any help you could give, and if this can't be done, then how can I shrink this code block?
Thanks!
List<object> customBoxes = new List<object>();
customBoxes.AddRange(new[] { "TextBox", "DateBox", "DigitBox", "PhoneBox", "WaterTextBox" });
public void ClearTextBoxes()
{
ChildControls ccChildren = new ChildControls();
foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
{
foreach (object box in customBoxes)
{
if (o.GetType() == typeof(TextBox))
{
TextBox txt = (TextBox)o;
txt.Text = "";
}
if (o.GetType() == typeof(DigitBox))
{
DigitBox digit = (DigitBox)o;
digit.Text = "";
}
if (o.GetType() == typeof(PhoneBox))
{
PhoneBox phone = (PhoneBox)o;
phone.Text = "";
}
if (o.GetType() == typeof(DateBox))
{
DateBox date = (DateBox)o;
date.Text = "";
}
if (o.GetType() == typeof(WatermarkTextBox))
{
WatermarkTextBox water = (WatermarkTextBox)o;
water.Text = "";
}
}
}
}
List<Type> customBoxes = new List<Type>();
customBoxes.AddRange(new[] { typeof(PhoneBox), typeof(DigitBox), ....." });
foreach (Control c in this.Controls)
{
if (customBoxes.Contains(c.GetType()))
{
c.Text = string.Empty;
}
}
I would create an interface with a ClearText() method.
interface IClearable
{
public void ClearText();
}
Then you can inherit from each control and apply that interface:
class ClearableDigitBox : DigitBox, IClearable
{
public void ClearText() {
Text = String.Empty;
}
}
// etc...
So it's just:
var list = new List<IClearable>;
// ...
foreach (IClearable control in list) control.ClearText();
You could use reflection in some way to mimic some ductyping behavior but i wouldnt go for that solution since it's not performant and ugly.
foreach (object box in customBoxes)
{
var boxType = box.GetType();
var textProperty = boxType.GetProperty("Text");
if (textProperty != null && textProperty.CanWrite)
{
textProperty.SetValue(box, "", null);
}
}
Or you can use dynamic to achieve the same result:
foreach (dynamic box in customBoxes)
{
box.Text = "";
}
The way to go would be to make your custom controls implement a single interface IWithTextProperty which ofcourse exposes the text property.
Aren't all the input boxes a part of Control object?
if so, and you want to clear all the text from the controls
then i would probably have a method like:
public void ClearText(List<Control> items)
{
foreach (Control control in items)
{
control.Text = string.Empty;
}
}
if you just want to locate controls of a specific type
public void ClearText(List<Control> items)
{
foreach (Control control in items)
{
if (control is TextBox)
((TextBox)control).Text = string.Empty;
else if (control is DigitBox)
((DigitBox)control).Text = string.Empty;
else
{ // Handle anything else.}
}
}
In response to a couple of the replies so far, this is the class file I have for the custom boxes. The NumberTextBox class is the default snippet that VS added. I haven't used it, just haven't deleted it either. In addition to the DateBox(which is collapsed to save space) class, there is also a PhoneBox class that inherits from DigitBox as well. the WatermarkTextBox class that DigitBox inherits from is in the WpfToolkit.Extended.dll. The only real difference in these classes is that each adds a method to allow/disallow formatting keys being pressed (parenthesis, periods, hyphens, etc).
This class basically came about as a result of trying to merge several different snippets I found around the web, but the purpose of these boxes is to enable a watermark and also restrict the characters that can be entered into those boxes.
public class NumberTextBox : Control
{
static NumberTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberTextBox), new FrameworkPropertyMetadata(typeof(NumberTextBox)));
}
}
public class DigitBox : WatermarkTextBox, IClearable
{
#region Constructors
///<summary>
///The default constructor
/// </summary>
public DigitBox()
{
TextChanged += new TextChangedEventHandler(OnTextChanged);
KeyDown += new KeyEventHandler(OnKeyDown);
PreviewKeyDown += new KeyEventHandler(OnPreviewDown);
}
#endregion
#region Properties
new public String Text
{
get { return base.Text; }
set
{
base.Text = LeaveOnlyNumbers(value);
}
}
#endregion
#region Functions
public bool IsNumberKey(Key inKey)
{
if (inKey < Key.D0 || inKey > Key.D9)
{
if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
{
return false;
}
}
return true;
}
public bool IsActionKey(Key inKey)
{
return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return;
}
public string LeaveOnlyNumbers(String inString)
{
String tmp = inString;
foreach (char c in inString.ToCharArray())
{
if (!IsDigit(c))
{
tmp = tmp.Replace(c.ToString(), "");
}
}
return tmp;
}
public bool IsSpaceKey(Key inKey)
{
if (inKey == Key.Space)
{
return true;
}
return false;
}
public bool IsDigit(char c)
{
return (c >= '0' || c <='9');
}
#endregion
#region Event Functions
protected virtual void OnKeyDown(object sender, KeyEventArgs e)
{
e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key) && !IsSpaceKey(e.Key);
}
protected virtual void OnTextChanged(object sender, TextChangedEventArgs e)
{
base.Text = LeaveOnlyNumbers(Text);
}
protected virtual void OnPreviewDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
#endregion
}
public class DateBox : DigitBox

Categories

Resources