How to call the same form(WinForm) in the desktop application using c#

vasanth 0 Reputation points
2024-02-09T11:46:41.8666667+00:00

already i'm opened winform from there i have add to new form to get a search window in the search window ,if the user input some number from 1 to 10 again after clicking ok button in the search window.it should navigate to the same window which is opened initially and the content in the window has to change now in the front-end.

but in my case it is opening new window form instead how to fix this below are my code snippets:

  private void button1_Click(object sender, EventArgs e)
        {
            //FrmAttemptInstruction frmAttemptInstruction = new FrmAttemptInstruction() ;
            //Close(frmAttemptInstruction);
            //textvalue = textBox1.Text;            
            //this.ShowDialog(new FrmAttemptInstruction());


            if (Application.OpenForms.OfType<FrmAttemptInstruction>().Any())
            {
                //Application.OpenForms.OfType<FrmAttemptInstruction>().First().Close();
                FrmAttemptInstruction frmAttemptInstructions = new FrmAttemptInstruction();
                frmAttemptInstructions.Hide(); 
            }

            FrmAttemptInstruction frmAttemptInstruction = new FrmAttemptInstruction();
            textvalue = textBox1.Text;
            frmAttemptInstruction.Show();

        }

        private void Close(FrmAttemptInstruction frmAttemptInstruction)
        {

                frmAttemptInstruction.Close();
        }
    }

     protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
in the above method i got issue in dispose method
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,015 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,376 Reputation points Microsoft Vendor
    2024-02-09T12:18:14.03+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.