I was wondering, is there a way I can reset all the checkboxes, textboxes, numerics and other controls back to the default values without writing code for every control individually? This is the code I've tried, but doesn't seem to work:
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].ResetText();
}
EDIT:
I've fixed it by manually setting the control values, sorry for all the trouble >.<.
Do as below create class and call it like this
Check : Reset all Controls (Textbox, ComboBox, CheckBox, ListBox) in a Windows Form using C#
private void button1_Click(object sender, EventArgs e)
{
Utilities.ResetAllControls(this);
}
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = null;
}
if (control is ComboBox)
{
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
}
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
}
if (control is ListBox)
{
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
}
}
}
}
You can create the form again and dispose the old one.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnReset_Click(object sender, EventArgs e)
{
Form1 NewForm = new Form1();
NewForm.Show();
this.Dispose(false);
}
}
foreach (Control field in container.Controls)
{
if (field is TextBox)
((TextBox)field).Clear();
else if (field is ComboBox)
((ComboBox)field).SelectedIndex=0;
else
dgView.DataSource = null;
ClearAllText(field);
}
Additional-> To clear the Child Controls
The below function would clear the nested(Child) controls also, wrap up in a class.
public static void ClearControl(Control control)
{
if (control is TextBox)
{
TextBox txtbox = (TextBox)control;
txtbox.Text = string.Empty;
}
else if (control is CheckBox)
{
CheckBox chkbox = (CheckBox)control;
chkbox.Checked = false;
}
else if (control is RadioButton)
{
RadioButton rdbtn = (RadioButton)control;
rdbtn.Checked = false;
}
else if (control is DateTimePicker)
{
DateTimePicker dtp = (DateTimePicker)control;
dtp.Value = DateTime.Now;
}
else if (control is ComboBox)
{
ComboBox cmb = (ComboBox)control;
if (cmb.DataSource != null)
{
cmb.SelectedItem = string.Empty;
cmb.SelectedValue = 0;
}
}
ClearErrors(control);
// repeat for combobox, listbox, checkbox and any other controls you want to clear
if (control.HasChildren)
{
foreach (Control child in control.Controls)
{
ClearControl(child);
}
}
}
If you have some panels or groupboxes reset fields should be recursive.
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
RecursiveResetForm(control);
}
}
private void RecursiveResetForm(Control control)
{
if (control.HasChildren)
{
foreach (Control subControl in control.Controls)
{
RecursiveResetForm(subControl);
}
}
switch (control.GetType().Name)
{
case "TextBox":
TextBox textBox = (TextBox)control;
textBox.Text = null;
break;
case "ComboBox":
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
break;
case "CheckBox":
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
break;
case "ListBox":
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
break;
case "NumericUpDown":
NumericUpDown numericUpDown = (NumericUpDown)control;
numericUpDown.Value = 0;
break;
}
}
}
You can reset all controls of a certain type. Something like
foreach(TextBox tb in this.Controls.OfType<TextBox>().ToArray())
{
tb.Clear();
}
But you can't reset all controls at once
Quick answer, maybe it'll help:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
while (f2.DialogResult == DialogResult.Retry)
{
f2 = new Form2();
f2.ShowDialog();
}
}
and in Form2 (The 'settings' Form):
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Retry;
Close();
}
I recently had to do this for Textboxes and Checkboxes but using JavaScript ...
How to reset textbox and checkbox controls in an ASP.net document
Here is the code ...
<script src="http://code.jquery.com/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
function ResetForm() {
//get the all the Input type elements in the document
var AllInputsElements = document.getElementsByTagName('input');
var TotalInputs = AllInputsElements.length;
//we have to find the checkboxes and uncheck them
//note: <asp:checkbox renders to <input type="checkbox" after compiling, which is why we use 'input' above
for(var i=0;i< TotalInputs ; i++ )
{
if(AllInputsElements[i].type =='checkbox')
{
AllInputsElements[i].checked = false;
}
}
//reset all textbox controls
$('input[type=text], textarea').val('');
Page_ClientValidateReset();
return false;
}
//This function resets all the validation controls so that they don't "fire" up
//during a post-back.
function Page_ClientValidateReset() {
if (typeof (Page_Validators) != "undefined") {
for (var i = 0; i < Page_Validators.length; i++) {
var validator = Page_Validators[i];
validator.isvalid = true;
ValidatorUpdateDisplay(validator);
}
}
}
</script>
And call it with a button or any other method ...
<asp:button id="btnRESET" runat="server" onclientclick="return ResetForm();" text="RESET" width="100px"></asp:button>
If you don't use ValidationControls on your website, just remove all the code refering to it above and the call Page_ClientValidateReset();
I am sure you can expand it for any other control using the DOM. And since there is no post to the server, it's faster and no "flashing" either.
function setToggleInputsinPnl(pnlName) {
var domCount = pnlName.length;
for (var i = 0; i < domCount; i++) {
if (pnlName[i].type == 'text') {
pnlName[i].value = '';
} else if (pnlName[i].type == 'select-one') {
pnlName[i].value = '';
}
}
}
There is a very effective way to use to clear or reset Windows Form C# controls like TextBox, ComboBox, RadioButton, CheckBox, DateTimePicker etc.
private void btnClear_Click(object sender, EventArgs e)
{
Utilities.ClearAllControls(this);
}
internal class Utilities
{
internal static void ClearAllControls(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
else if (c is ComboBox)
{
((ComboBox)c).SelectedIndex = -1;
}
else if (c is RadioButton)
{
((RadioButton)c).Checked = false;
}
else if (c is CheckBox)
{
((CheckBox)c).Checked = false;
}
else if (c is DateTimePicker)
{
((DateTimePicker)c).Value = DateTime.Now; // or null
}
}
}
}
To accomplish this with overall user experience in c# we can use one statement to clear them. Pretty straight forward so far, above is the code.
Related
Basically, I'm making a basic note taking app and would like to add different color options, for an example, a light version and dark version for easier viewing by the user. I plan to give it to a few friends but no one can agree on a color so I thought I'd just make it customizeable by the user via small buttons.
I'm not very experienced however and was wondering how I'd go about doing this?
You could loop through all the controls and change their .BackColor:
private void ChangeTheme_btn_Click(object sender, EventArgs e)
{
ChangeTheme(this.Controls, Color.Aqua);
}
private void ChangeTheme(Control.ControlCollection controls, Color color)
{
foreach (Control control in controls)
{
if (control.HasChildren)
{
// Recursively loop through the child controls
ChangeTheme(control.Controls, color);
}
else
{
if (control is TextBox textBox)
{
textBox.BackColor = color;
}
else if (control is Button button)
{
button.BackColor = color;
}
// Example (remove this if you are not using Guna UI)
else if (control is Guna.UI2.WinForms.Guna2Button gBbutton)
{
gBbutton.FillColor = color;
}
}
}
}
You could set a separate color to each type of control, and also change other properties. For example, if you are using Guna UI, then you may need to set the .FillColor instead of the .BackColor.
If you have multiple forms, you can do:
private void MyForm1_form_Load(object sender, EventArgs e)
{
// Add all the controls to the list
foreach (Control item in MyForm1.instance.Controls)
{
listOfAllFormControls.Add(item);
}
foreach (Control item in MyForm2.instance.Controls)
{
listOfAllFormControls.Add(item);
}
}
private void ChangeTheme_btn_Click(object sender, EventArgs e)
{
SetColorThemeToLight(listOfAllFormControls);
}
private readonly List<Control> listOfAllFormControls = new List<Control>();
private void SetColorThemeToLight(List<Control> list)
{
foreach (Control control in list)
{
if (control.HasChildren)
{
// Recursively loop through the child controls
List<Control> controlList = new List<Control>();
foreach (Control item in control.Controls)
{
controlList.Add(item);
}
SetColorThemeToLight(controlList);
}
else
{
switch (control)
{
case TextBox textBox:
textBox.BackColor = Color.Blue;
break;
case Button button:
button.BackColor = Color.Blue;
break;
// Example (remove this if you are not using Guna UI)
case Guna.UI2.WinForms.Guna2Button gButton:
gButton.BackColor = Color.Blue;
break;
}
}
}
}
I also used a switch case instead
IF YOU ARE USING GUNA UI
This is only needed if you are using Guna:
Some Guna controls have a child by default, which doesn't work well with the code above. For example, the Guna2RadioButton contains a child control called Guna2CustomRadioButton.
So here's the updated code if you are using Guna:
private static void SetColorThemeToDark(List<Control> list)
{
foreach (Control control in list)
{
bool pass = false;
if (Control.HasChildren)
{
// Recursively loop through the child controls
List<Control> childList = new List<Control>();
foreach (Control item in control.Controls)
{
childList.Add(item);
}
SetColorThemeToDark(childList);
pass = true;
}
if (!control.HasChildren || pass)
{
switch (control)
{
case Button button:
button.BackColor = Color.Red;
button.ForeColor = Color.White;
break;
case Label label:
label.BackColor = Color.Red;
break;
case Guna.UI2.WinForms.Guna2Button guna2Button:
guna2Button.FillColor = Color.Red;
}
}
}
}
private void DisableControl(Control control)
{
foreach (Control childControl in divPanelAdd.Controls)
{
var textbox = control as TextBox;
if (textbox != null || textbox == null)
textbox.Enabled= false;
var dropDownList = control as DropDownList;
if (dropDownList != null || dropDownList == null)
dropDownList.Enabled = false;
DisableControl(childControl);
}
}
I'm using for each loop to enable or disable of "divPanelAdd" div but it shows nullexception error.help me to solve this.
Try this:
private void DisableControl() {
foreach (Control childControl in divPanelAdd.Controls) {
if (childControl.GetType() == typeof(TextBox) || childControl.GetType() == typeof(DropDownList))
((WebControl)childControl).Enabled = false;
}
}
private void SomeEventHandler(object Sender, EventArgs e)
{
DisableControl(divPanelAdd)
}
// Disable textboxes and dropdowns (recusively) under parent.
private void DisableControl(Control parent)
{
foreach (Control child in parent.Controls)
{
var textbox = child as TextBox;
if (textbox != null)
{
textbox.Enabled = false;
}
else
{
var dropDownList = child as DropDownList;
if (dropDownList != null)
{
dropDownList.Enabled = false;
DisableControl(dropDownList);
}
}
}
}
You did intend for this to be recursive? If so the for shouldn't the for loop refer to the control passed in not divPanel. divPanel add can be the first parent passed in from somewhere else.
I have a tab control where I can add custom controls made from a button and a label underneath. I want to add a search function to my project so that when a user type a name of a control, it will show all controls where the name (label) starts with the letters typed. Also typing in a textbox will do the job.. Is there an easy way to do this?
You can search controls within parent control collection:
foreach(Control c in ParentControl.Controls)
{
if(c.Name == "label1")
{
//add to your list
}
}
You can also check using StartsWith("stringVal")
if(c.Name.StartsWith("l"))
{
//add to your list
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
// Skip, if the control is the used TextBox
if (control == textBox1) { continue; }
// Show all controls where name starts with inputed string
// (use ToLower(), so casing doesnt matter)
if (control.Name.ToLower().StartsWith(textBox1.Text.Trim().ToLower()))
{
control.Visible = true;
}
// Hide objects that doesn't match
else { control.Visible = false; }
}
}
This toggles the controls' visibility, and hide's all items, that doesn't match the given input. Also casing doesn't matter.
Done when adding the text to search in a textbox.
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control c in fl_panel.Controls)
{
if (c.Name.ToUpper().StartsWith(textBox1.Text.ToUpper().ToString()) && textBox1.Text != "")
{
Control[] ctrls = fl_panel.Controls.Find(textBox1.Text.ToString(), true);
c.Visible = true; // to restore previous matches if I delete some text
}
else if(textBox1.Text == "")
{
c.Visible = true;
}
else
{
c.Visible = false;
}
}
}
you can try this to find the controls that matches a text typed in the textbox, after that you can do whatever you want with it.
private void textBox1_TextChanged(object sender, EventArgs e)
{
var controlMatchesCriteria = from c in this.Controls.OfType<TextBox>()
where c.Name == textBox1.Text
select c;
}
I have a number of different tabs and within the tabs i have some radio buttons. I only want one radio button at a time within the tab pages to be selected. I have the following code
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
var rdoButtonName = sender as RadioButton;
foreach (TabPage page in pages)
{
foreach (Control item in page.Controls)
{
if (item is RadioButton)
{
if (rdoButtonName.Name == item.Name)
{
rdoButtonName.Checked = true;
}
else
{
rdoButtonName.Checked = false;
}
}
}
}
}
When i go to click on the radio buttons on the first tab page it does not allow me to change their checked status but it does on the other pages. Can anyone see anything wrong with the above code?
I see that you set Cheked value to the sender and not to other radio buttons...
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
var rdoButtonName = sender as RadioButton;
foreach (TabPage page in pages)
{
foreach (Control item in page.Controls)
{
if (item is RadioButton)
{
if (item.Name != rdoButtonName.Name)
{
item.Checked = false;
}
}
}
}
}
I think (better debug and check) that your problem is that each time you change one of your RadioButton state it will call this function over again and will make the unexpected results.
To fix that I would detach and reattach your event handler before changing the Checked state, like this:
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
var rdoButtonName = sender as RadioButton;
foreach (TabPage page in pages)
{
foreach (Control item in page.Controls)
{
if (item is RadioButton)
{
rdoButtonName.CheckedChanged -= new System.EventHandler(this.RadioButtonCheckedChanged);
if (rdoButtonName.Name == item.Name)
{
rdoButtonName.Checked = true;
}
else
{
rdoButtonName.Checked = false;
}
rdoButtonName.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged);
}
}
}
}
The problem is, that "CheckedChanged"-event can't be used in this kind of situation, and that is where the problem lies. The key is to use another event - "Click":
private void radioButton_Click(object sender, EventArgs e)
{
foreach (TabPage page in tabControl1.TabPages)
{
foreach (RadioButton radioButton in page.Controls)
{
if (radioButton != (RadioButton)sender) { radioButton.Checked = false; }
}
}
}
Change your RadioButtons to use this method in the "Click"-event and it'll work. I tested it myself.
My requirement is to count the total number of TextBox and CheckBox present directly inside the form with the id="form1", when the user clicks on the Button 'btnGetCount'. Here is the code which I have tried but it doesn't count anything and counter remains at zero, though I have three TextBoxes and two CheckBoxes in the form. However, if I remove foreach loop and pass TextBox control = new TextBox(); instead of the present code then it counts the first TextBox and countTB returns the value as one.
protected void btnGetCount_Click(object sender, EventArgs e)
{
Control control = new Control();
int countCB = 0;
int countTB = 0;
foreach (Control c in this.Controls)
{
if (control.GetType() == typeof(CheckBox))
{
countCB++;
}
else if (control is TextBox)
{
countTB++;
}
}
Response.Write("No of TextBoxes: " + countTB);
Response.Write("<br>");
Response.Write("No of CheckBoxes: " + countCB);
}
You must recursively loop through other controls.
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:CheckBox ID="cb" runat="server"></asp:CheckBox>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
var controls = form1.Controls;
var tbCount = 0;
var cbCount = 0;
CountControls(ref tbCount, controls, ref cbCount);
Response.Write(tbCount);
Response.Write(cbCount);
}
private static void CountControls(ref int tbCount, ControlCollection controls, ref int cbCount)
{
foreach (Control wc in controls)
{
if (wc is TextBox)
tbCount++;
else if (wc is CheckBox)
cbCount++;
else if(wc.Controls.Count > 0)
CountControls(ref tbCount, wc.Controls, ref cbCount);
}
}
It allows gives you zero since you are counting the type of your Control control which is not available change your code to :
protected void btnGetCount_Click(object sender, EventArgs e)
{
int countCB = 0;
int countTB = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(CheckBox))
{
countCB++;
}
else if (c.GetType()== typeof(TextBox))
{
countTB++;
}
}
Response.Write("No of TextBoxes: " + countTB);
Response.Write("<br>");
Response.Write("No of CheckBoxes: " + countCB);
}
I believe you have to be recursive, this.Controls will only return the controls that are its immediate children. If the TextBox is within a control group within there, you will need to see the containers controls as well.
See the answer to this other stackoverflow: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
EDIT:
I realize the answer is for WinForms, but the solution still applies.
Just by doing some minor change in the code suggested by Alyafey, pcnThird and Valamas I wrote this code which works.
protected void btnGetCount_Click(object sender, EventArgs e)
{
int countCB = 0;
int countTB = 0;
foreach (Control c in form1.Controls) //here is the minor change
{
if (c.GetType() == typeof(CheckBox))
{
countCB++;
}
else if (c.GetType()== typeof(TextBox))
{
countTB++;
}
}
Response.Write("No of TextBoxes: " + countTB);
Response.Write("<br>");
Response.Write("No of CheckBoxes: " + countCB);
}