不同形式的调用方法给Txtboxlines计数为零

时间:2019-01-29 17:10:21

标签: c#

我有一个方法MainTask();其中包含以下代码。

 int i = this.textBox1.Lines.Length

- 当我以不同的形式调用方法MainTask()时,如果我以相同的形式直接调用该方法可以正常工作,则文本框计数为零。有什么问题吗?

编辑:

 private void button1_Click(object sender, EventArgs e)
        {
            if (rankDomain.Text == "")
            {
                MessageBox.Show("Please enter the SERP tracking url ex:domain.com, you have 60 seconds time.");
                WaitNSeconds(60);
            }
            else
            {
                RankingsSheetName rsn = new RankingsSheetName();
                rsn.Show();
            }

        }

   public async void MainTask(ExcelWorksheet rankingSheet=null, ExcelPackage excelPkg=null)
            {
                int excelRowNumber = 2;
                String currentDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                //rankingSheet.Cells[1, 1].Value = "Keyword";
                //rankingSheet.Cells[1, 2].Value = "Featured Snippet";
                //rankingSheet.Cells[1, 2].Value = "Comp in 1st postion";
                //rankingSheet.Cells[1, 2].Value = "Comp in 2nd postion";
                //rankingSheet.Cells[1, 2].Value = "Comp in 3rd postion";
                //rankingSheet.Cells[1, 2].Value = "SERPS";
                    int waitTime;
                    textBox1.Enabled = false;
                    btnExport.Enabled = false;
                    button2.PerformClick();
                    button2.Enabled = false;
                    d.dataGridView1.Rows.Clear();
                    d.dataGridView1.Refresh();

                    button1.Enabled = false;
                    btnStop.Enabled = true;
                    completedkeywordsHelp.Lines = textBox1.Lines;
                    Random rnd = new Random();
    MessageBox.Show(textBox1.Lines.Length.ToString());
    }

public partial class RankingsSheetName : Form
    {
        public RankingsSheetName()
        {
            InitializeComponent();
        }
        public ExcelWorksheet rankingSheet;
        public ExcelPackage excelPkg;

        String currentDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        private void btnOk_Click(object sender, EventArgs e)
        {
            CreateExcel();
        }

        public void CreateExcel()
        {
            Form1 fm = new Form1();
            List<string> sheetNames = new List<string>();
            FileInfo fileInfo = new FileInfo(currentDir + "RankingExcelFileAutoSave.xlsx");
            if (File.Exists(currentDir + "RankingExcelFileAutoSave.xlsx"))
            {
                excelPkg = new ExcelPackage(fileInfo);
                foreach (ExcelWorksheet eSheet in excelPkg.Workbook.Worksheets)
                {
                    sheetNames.Add(eSheet.ToString());
                }
            }
            int sheetSearchCount = sheetNames.Where(s => s.Equals(txtBoxSheetName.Text, StringComparison.CurrentCultureIgnoreCase)).Count();
            if (File.Exists(currentDir + "RankingExcelFileAutoSave.xlsx"))
            {
                    if (sheetSearchCount == 1)
                    {
                        MessageBox.Show("Name already exists in the excel file, please enter different name");
                        excelPkg.Dispose();
                    }
                    else
                    {
                            rankingSheet = excelPkg.Workbook.Worksheets.Add(txtBoxSheetName.Text);
                            excelPkg.SaveAs(fileInfo);
                            fm.MainTask(rankingSheet, excelPkg);


                    }
            }
            else
            {
                rankingSheet = excelPkg.Workbook.Worksheets.Add(txtBoxSheetName.Text);
                excelPkg.SaveAs(fileInfo);
                    this.Hide();
                f.MainTask(rankingSheet, excelPkg);
            }

}

我添加了更多代码。请帮助我。谢谢。

0 个答案:

没有答案