Changing a tab and clearing content - c#

I have a program which gives two options through which to test a student's knowledge of complex numbers. However, I want the content in the tab (labels, textboxes) to be cleared when the tab is changed. Is there a method I can use to do this?

just handle the SelectedIndexChanged event of TabControl and retrieve all the controls within the tab. Now you can loop through controls and do whatever you want with them, like this-
// SelectedIndexChange Event
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get Selected Tab
var selectedTab = tabControl1.SelectedTab;
foreach (Control ctrl in selectedTab.Controls)
{
if (ctrl is TextBox)
{
(ctrl as TextBox).Text = string.Empty;
}
if (ctrl is Label)
{
(ctrl as Label).Text = string.Empty;
}
// Other Controls....
}
}
hope it helps...!!

You could reset the value of label and textbox in Tabchange event. Take a look at
Tab Change MSDN
Hope it helps

Related

How to clear TextBoxes nested inside a TabControl, TabPage and 2 Panels?

The picture below represents a plug-in I am building.
Yellow = tabControl1; Orange = tabPage1; Blue = Main Panel (mainPanel); Green = 3 different panels inside the main panel (panel1, panel2 and panel3); White = TextBoxes (that I want to clear); DarkBlue = CheckBoxes corresponsing to the green panels
I am trying to clear any text from the TextBoxes that contain text and reset CheckBoxes when checked, with a button click.
I researched online and tried to accomplish this by the following way but it doesn't seem to be the correct way to handle this problem.
foreach (Control a in tabControl1.Controls)
{
foreach(Control b in tabPage1.Controls)
{
foreach (Control c in mainPanel.Controls)
{
foreach (Control d in panel1.Controls)
{
if (d is TextBox)
{
d.Text = " ";
}
if (d is CheckBox)
{
((CheckBox)d).Checked = false;
}
}
}
}
}
I have only shown panel1 here but tried doing the same thing for panel2 and 3 as well.
What exactly am I doing wrong here? Any help would be greatly appreciated!
You just need a simple recursive method that iterates all controls inside all child containers of a specified parent container, mainPanel, here.
If a control is of type TextBox or CheckBox, set its Text or Checked property accordingly:
(Note that you can also pass tabPage1 to the method, or any other ancestor)
ClearControls(panel1);
// or
ClearControls(tabPage1);
private void ClearControls(Control parent)
{
if ((parent == null) || (!parent.HasChildren))
return;
foreach (var ctl in parent.Controls.OfType<Control>())
{
if (ctl is TextBox txt) {
txt.Clear();
}
if (ctl is CheckBox chk) {
chk.Checked = false;
}
else {
if (ctl.HasChildren) {
ClearControls(ctl);
}
}
}
}
If you want to clear the TextBoxes and ComboBoxes inside of panel1 then you only need to loop through panel1's controls to do it. To handle any panel, you could just write a ClearPanel(Panel) function.
private void ClearPanel(Panel panel)
{
foreach (var ctrl in panel.Controls)
{
if (ctrl is TextBox tb)
{
tb.Clear();
}
else if (ctrl is CheckBox chkBx)
{
chkBx.Checked = false;
}
}
}
private void ClearPanel1Button_Click(object sender, EventArgs e)
{
ClearPanel(panel1);
}
Iterating through the controls in tabControl1, tabPage1, etc, etc adds unnecessary overhead if you already have a reference to the Panel you're dealing with. What's more, if any of the controls in the outer loops have an empty Controls collection then the inner loops won't execute. I suspect this might be why your code isn't working.
Note that the above solution will not handle any TextBox or ComboBox that is inside another container inside panel1. So, if panel1 had a GroupBox inside it which contained TextBoxes or ComboBoxes, they wouldn't be cleared. See the recursive solution below to handle that situation.
EDIT: After re-reading your question I thought maybe you want to clear all TextBox and CheckBox controls on the Form.
If you're needing to clear every TextBox or CheckBox on the Form, you can do this with recursion.
private void Clear(Control ctrl)
{
if (ctrl is TextBox tb)
{
tb.Clear();
}
if (ctrl is CheckBox chkBx)
{
chkBx.Checked = false;
}
foreach (Control child in ctrl.Controls)
{
Clear(child);
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
Clear(this);
}
You could pass any Control to Clear(Control), so if you only wanted to do the TextBoxes and ComboBoxes on tabPage1 you could call Clear(tabPage1). This will clear all the TextBoxes and ComboBoxes on tabPage1, even if they're in a Panel or GroupBox or some other container.

To Enable right click in disabled control

I Have a textbox which is disabled and has value .And i want to enable right click option to copy the disabled value from textbox (Windows application).Pls help me to do this.
Try this, keeping in mind that you have to have your contextmenustrip added:
private void YourFormName_Load(object sender, EventArgs e)
{
ContextMenu mnu = new ContextMenu();
MenuItem mnuCopy = new MenuItem("Copy");
mnuCopy.Click += (sen, ev) =>
{
System.Windows.Forms.Clipboard.SetText(YourTextBoxName.Text);
};
mnu.MenuItems.AddRange(new MenuItem[] { mnuCopy });
YourTextBoxName.ContextMenu = mnu;
}
private void YourFormName_MouseUp(object sender, MouseEventArgs e)
{
Control ctl = this.GetChildAtPoint(e.Location);
if (ctl != null && !ctl.Enabled && ctl.ContextMenu != null)
ctl.ContextMenu.Show(this, e.Location);
}
When you click on a disabled element in a page, the event is handled by the parent element of the disabled element.
for example if your textbox is in a page then the page handles it. if the text box is in a different container like div, then that container will handle the mouse click event.
for your situation, you could write a handler on the parent element.
a javascript function that will catch the event and it can read the value for you. for instance, the JS function can change the disabled property to false, read the value, and then disable the textbox again.
Coming back to Vijaya's answer, I handled the problem by just placing the control with Dock=Fill into a panel control with zero padding and margin. So you would do your things in the MouseUp event of the panel instead.

get text of a selected dynamically textbox c#

I have on my winform an usercontrol and I create multiple usercontrols at every button click(at runtime).My usercontrol has an textbox. Also,on winform I have a simple textbox . I want ,when I select an usercontrol,the text from the dynamical textbox to appear also in the simple textbox. In my code it says that the textbox from usercontrol is not in the current context. My code:
private void Gettext()
{
int i = 0;
Control[] txt = Controls.Find("txtBox" + i.ToString(), true);//here I search for the dynamical textbox
foreach (Control c in panel1.Controls)
{
if (c is UserControl1)
{
if (((UserControl)c).Selected)
txtSimple.Text= txtBox[0].Text ;
}
i++;
}
I don't know if I understood your question correctly:
The structure of your form looks something like this:
Your form has a Panel panel1 that has many UserControls of the type UserControl1, created on runtime, and one TextBox txtSimple.
Every UserControl has a TextBox named ["txtBox" + i]
on select you want to synchronize texts of txtSimple and TextBox of selected UserControl
Then:
int i=0;
foreach (Control c in panel1.Controls)
{
if (c is UserControl1)
{
if (((UserControl)c).Selected)
{
TextBox dynTxtBox = (TextBox)c.Controls["txtBox" + i];
txtSimple.Text= dynTxtBoxe.Text;
}
}
i++;
}
If you can't find your TextBox this way, it probably means that its name is not set correctly.
Also, if you have only one TextBox on your UserControl then there's normally no need to name it in such a specific way (I mean from your code I assumed you have txtBox0 on your first user control, txtBox1 on your second and so on). You can simply name it "txtBox", then access it like this:
txtSimple.Text = selectedUserControl.Controls["txtBox"].Text;
Control names are unique in a Controls collection of a Control, UserControl and Form.
Control[] txt = ...
txtSimple.Text= txtBox[0].Text ;
May be replace txtBox[0].Text to txt[0].Text ?
Well for a start
Control[] txt = Panel1.Controls.Find("txtBox" + i.ToString(), true)
Then
foreach (Control c in txt) // txt???
{
UserControl1 uc = c as UserControl1;
if (uc != null)
{
if (uc.Selected) txtSimple.Text= uc.Text ;
}
}
Then if if you are are testing for UserControl1, you should also cast to UserControl1 not UserControl
UserControl1 is an extremely bad name for it..
I'm not even going to mention the assumption that all controls have a name starting with txtBox and that no other controls have...
And the entire thing dies if more than one control is selected when it runs.
you need to have a Selected event on your UserControl.
//in UserControl
public event EventHandler Selected;
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
if(Selected!=null)
Selected(this,null);
}
now subscribe to Selected event of UserControl when you dynamically create it. Like this:
UserControl control = new UserControl();
control.Selected += myControl_Selected;
private void myControl_Selected(object sender, EventArgs e)
{
UserControl control = (UserControl)sender;
textBox2.Text = control.Text;
}
I hope this helps.

Find Input Type Controls from Windows Form

I have Window Application and have one popup dialog(Form) with some input controls(TextBox, ComboBox) and other controls like PictureBox, Label. My form have two Mode 1) Add or Edit mode 2) View Mode. In View Mode user can only see details and also can copy input value(e.g user can copy TextBox value).
If form mode is View then I want to set read only property to true for all Input controls of form with Iterate One by one control of my Forma(using for each). But I don't have idea about How can I know particular control is a Input type control. System.Windows.Forms.Control does not have ReadOnly property. I found that I can use Enable property for my solution but problem is that user can not copy text value from TextBox if Enable set to false.
Can any one help me How can I know particular control is a Input type Control.
Thanks.
System.Windows.forms.clipboard.clear();
try this.
This code may help you:
foreach(Control ctl in form.Controls)
{
if (ctl is TextBox)
{ }
if (ctl is CheckBox)
{ }
if (ctl is ComboBox)
{ }
/* etc */
}
I usually cycle through all the controls on the form and then assess each by type. A little bit of work to start with but once in place you can add more controls without having to worry about setting them individually.
foreach (Control c in formMain.Controls)
{
if(c.GetType()==typeof(ComboBox))
{
ComboBox cb = (ComboBox) c;
//do something
}
else if(c.GetType()==typeof(TextBox))
{
TextBox t = (TextBox) c;
t.ReadOnly = true;
}
}
I also use the same process for setting generic event handlers e.g. saving a control value to settings on text change. So if you create settings with the same name as your control you can put something like this in the event handler ...
private void TextBoxTextchanged(object sender, EventArgs e)
{
TextBox t = (TextBox) sender;
Settings.Default[t.Name] = t.Text;
Settings.Default.Save();
}
I think TextBoxBase could be the input type Control:
foreach (TextBoxBase txt in this.Controls.OfType<TextBoxBase>())
{
txt.ReadOnly = true;
}

