2017-02-18 49 views
1

我是c#編碼的新手。我想知道如何將字符串str1的值傳遞給另一個窗體並從下面的代碼中將窗體顯示在另一個窗體中。那麼我可以在消息框中顯示它爲「MessageBox.Show(str1);」但我想傳遞str1的值並以另一種形式顯示它。如何以其他形式顯示結果?

enter code here 

<pre> <code> 

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; 

命名空間貸款 {

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


} 

}

回答

1

一個好的解決方法是在第二個窗體的構造函數中發送字符串。此外,您還可以將標籤貼在你的第二個形式,並保存在字符串中該標籤:

public partial class Form2 : Form 
{ 
    private string _stringToShow; 
    public Form2(string stringToShow) 
    { 
     _stringToShow = stringToShow; 
     InitializeComponent(); 
    } 

    private void Form2_Load(object sender, EventArgs e) 
    { 
     label1.Text = _stringToShow; 
    } 
} 

而且從第一種形式我叫第二種形式:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     List<string> Ll = new List<string>() { "one", "two", "three" }; 
     string concatenate = string.Join(" ", Ll.ToArray()); 
     new Form2(concatenate).Show(); 
    } 
} 

希望它能幫助。

+0

謝謝你的解決方案。我嘗試了它,我得到一個錯誤 - >不能從'System.Collections.Generic.List '轉換爲'字符串' –

+0

你在那裏發送什麼值?看來你在列表和字符串之間進行了錯誤的轉換。我在解決方案上也添加了第一個窗口的代碼。 –

+0

我從excel文件中檢索單元格值並將它們存儲在一個字符串列表中。現在我想以第二種形式顯示檢索到的值。我沒有使用文本框。 –

-1

你可以將你的第一個表格上設置你的第二個表格上聲明一個變量:

添加這對您的第二種形式(將接收消息的形式)

public string strVariable { get; set; } 

然後,你的第一形式,示出了它之前聲明變量:

Form2 frm = new Form2(); 
    frm.strVariable = "Hello World"; 
    frm.Show(); 

在第二形式(接收器)的負荷,就可以訪問該數據,並將其顯示給用戶,在這種情況下,一個標籤,例如在表單加載時:

private void Form2_Load(object sender, EventArgs e) 
    { 
     label1.Text = strVariable; 
    }