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;
                }
            }
        }
    }
}

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;
        }
    }
}

 

Tags:



Comments are closed.