C# Tutorial – How to change data between forms in Visual Studio

Hi, in this tutorial we will demonstrate how to change data between forms in visual studio. We will be making an image viewer, that needs to have its image location set on another form and when we click send it will update the value on the main form. This tutorial is aimed to aid those who want to create their own custom dialog boxes in windows form. You can use this method in any sort of application that requires you to make a custom dialog box that will capture any sort of user data and then with a click of a button it can be applied to the main form. Follow along on this quick tutorial and hopefully you can be on track to make your own desktop application.

Lesson Objectives –

  1. Create a windows form application in visual studio with C#
  2. Create two forms in the project
  3. Add picture boxes, labels, buttons and text boxes to the forms
  4. Add events and functions to the form
  5. Allow the program to load URL images to the picture box
  6. Check if the URL is valid in order for the picture to be loaded
  7. If file loaded is a local file then load the picture differently.

Video Tutorial

Source Code –

Form1.CS this is the main form source code

namespace Pass_Values_Form2_to_Form1_MOO_ICT
{
    public partial class Form1 : Form
    {

        public static string filename = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void AddImageClickEvent(object sender, EventArgs e)
        {
            Form2 newForm = new Form2();

            DialogResult result = newForm.ShowDialog();

            if (result == DialogResult.OK)
            {
                if (filename.StartsWith("http://") || filename.StartsWith("https://"))
                {
                    pictureBox1.Load(filename);
                    label1.Text = filename;
                }
                else
                {
                    pictureBox1.Image = Image.FromFile(filename);
                    label1.Text = filename;
                }
            }
        }
    }
}

In Form1, there’s a static string variable named filename, initialized as an empty string. This variable will be used to store the filename passed from Form2 to Form1.

The constructor of Form1 initializes the form’s components, likely including controls like buttons and labels, using the InitializeComponent() method.

The AddImageClickEvent method is an event handler for a click event on a button or other control (not shown in the provided code). When triggered, it creates an instance of Form2 and displays it as a modal dialog using the ShowDialog() method.

After displaying Form2, the method checks the result of the dialog. If the result is DialogResult.OK, it proceeds to handle the selected filename.

Within the handling of the selected filename, the method checks if the filename starts with “http://” or “https://”. If it does, it assumes the filename is a URL and loads the image from that URL into a PictureBox named pictureBox1. Otherwise, it assumes the filename is a local file path and loads the image from that file into the PictureBox.

Regardless of whether the filename is a URL or a local file path, the filename is assigned to the Text property of a Label named label1, likely displaying the filename or some information related to the image.

In summary, this code enables users to select an image file using Form2, and then displays the selected image in Form1. The selected filename is stored in a static variable named filename and is accessible from other parts of the application.

Form2.CS This is the picture browser form source code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pass_Values_Form2_to_Form1_MOO_ICT
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void BrowseForImageClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Images Only | *.jpg;*.jpeg;*.png;*.gif";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = Path.GetFullPath(ofd.FileName);
            }
        }

        private void SendOK(object sender, EventArgs e)
        {
            Form1.filename = textBox1.Text;
        }
    }
}

The provided C# code resides within the namespace “Pass_Values_Form2_to_Form1_MOO_ICT” and defines a class named “Form2”. This class represents a secondary form within the Windows Forms application.

The constructor of Form2 initializes the visual components of the form.

The method “BrowseForImageClick” serves as an event handler for selecting an image file. When a button or control is clicked, this method creates an instance of OpenFileDialog, which allows users to select files. It restricts the file selection to image files only (with extensions .jpg, .jpeg, .png, and .gif). If the user selects a file and confirms their selection, the full path of the selected file is displayed in a TextBox named “textBox1”.

The method “SendOK” acts as an event handler for confirming the selected image file. Upon confirmation (e.g., clicking an OK button), it assigns the full path of the selected file (stored in “textBox1”) to the static variable “filename” of the “Form1” class. This static variable facilitates passing the selected filename from Form2 to Form1, enabling further processing or display of the selected image.

In essence, this code segment enables users to browse for and select an image file in Form2, and upon confirmation, the selected filename is stored in a static variable of Form1, allowing it to be accessed and utilized in other parts of the application.

Tags:



Comments are closed.