Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi guys, I am a newbie to c# coding. I wanted to know that how can i pass the values of the string str1 to another form and display it in another windows form from the below code. Well i can display it in a messagebox as "MessageBox.Show(str1);" but i want to pass the value of str1 and display it in another form.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using MyExcel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Microsoft.Vbe.Interop;
using System.Diagnostics;


namespace Loans
{

    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {




            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select an Excel File";
            fdlg.InitialDirectory = @"d:\test";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;





            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("selected file is :" + fdlg.FileName);

            }

            MyExcel.Application xlApp;
            MyExcel.Workbook xlWorkBook;
            MyExcel.Worksheet xlWorkSheet;
            MyExcel.Range range;

            string cellValue;
            int rCnt;
            int cCnt;
            int rw = 0;
            int cl = 0;

            Loans.Form2 frm = new Loans.Form2();


            xlApp = new MyExcel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@fdlg.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (MyExcel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            rw = range.Rows.Count;
            cl = range.Columns.Count;

            //MessageBox.Show("Working");
            for (rCnt = 1; rCnt <= rw; rCnt++)
            {
                for (cCnt = 1; cCnt <= cl; cCnt++)
                {

                    string str = Convert.ToString((range.Cells[rCnt, "N"] as MyExcel.Range).Value2);

                    if (str == "3" || str == "4" || str == "5" || str == "6" || str == "7")
                    {
                        string str1 = Convert.ToString((range.Cells[rCnt, cCnt] as MyExcel.Range).Value2);
                        MessageBox.Show(str1); //want to display the values
                                               //of str1 in a seperate form  


                    }


                }
            }



            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

        }

        public void Show(string text)
        {

            this.Show();
        }




        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeComponent();
        }


    }
}
Posted
Updated 17-Feb-17 21:36pm

1 solution

That depends on exactly what form you want to display it in!
If you don't have a form yet, then it's pretty easy. Create a new form (by adding it to your Project - right click the Project in the Solution Explorer pane and it's in the drop down menu) Add a label or text box to your form in the designer and give it an appropriate name: theControlIWantToDisplayIn will do for this discussion. Then add a property to teh form:
C#
public string DisplayThis
   {
   get { return theControlIWantToDisplayIn.Text; }
   set { theControlIWantToDisplayIn.Text = value; }
   }
Then, when you want to display the message from your original form:
C#
MyNewForm f = new MyNewForm();
f.DisplayThis = str1;
f.ShowDialog();


If your form is already visible, and you want to add info to it, then it's more complex, but one of these should help:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900