RichTextBox Horizontal Scrollbar Doesn't Show When Displaying Table - c#

I have already changed Multiline properties to true and WordWrap to false but it didnt work.
The code to show the table is :
for (int i = -1; i < membershipmat.Rows.Count; i++)
{
rtf.Append(#"{\trowd");
ColumnPosition = ColWidth;
for (int j = 0; j < membershipmat.Columns.Count; j++)
{
rtf.Append(#"\clwWidth" + ColWidth.ToString() + #"\cellx" + ColumnPosition.ToString());
ColumnPosition += ColWidth;
}
rtf.Append("{");
for (int j = 0; j < membershipmat.Columns.Count; j++)
{
if (i == -1)
rtf.Append(#"{\fs24\f3\b\intbl {\ltrch Cluster " + (j + 1).ToString() + #"}\li0\ri0\sa0\sb0\fi0\ql\sl15\slmult0\cell}");
else
rtf.Append(#"{\fs22\f3\b0\intbl {\ltrch " + membershipmat.Rows[i].ItemArray[j].ToString() + #"}\li0\ri0\sa0\sb0\fi0\ql\sl15\slmult0\cell}");
}
rtf.Append("}");
rtf.Append(#"{\trowd");
rtf.Append(#"\row}}");
}
The result looks like this :

Related

export certain columns to certain columns in excel c#

I need three columns from the datagridview to export to columns a, c, and i in excel. All the data in the dataGridView needs to be visible there, but only the first three rows need to be exported.
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
objexcelapp.Cells[1, i] = dataGridView1.Columns.Count - 1; i++ ;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
objexcelapp.Cells[i + 11, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
If you only need the first 3 rows, then count to 3 instead of all rows. I don't understand how you ended up with all rows
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
objexcelapp.Cells[1, i] = dataGridView1.Columns.Count - 1; i++;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
objexcelapp.Cells[i + 11, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
Update
To write to excel check this post

Why comma(",") is read as dot(".") string?

I have been trying to separate two numbers using "." but in Excel it sets ",". How can I fix it?
int col= 2;
for (int i = 0; i < proc; i++)
{
for (int j = 0; j < mac; j++)
{
wse.Cells[1, col] = (i + 1).ToString()+"." + (j + 1).ToString();
col++;
}
}

pyramid structure alignment is not in place

Pyramid structure in c#. I used Environment.NewLine to break line but this not getting pyramid structure
int i, j, k, n;
n = Convert.ToInt32(TextBox3.Text);
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
Label4.Text += "";
}
for (k = 0; k < 2 * i - 1; k++)
{
Label4.Text += "*";
}
Label4.Text += Environment.NewLine;
}
your first inner loop ,
replace this code :
for (j = i; j < n; j++)
{
Label4.Text += "";
}
output :
*
**
***
****
with:
for (j = i; j < n; j++)
{
Label4.Text += " "; // just add one space in string
}
output :
*
* *
* * *
* * * *
if you're working with asp.net you need <br /> for line breaks and for space
int n;
string Pyramid = string.Empty;
n = Convert.ToInt32(TextBox3.Text);
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
Pyramid += " ";
}
for (int k = 0; k < 2 * i - 1; k++)
{
Pyramid += "*";
}
Pyramid += "<br />";
}
Label4.Text = Pyramid;
solution to my problem -
int i, j, k, n;
n = Convert.ToInt32(TextBox3.Text);
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
Label4.Text += " ";
}
for (k = 0; k < 2 * i - 1; k++)
{
Label4.Text += "*";
}
Label4.Text += "</br>";

Multidimensional array adding controls

How can i make the below code more efficient, with less lines.
Im adding some PictureBox elements to a two dimensinal array.
int a = 0;
int b = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " a = " + a);
Console.WriteLine("Iteration: " + i + " b = " + b);
pictureBoxArr[a, b] = new PictureBox();
b++;
}
int aa = 1;
int bb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aa = " + aa);
Console.WriteLine("Iteration: " + i + " bb = " + bb);
pictureBoxArr[aa, bb] = new PictureBox();
bb++;
}
int aaa = 2;
int bbb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aaa = " + aaa);
Console.WriteLine("Iteration: " + i + " bbb = " + bbb);
pictureBoxArr[aaa, bbb] = new PictureBox();
bbb++;
}
I was thinking something like this - but im kinda stuck.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; i++)
{
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
You almost had it.
If you want something more re-usable, you could set up a couple variables to hold your bounds.
int boundX = 10;
int boundY = 10;
for (int i = 0; i < boundX ; i++)
{
for (int j = 0; j < boundY ; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
Try:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Iteration: " + i + " a = " + i);
Console.WriteLine("Iteration: " + j + " b = " + j);
pictureBoxArr[i, j] = new PictureBox();
}
}
Dude, you are very close

How to find that one matrix is submatrix of the other in C#?

I want to find that a given matrix is a sub matrix of the other.
I have tried below piece of code but I am not sure that it would work:-
for (int i = 0; i < a.length - b.length + 1; i++) {
for (int j = 0; j < a[0].length - b[0].length + 1; j++) {
boolean submatrix = true; // at start we assume we have a submatrix
for (int k = 0; k < b.length; ++k) {
for (int l = 0; l < b[0].length; ++l) {
if (a[i + k][j + l] == b[k][l]) {
Console.WriteLine("a[" + (i + k) + "][" + (j + l) + "] = b[" + k + "][" + l + "]");
} else {
submatrix = false; // we found inequality, so it's not a submatrix
}
}
}
if (submatrix) {
Console.WriteLine("Found subatrix at " + i + "," + j + ".");
}
}
}
Please suggest??
Your suggested method is correct, there are only a few syntax and control flow issues which I've fixed.
It is important to point out that this method is only useful for detecting a submatrix of a 2D matrix, not any dimension matrix.
I assumed the datatype is a jagged array of int, though it can easily be changed.
private static bool IsSubMatrix(int[][] a, int[][] b)
{
for (int i = 0; i < a.Length - b.Length + 1; i++)
{
for (int j = 0; j < a[0].Length - b[0].Length + 1; j++)
{
bool found = true;
for (int k = 0; k < b.Length; ++k)
{
for (int l = 0; l < b[0].Length; ++l)
{
if (a[i + k][j + l] != b[k][l])
{
found = false;
break;
}
}
if (!found) break;
}
if (found) return true;
}
}
return false;
}
This is probably also not the fastest implementation.

Categories