M
mp
I'm playing with a little app to show some values from a spreadsheet(excel)
First i had some code in the form to open and read the excel files
Then I was thinking about the idea of the form being coupled as little as
possible to the rest of the code
So I took the excel stuff out of the form and created an excelReader class
to do all that stuff...
now if i want to show some results in the form, is the following an "ok" way
to do it or is there a better pattern i should be using?
it's clunky but at some point the form has to know what control it want to
display what info in...so there's some inevitable coupling at some point,
no???
thanks
mark
ps i tried to get rid of the switch statement below by using foreach but
found a lable control isn't a control apparently
foreach (Control ctl in this.Controls)
{
//label is not a control!!!
Debug.Print("Control " + ctl.Name);
if (ctl.Name == "lbl" + rangeName)
{
ctl.Text = rangeName + ": " + String.Format("{0:
###,###.00}", rng1.Value2);
}
}
(no label names are output)
code snip follows:
...in the form (show value of certain ranges in a lable in the form)
private void btnReadExcel_Click(object sender, EventArgs e)
{
string filename = @"Path to file.xls";
//open excel file and get reference to specific worksheet
ExcelReader xlReader=new ExcelReader(filename, sheetname);
//range names i want to view
string [] rangeNames = new string[] {"Cash","Stocks", "Bonds" ,
"RealEstate", "Total"};
//show values in lables(or other control later )
for (int i = 0; i < rangeNames.Length; i++)
{
this.ShowRangeLabel(xlReader, rangeNames);
}
xlReader.Close();
}
private void ShowRangeLabel( ExcelReader xlReader,string rangeName)
{
Excel.Range rng1;
try
{
rng1 = xlReader.GetRange(rangeName);
}
catch
{
MessageBox.Show("Range not found " + rangeName);
return;
}
switch (rangeName)
{
case "Cash":
this.lblCash.Text = "Cash: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
case "Stocks":
this.lblStocks.Text = "Stocks: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
case "Bonds":
this.lblBonds.Text = "Bonds: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
case "RealEstate":
this.lblRealEstate.Text = "Real Estate: " +
String.Format("{0: ###,###.00}", rng1.Value2);
break;
case "Total":
this.lblTotal.Text = "Total: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
default:
MessageBox.Show("Unrecognized range name <" + rangeName
+ ">");
break;
}
}
First i had some code in the form to open and read the excel files
Then I was thinking about the idea of the form being coupled as little as
possible to the rest of the code
So I took the excel stuff out of the form and created an excelReader class
to do all that stuff...
now if i want to show some results in the form, is the following an "ok" way
to do it or is there a better pattern i should be using?
it's clunky but at some point the form has to know what control it want to
display what info in...so there's some inevitable coupling at some point,
no???
thanks
mark
ps i tried to get rid of the switch statement below by using foreach but
found a lable control isn't a control apparently
foreach (Control ctl in this.Controls)
{
//label is not a control!!!
Debug.Print("Control " + ctl.Name);
if (ctl.Name == "lbl" + rangeName)
{
ctl.Text = rangeName + ": " + String.Format("{0:
###,###.00}", rng1.Value2);
}
}
(no label names are output)
code snip follows:
...in the form (show value of certain ranges in a lable in the form)
private void btnReadExcel_Click(object sender, EventArgs e)
{
string filename = @"Path to file.xls";
//open excel file and get reference to specific worksheet
ExcelReader xlReader=new ExcelReader(filename, sheetname);
//range names i want to view
string [] rangeNames = new string[] {"Cash","Stocks", "Bonds" ,
"RealEstate", "Total"};
//show values in lables(or other control later )
for (int i = 0; i < rangeNames.Length; i++)
{
this.ShowRangeLabel(xlReader, rangeNames);
}
xlReader.Close();
}
private void ShowRangeLabel( ExcelReader xlReader,string rangeName)
{
Excel.Range rng1;
try
{
rng1 = xlReader.GetRange(rangeName);
}
catch
{
MessageBox.Show("Range not found " + rangeName);
return;
}
switch (rangeName)
{
case "Cash":
this.lblCash.Text = "Cash: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
case "Stocks":
this.lblStocks.Text = "Stocks: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
case "Bonds":
this.lblBonds.Text = "Bonds: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
case "RealEstate":
this.lblRealEstate.Text = "Real Estate: " +
String.Format("{0: ###,###.00}", rng1.Value2);
break;
case "Total":
this.lblTotal.Text = "Total: " + String.Format("{0:
###,###.00}", rng1.Value2);
break;
default:
MessageBox.Show("Unrecognized range name <" + rangeName
+ ">");
break;
}
}