我想從Form2訪問InitializeWithFile方法到Form1 .Form1是我的起始表單。訪問方法c#winforms時,對象引用未設置爲對象的實例
在Form1:
Form2 f2;
public Form1(Form2 _f2)
{
f2 = _f2;
StringA = @"D://abc.csv";
InitializeComponent();
string s = textBox1.Text;
try
{
csvData = GetDataTabletFromCSVFile(StringA);
dataGridView1.DataSource = csvData;
//var f = new Form2();
_f2.InitializeWithFile(StringA);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
return;
}
public Form1()
: this(null)
{
}
在FORM2:
public void InitializeWithFile(string csFileName)
{
StreamReader hGetCommaDelimiterGrid = new StreamReader(csFileName);
m_arycsFullData.Clear();
m_arycsFilteredData.Clear();
m_arycsLogsExcluded.Clear();
m_arycsLogsIncluded.Clear();
m_arycsSidesExcluded.Clear();
m_arycsSidesIncluded.Clear();
m_arycsMainCodesExcluded.Clear();
m_arycsMainCodesIncluded.Clear();
m_arycsMinorCodesExcluded.Clear();
m_arycsMinorCodesIncluded.Clear();
while (bReadingFile)
{
bReadingFile = Convert.ToBoolean(hGetCommaDelimiterGrid.ReadToEnd());
if (bReadingFile)
{
csCell = GetFilteredData(iRow, SORT_COLUMN_LOG);
//AddOptionsText(SORT_INDEX_LOG,csCell);
AddIncludedText(SORT_INDEX_LOG, csCell, true);
csCell = GetFilteredData(iRow, SORT_COLUMN_SIDE);
//AddOptionsText(SORT_INDEX_SIDE,csCell);
AddIncludedText(SORT_INDEX_SIDE, csCell, true);
csCell = GetFilteredData(iRow, SORT_COLUMN_MAIN_CODE);
//AddOptionsText(SORT_INDEX_MAIN_CODE,csCell);
AddIncludedText(SORT_INDEX_MAIN_CODE, csCell, true);
csCell = GetFilteredData(iRow, SORT_COLUMN_MINOR_CODE);
//AddOptionsText(SORT_INDEX_MINOR_CODE,csCell);
AddIncludedText(SORT_INDEX_MINOR_CODE, csCell, true);
}
iRow++;
}
}
而且CStdioFile相當於在C#??????
如果Form1是您的開始形式,如您所說,那麼您如何將一個實例化的Form2傳遞給它的構造函數?有代碼可以告訴我們調用Form1嗎?我想知道你的「Form2」沒有正確實例化。此外,您是否可以附加調試器並逐步執行代碼?如果是這樣,哪行會引發錯誤? – David
_f2.InitializeWithFile(StringA);在這條線我越來越_f2是空的 – user3074200
有這樣做的另一種方法?意味着從form1訪問form2方法? – user3074200