Hi @vasanth ,
Do you mean that if the user enters the same number, the app opens a window that was already open before, and if a new number is entered, a new window is created?
// FrmAttemptInstruction.cs
public partial class FrmAttemptInstruction : Form
{
// Property to store the number associated with the instruction
public int Number { get; set; }
public FrmAttemptInstruction()
{
InitializeComponent();
}
}
// MainForm.cs
public partial class MainForm : Form
{
// List to store all FrmAttemptInstruction instances
private List<FrmAttemptInstruction> attemptInstructions = new List<FrmAttemptInstruction>();
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int inputNumber;
if (int.TryParse(textBox1.Text, out inputNumber))
{
// Check if an existing instruction matches the input number
FrmAttemptInstruction existingInstruction = attemptInstructions.FirstOrDefault(inst => inst.Number == inputNumber);
if (existingInstruction != null)
{
// If an instruction for the input number exists, show it and hide others
foreach (var instruction in attemptInstructions)
{
if (instruction != existingInstruction)
instruction.Hide();
}
existingInstruction.Show();
}
else
{
// If no instruction for the input number exists, create a new one and manage visibility
FrmAttemptInstruction newInstruction = new FrmAttemptInstruction();
newInstruction.Number = inputNumber;
attemptInstructions.Add(newInstruction);
newInstruction.Show();
foreach (var instruction in attemptInstructions)
{
if (instruction != newInstruction)
instruction.Hide();
}
}
}
else
{
MessageBox.Show("Please enter a valid number!");
}
}
}
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.