I have a form1 (that runs the program) and form2 (that is a form for user to input). Form2 has a function that clears the user input (textboxes, checkboxes, combo boxes, it clears them).
The function looks like this:
public void CleartheForm(Control groupofcontrols)
{
foreach (Control c in groupofcontrols.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
if (c.HasChildren)
{
CleartheForm(c);
}
if (c is CheckBox)
{
((CheckBox)c).Checked = false;
}
label3.Text = "";
comboBox1.SelectedIndex = -1;
comboBox2.SelectedIndex = -1;
}
}
This works on its own. On my main form, I need to call this function, it should look like this:
I make a instance of form2 call Inputform and then:
private void Addrecord_Click(object sender, EventArgs e)
{
Inputform.ShowDialog();
if(Inputform.Addedrecord == true)
{
Inputform.Addrecord();
Inputform.CleartheForm(WHAT DO I PUT IN HERE??);
}
}
So that once a record has been added, the input form clears itself and ready for another record to be added.
The question is as above, what do I put in there? How do I call the groupofcontrols that is in the Inputform.CleartheForm()
that is located in form2 from form1?? I tried to make a public Control groupofcontrols on the top of form2 and then leave my form1 as Inputform.CleartheForm(Control groupofcontorls)
, but then it saids I don't have object reference. If I leave it blank it saids Inputform.CleartheForm();
does not take 0 arguement.
Well, if you wanted to clear all the controls in the InputForm you could write another version of CleartheForm()
without a parameter, and call the version with a parameter from it, like so:
public void CleartheForm()
{
ClearTheForm(this); // Passes all the controls in the Form.
}
Or you can just call the original ClearTheForm(Control)
passing a reference to the InputForm as an argument: InputForm.ClearTheForm(InputForm)
.
If you always want to clear all the controls, I'd write a separate parameterless method for clarity.
However, this is only useful if you want to clear all the controls in InputForm.
Because Form
is inherited from Control
, you can call your method with form as an argument - it will clear all the TextBoxes
and CheckBoxes
in it:
Inputform.CleartheForm(Inputform);
However, probably, you can simply create new (and empty!) Inputform each time.
And, for your method - you should put
label3.Text = "";
comboBox1.SelectedIndex = -1;
comboBox2.SelectedIndex = -1;
away from the loop.
From what you've said (ie you put the groupofcontrols as public on form2), this should work:
Inputform.CleartheForm(Inputform.groupofcontrols);
(This is considering that Inputform is a property or a field on your main form - which I observed it is as you're calling ShowDialog on it.)
I don't understand your program's workflow.
If it is:
You should use new instance of the Form2 for the each iteration
To do that you should remove any clear code from your Form2 and change your code to the next one:
private void Addrecord_Click(object sender, EventArgs e)
{
var inputForm = new Form2();
inputForm.ShowDialog();
if(inputForm.Addedrecord == true)
{
...Reflect changes here...
}
}
But if you need a Form2 should stay opened while a Form1 adds new record each time when user click "Ok" button on the Form2
You should call Form1 from the Form2, the next design will be nice
interface IDataTarget
{
void Update(int id, string name); // you should reflect here all of your data
}
class Form1
: Form, IDataTarget
{
public void Update(int id, string name)
{
// add new record here
}
private void AddButton_Click(...)
{
using(var form2 = new Form2(this))
{
form.ShowDialog(this);
}
}
}
class Form2
: Form
{
private readobly IDataTarget dataTarget;
public Form2(IDatatarget dataTarget)
{
this.dataTarget = dataTarget;
}
private OK_Click(...)
{
dataTarget.Update(textBox1.Text, textBox2.Text);
ClearTheControls();
}
}
Personally I would move this method into a class library. You can make it static class and then just call
FromControls.CleartheForm(Inputform.groupOfControls)
So, your new class in a class library is (and nothing has been tested here, so mistakes are likely!)
public static class FormControls
{
public static void CleartheForm(Control groupofcontrols)
{
foreach (Control c in groupofcontrols.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
if (c.HasChildren)
CleartheForm(c);
if (c is CheckBox)
((CheckBox)c).Checked = false;
}
}
}
I would remove the following code:
label3.Text = "";
comboBox1.SelectedIndex = -1;
comboBox2.SelectedIndex = -1;
...as the CleartheForm code above will clear it.
So, your code would be (remembering to add a reference to your class library)
private void Addrecord_Click(object sender, EventArgs e)
{
AddRecord();
}
private void AddRecord()
{
Inputform.ShowDialog();
if(Inputform.Addedrecord)
{
Inputform.Addrecord();
FromControls.CleartheForm(GetControlOnPage())
}
}
private control GetControlOnPage()
{
//logic to return control if needed although it may just be this:
return Inputform.groupofcontrols
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.