skip to Main Content

So, I’m working on a C# and .NET Windows Forms project using Visual Studios 2022. The project contains three different forms, but for this forum post, we’ll only be talking about the first two forms. The first form is called frmHome and is the default form that displays when the code is ran. When the user clicks the "View" button on frmHome, the second form pops up, being frmViewCollection. The goal is to get the string of the highlighted item in the list of frmHome and have it display in the Collection Name text box on frmViewCollection when the "View" button is clicked on frmHome.

Here’s my code for frmHome:

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

namespace ProjectAlexKadyn
{

    public partial class frmHome : Form
    {
        public string collection;
        public frmHome()
        {
            InitializeComponent();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            collection = lstCollections.Text.ToString();
            frmViewCollection frmViewCollection = new frmViewCollection();
            frmViewCollection.collectionName = collection;
            frmViewCollection.Show(this);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            
            this.Close();

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }
    }
}

Here’s my code for frmViewCollection:

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

namespace ProjectAlexKadyn
{
    public partial class frmViewCollection : Form
    {
        public double totalValue;
        public string collectionName;
        public frmViewCollection()
        {
            InitializeComponent();

            txtCollectionName.Text = collectionName; //Currently doesn't display variable value in txtCollectionName
            totalValue = 0.00;
            txtTotalValue.Text = totalValue.ToString("C", CultureInfo.CurrentCulture);

       
                /*
                 * Oh boy it's about to get slightly hairy...
                 * 
                 * Prerequisites for this part of the code:
                 * 
                 * - We need to start doing the 4D array with all the data of each collection that is saved in the current session
                 * - The 4D array will contain these four variables: Collection name, model of each item, short description of each item, each item's value
                 * - Every time a new collection is saved, it saves the data of the collection to the 4D array and adds collectionName to lstCollections in frmHome
                 * - Alex, if you don't know how 4D arrays work, research them real quick and understanding what I'm trying to do 
                 * should be extremely straight forward with your new found knowledge afterwards 
                 * 
                 * Goal:
                 * 
                 * The goal of this block of code is to set the default total value of the collection in txtTotalValue to the actual value of the collection when btnView is 
                 * clicked in frmHome, that's if the collection is not the default "Add New Collection" and was a previously saved collection with a value greater than zero.
                 * 
                 * How the Code Should Work:
                 * 
                 * 1. Matches collectionName of the class to the collectionName of the 4D array
                 * 2. Indexes through each modelIdentifier dimension of the array of the specific collectionName value
                 * 3. As it's indexing through each modelIdentifier under that collectionName, it grabs the itemValue from the itemValue dimension under that modelIdentifier
                 * 4. Sums up the total of every itemValue and sets totalValue of the class to that summed-up total
                 * 5. Converts totalValue to currency as a string and displays it in txtTotalValue
                 * 6. Put the code in this block and it should do it's thing once btnView is clicked in frmHome
                 */
            

        }

        private void btnAddNewItem_Click(object sender, EventArgs e)
        {
            
            

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSaveAsCurrent_Click(object sender, EventArgs e)
        {

        }

        private void btnSaveAsNew_Click(object sender, EventArgs e)
        {

        }
    }
}

In frmHome‘s code, the code that matters is the btnView_Click event handler. In frmViewCollection‘s code, please ignore that massive comment instructing my partner on the next steps, but the code that matters happens before that comment. This code should work, and it’s not throwing me any errors, but the string isn’t being displayed in the Collection Name text box.

Here’s images of the actual GUIs of each form:

Image of frmHome’s GUI
Image of frmViewCollection’s GUI

2

Answers


  1. To pass a string from the first form to a second form in a C# .NET Windows Forms application, you can follow a straightforward approach by creating a public property or method in the second form that you can access from the first form.

    Pass the Value from the First Form

    // When you want to show Form2 from Form1
    Form2 form2 = new Form2();
    form2.PassedValue = "The string you want to pass"; // Set the value here
    form2.Show();
    

    Create the public property in the Second Form

    public partial class Form2 : Form
    {
        public string PassedValue { get; set; }
    
        public Form2()
        {
            InitializeComponent();
        }
    
        // Use this method to update the UI when the form is shown or loaded
        private void Form2_Load(object sender, EventArgs e)
        {
            // Assuming you have a TextBox called textBox1 in Form2
            textBox1.Text = PassedValue;
        }
    }
    
    Login or Signup to reply.
  2. You can add a parameter to the constructor of the second form and then call frmViewCollection(this);.

    frmHome code:

    private void btnView_Click(object sender, EventArgs e)
    {
        collection = lstCollections.Text.ToString();
        frmViewCollection frmViewCollection = new frmViewCollection(this);
        frmViewCollection.collectionName = collection;
        frmViewCollection.Show(this);
    }
    

    frmViewCollection code:

    namespace ProjectAlexKadyn
    {
        public partial class frmViewCollection : Form
        {
            private frmHome home = new();
            public frmViewCollection(frmHome home)
            {
                InitializeComponent();
                
                txtCollectionName.Text = collectionName;
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search