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.