How to click button to show/hide another form in one WinForms App program.

john zyd 421 Reputation points
2021-10-24T19:48:21.003+00:00

Hello:
I am using Visual Studio 2019 (Version 16.11.5), I want to create one WinForms App project, in which I need two Forms: Form1 and Form2.
When the application runs, at first, only Form1 is shown, Form2 is hide.
On both forms, I add one button. On Form1, when I click on the button, the Form1 will be hidden, and Form2 will be shown; on Form2, when I click on the button, Form2 will be hidden, and Form1 will be shown.
I need this feature for my project, in my project, I need one form to download some files, and I need another form to upload some files when a few files have been downloaded in form1. On each form, there will be a few buttons to trigger some functions to process the files.
Please advise,
Thanks,

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2021-10-24T21:31:13.157+00:00

    As a starting point I would probably just create Form2 inside Form1 and I'd give Form2 a reference to Form1 to allow it to switch back visibility to the original form. Disclaimer: I don't use Winforms often so this may be a naive approach, but it should be reasonable enough:

    Form1:

    public partial class Form1 : Form {
        private Form2 _next;
    
        public Form1() {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e) {
            Hide();
            _next ??= new Form2(this);
            _next.Show();
        }
    
        private async void button2_Click(object sender, EventArgs e) {
            const string fileUri = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Domestic_Cat_Face_Shot.jpg/1920px-Domestic_Cat_Face_Shot.jpg";
    
            using HttpClient client = new HttpClient();
            using Stream stream = await client.GetStreamAsync(fileUri);
            using FileStream fileStream = new FileStream(@"d:\files\cat.jpg", FileMode.OpenOrCreate);
    
            await stream.CopyToAsync(fileStream);
        }
    }
    

    Form2:

    public partial class Form2 : Form {
        private readonly Form1 _previous;
    
        public Form2(Form1 previous) {
            InitializeComponent();
            _previous = previous;
        }
    
        private void button1_Click(object sender, EventArgs e) {
            Hide();
            _previous.Show();
        }
    
        private async void button2_Click(object sender, EventArgs e) {
            const string serverHost = "https://...";
            const string filepath = @"d:\files\cat.jpg";
    
            string filename = Path.GetFileNameWithoutExtension(filepath);
    
            using HttpClient client = new HttpClient();
    
            using FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
    
            HttpResponseMessage response = await client.PostAsync(serverHost, new MultipartFormDataContent {
                { new StreamContent(stream), "file", filename }
            });
        }
    }
    

    The former has two buttons (to create/show the next form & to download a single file). The second snippet has the same buttons, except it uploads that file from disk to a server as multi-part form data. From here it's dependant on the details of your requirements, i.e. how you show these files for selection on Form2, how you communicate downloads from your Form1 to your Form2 (e.g. via an event, observer, repository, or a combination.) Hopefully this should be enough to get you started though.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.