How can i left-align a whole row with numbers - c#

I have a listbox full of numbers. I am exporting these values to excel but I Want to left align the the numbers in each cell. Every time I try this my program crashes
I tried to add an extra row of code in my for loop to try to align it left but it crashes it gives the following error:
Additional information: Exception from HRESULT: 0x800A03EC
This is what I tried:
for (int i = 0; i < listBox3.Items.Count; i++)
{
worksheet.Cells[i + 21, 1] = listBox3.Items[i].ToString();
worksheet.Cells[21, i].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}
but it crashes. What I want is that all the values align left.

Why are you using different indices for worksheet.Cells inside the loop? Shouldn't they be the same? Like this:
for (int i = 0; i < listBox3.Items.Count; i++)
{
worksheet.Cells[i + 21, 1] = listBox3.Items[i].ToString();
worksheet.Cells[i + 21, 1].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}

Related

Write to Cells in a for-loop with EPPLUS result in duplicate values

I do not understand why the following code write 2,2,2 to Excel while it should be 0,1,2. The code is straightforward - create an ExcelPackage, add a Worksheet, and iterate a loop to write values to a cells.
using (ExcelPackage p = new ExcelPackage())
{
p.Workbook.Worksheets.Add("Foo");
for (int j = 0; j < 3; j++)
p.Workbook.Worksheets["Foo"].Cells[1, 1, 1, 1 + j].Value = j;
p.SaveAs(new FileInfo(#"C:\FooFolder\Foo.xlsx"));
}
You are writting in a range instead of a unique cell. With Cells[1, 1, 1, 1 + j] you are writting from cell 1,1 to cell 1,1+j : the complete range take the assigned value.
Use Cells[1, j, 1, 1 + j] instead

C# & VS error: "make sure that the maximum index on a list is less than the list size"

I faced the error mentioned in the title when doing my homework, and simply can't find a way to remove it. Here is the method that I have this problem with:
public static double LaskeMiidi(double[] luvut)
{
double ka = Keskiarvo(luvut);
double miidi = luvut[0];
for (int i = 0; i < luvut.Length; i++)
{
if (luvut[i] - ka < luvut[i + 1] - ka) // The line error points to!
{
miidi = luvut[i];
}
else
{
miidi = luvut[i + 1];
}
}
return miidi;
}
So basically the problem is that when I say luvut[i + 1], at some point this index might become more than the length of the array is. I just can't figure out any ways to solve this problem, since I'm only a beginner with programming.
Yes, this is the problem:
for (int i = 0; i < luvut.Length; i++)
{
if (luvut[i] - ka < luvut[i + 1] - ka)
When i is luvut.Length - 1, then i + 1 will be luvut.Length - and therefore an invalid index. (For an array of length x, the valid indexes are 0 to x - 1 inclusive.) You probably want to end one iteration earlier:
for (int i = 0; i < luvut.Length - 1; i++)
That way i + 1 will still be a valid index in the array - both in the if condition and in the body of the else clause.
End your loop earlier:
for (int i = 0; i < luvut.Length - 1; i++)
This stops the loop ever getting to the point where an index would be invalid.
When i = luvut.Length -1, luvut[i + 1] will give an error as it is beyond the array bounds.
You need either:
for (int i = 0; i < luvut.Length - 1; i++)
Or else handle the luvut[i + 1] issue in another way in another If block.
Notice that when you define an array, range of items are between 0 and array.length-1. so you should write:
for (int i = 0; i < luvut.Length-1; i++)

C# Printing a border around a 2D Array

I have a multidimensional array that I'm using as a box, and I have code that generates a border around it, like this:
#######
# #
# #
# #
# #
#######
However what I don't understand is that I can have either a 0 or a 1 in the "j == ProcArea.GetUpperBound(...)" part and it works successfully without any errors or unexpected output.
int[,] ProcArea = new int[rows, columns];
//Generate border
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if (i == 0 || j == 0 || i == ProcArea.GetUpperBound(0) || j == ProcArea.GetUpperBound(1))
{
ProcArea[i, j] = 2;
}
}
}
Why does this work, and what is the correct value I should be using?
Thanks
If the number of rows and columns are the same, then GetUpperBound(0) and GetUpperBound(1) are going to return the same value.
Arrays you create in C# (unless you call Array.CreateInstance directly) are always 0-based. So GetUpperBound(0) will always return rows - 1, and GetUpperBound(1) will always return columns - 1.
So the code will "work" regardless of which upper bound you check, although I think you'll find that if rows != columns, then using GetUpperBound(0) will create a different sized box than GetUpperBound(1).
By the way, an alternate way of making your border would be:
var maxRow = ProcArea.GetUpperBound(0);
var maxCol = ProcArea.GetUpperBound(1);
// do top and bottom
for (int col = 0; col <= maxCol; ++col)
{
ProcArea[0, col] = 2;
ProcArea[maxRow, col] = 2;
}
// do left and right
for (int row = 0; row <= maxRow; ++row)
{
ProcArea[row, 0] = 2;
ProcArea[row, maxCol] = 2;
}
It's slightly more code, true, but you don't waste time checking indexes unnecessarily. Won't make a difference with small arrays, of course.
Check the documentation http://msdn.microsoft.com/en-us/library/system.array.getupperbound.aspx. Your array has 2 dimensions (rows and columns).
ProcArea.GetUpperBound(0) is equivalent to rows - 1
ProcArea.GetUpperBound(1) is equivalent to columns - 1

How to set values for multichannel matrix in Emgucv

I didn't find any explanations how to use a matrix with more than one channel im emgucv
var matrixa = new Matrix<float>(usablePoints.Count, 1, 2);
I tried with the Split() function but it didn't change the values of matrixa
var channels = matrixa.Split();
for (int i = 0; i < usablePoints.Count; ++i)
{
channels[0][i, 0] = usablePoints[i].X;
channels[1][i, 0] = usablePoints[i].Y;
}
What am I missing? How can i manipulate values of matrixa?
If you look at matrixa.Data, this will be a float[,] with the first dimension corresponding to rows and the second being the columns and channels merged into one dimension.
If the number of channels is N, the current channel is n and the current column is m, the index j of the second dimension is
j = m*N + n
So, for your example:
for (int i = 0; i < usablePoints.Count; ++i)
{
matrixa.Data[i, 0] = usablePoints[i].X;
matrixa.Data[i, 1] = usablePoints[i].Y;
}
should work.
A more complicated example: Say that we have 3 channels, 5 columns and want to set the value of the 2nd row, 4th column and 3rd channel to 1:
j = m*N + n = 3*3 + 2 = 11
=>
matrixa.Data[1, 11] = 1;

Index was outside the bounds of the array

Friends
I have got an error which tells "Index was outside the bounds of the array" I dont know y it was happening since after completeting the for loop and entering the loop fresh again it was showing the variable value when exited from the loop before.
int[,] arrScr = new int[lstTest.Count, cnt2 + 3];
string[,] arrName = new string[lstTest.Count, cnt2 + 3];
int p;
for (i = 0; i < lstTest.Count; i++)
{
using (DataTableReader dtr3 = ds.Tables["scord_mark_table" + (i + 1).ToString()].CreateDataReader())
{
p = 0;
while (dtr3.Read())
{
arrName[i, 2 + p] = dtr3[15].ToString();
for (int k = 2; k < 12; k++)
{
arrScr[i, 2 + p] += Convert.ToInt32(dtr3[k].ToString());
}
p++;
}
}
What is in dtr3[12]? does it return null?
What is the value of cnt2?
Change k <= 12 to k < 12.
If this is not the cause of your problem you should rewrite k <= 12 to k < 13 as that is the convention most coders are used to read and write.
This means that your array does not contain as many elements as you are trying to access.
It's going to exit from the loop as k = 13 because the last valid value of k is 12, then it went through the loop, executed k++ (making it 13). At which point it fails the condition because 13 > 12 and that's when it actually exits.

Categories

Resources