Would like to Show the form N number of times when user enter a Number in a text box
I will have a text box where user can enter 0-9999 . If the user enters 10 i would like to load a Form. This form will have some controls and buttons namely Save . If user enter data and click on save i would like to clear the fields and have to sh开发者_Python百科ow that form again up to the number of times(as per i said 10) corresponding to the value entered by the user in the textbox.
instead of showing the form 0-9999 times, you could pass the value (0-9999) to the form, and clear the fields on that form for 0-9999 times after user click on save and then close it.
e.g:
//on main form:
int i = 0;
//parse the textbox1.text to int and check the result:
if(!int.TryParse(textbox1.Text,out i)||i<0||i>9999)
{
//incorrect int value
MessageBox.Show("Please enter a valid value");
}
else //correct int value
{
subform mysub=new subform(i);
subform.ShowDialog();
}
//on your subform:
int timebeforeclose=0;
public subform(int count)
{
timebeforeclose=count;
}
private void btnSave_Click(object sender, EventArgs e)
{
//1.save your data or whatever...
//2.empty any fields you want..
//update timebeforeclose:
timebeforeclose--;
//check the timebeforeclose:
if(timebeforeclose==0)
{
this.Close(); //close this form when reaches the specified number.
}
}
Simplest way would probably be a simple for loop.
for (int i=0; i < textboxvalue; i++)
{
MyForm form = new MyForm();
form.ShowDialog();
}
for (int i=0; i < textboxvalue; i++)
{
MyForm form = new MyForm();
form.ShowDialog();
}
This will show form n times but will appear once visible form is closed. ShowDialog stops the execution at parent.
Rather use form.Show()
精彩评论