获取函数中的变量

时间:2014-08-09 19:19:56

标签: c#

对不起我的英文:(

我使用过这段代码:

for (int i = val3; i < to_val3; i++)
{
    TextBox tbx4 = (TextBox)(from t4 in this.Controls.OfType<TextBox>() where t4.Name == i.ToString() select t4).First();
    str4 = tbx4.Text;

    if (!string.IsNullOrWhiteSpace(str4.Split(' ')[4]))
    {
        get_excel(str4);
    }

    else
    {
        MessageBox.Show("Error!, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
}

并将字符串"str4"发送到函数get_excel

Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook ObjWorkBook;
Microsoft.Office.Interop.Excel.Worksheet ObjWorkSheet;
ObjWorkBook = ObjExcel.Workbooks.Open("C:\\Шаблон.xls");
ObjWorkSheet = ObjWorkBook.Sheets[2];
ObjWorkSheet.Cells[4, 4] = str;
ObjWorkSheet.Cells[5, 5] = str;
ObjWorkSheet.Cells[6, 6] = str;
ObjWorkSheet.Cells[7, 7] = str;
ObjExcel.Visible = false;
ObjExcel.UserControl = true;
ObjExcel.DisplayAlerts = false;
ObjWorkBook.SaveAs("C:\\Шаблон_Изменен.xls");
ObjExcel.Quit();

我的问题:我想从循环(for)中获取变量的第一个值

1 个答案:

答案 0 :(得分:0)

我想您需要使用break声明MSDN break documentation

所以,在for循环的某个地方你需要设置break