empty textbox controls after the data is inserted / saved/ submitted in a c# winform application

I need to empty all the textbox controls after the SAVE button is clicked but the user. I have around 10 of them. How do i clear text from them all simultaneously. I just know about:
textbox1.Text="";
But, if i do this, then i need to repeat this for the no. of textbox controls on my Form, that would be a labor task instead of programmer?
Please guide.
Try this
foreach(TextBox textbox in this.Controls.OfType<TextBox>())
{
textbox.Text = string.Empty;
}
If you want recursivly clear all textboxes use this function.
void ClearTextBoxes(Control control)
{
foreach(Control childControl in control.Controls)
{
TextBox textbox = childControl as TextBox;
if(textbox != null)
textbox.Text = string.Empty;
else if(childControl.Controls.Count > 0)
ClearTextBoxes(childControl);
}
}
If you have all the textboxes on a form without panels or group boxes, you can do this:
foreach (var conrol in Controls)
{
var textbox = conrol as TextBox;
if (textbox != null)
textbox.Clear();
}
If you have a panel, use panel.Controls instead.
You could use the Linq API described in the following article:
http://www.codeproject.com/KB/linq/LinqToTree.aspx#linqforms
This allows you to apply Linq-to-XML style queries on Windows Forms. The following will clear all the TextBox controls that are descendants of 'this':
foreach(TextBox textbox in this.Descendants<TextBox>()
.Cast<TextBox>())
{
textbox.Text = string.Empty;
}
If you want to clear everything on the form, I would suggest a pair of utility function such as:
public static void ClearAllControls(Control.ControlCollection controls)
{
foreach (var control in controls)
ClearAllControls(control);
}
public static void ClearAllControls(Control control)
{
var textBox = control as TextBox
if (textBox != null)
{
textBox.Text = null;
return;
}
var comboBox = control as ComboBox;
if (comboBox != null)
{
comboBox.SelectedIndex = -1;
return;
}
// ...repeat blocks for other control types as needed
ClearAllControls(control.Controls);
}
Call the first method, passing the form's Controls collection, and it will recursively drill down through panels, groups, etc, clearing all the controls it knows about. You'll have to add a block for each different control type, but at least you only have to do it once. It's a bit brute-force, but it's not the kind of code that ends up running in a loop, and it runs plenty fast, anyway.
The final line, which does the recursion, will only be reached if the current control being worked on hasn't already proven to be one of the known types, so you don't have to worry about accidentally "drilling into" things like TextBoxes, looking for child controls that won't be there.

Categories