How to get richtextbox lines? - c#

How to get richtextbox lines without displaying the content on it, I have a code like this under the button2
for (int i = 0; i < split.Length; i++)
richTextBox2.Text* += split[i] + "\n";
MessageBox.Show("Tamam!");`
but it takes too much time to display the content on Richtext, If I did not use this code above, there have another code under the button3 which is related with the code above
foreach (string line in *richTextBox2.Lines*)
how should I do to solve this problem?

Not knowing what you exactly want to achieve but if updating of the control takes a lot of time you might try before to start the loop in buton2 to stop rendering the control. Don't forget to enable it again.
Like so:
richtextBox2.SuspendLayout();
for (int i = 0; i < split.Length; i++)
{
richTextBox2.Text* += split[i] + "\n";
}
MessageBox.Show("Tamam!");
richtexbox2.ResumeLayout();
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx
Or buildup your string with a stringbuilder first and the assign:
var builder = new StringBuilder();
for (int i = 0; i < split.Length; i++)
{
builder.Append(split[i]);
builder.Append('\n');
}
richTextBox2.Text = builder.ToString();
http://msdn.microsoft.com/en-us/library/y9sxk6fy.aspx

Related

C# Invalid expression term '+='

for (int i = 0; i <= dataGridView1.Rows.Count; i++)
{
string point_value += dataGridView1.Rows[i].Cells[1].Value + "|";
}
Does anyone know why I am getting this error
Invalid expression term '+='
for this code?
you create new variable with in your for loop that the reason
if you need to store data each loop you should create variable outside loop
like this
string point_value = "";
for(int i=0; i < dataGridView1.Rows.Count; i++)
{
point_value += dataGridView1.Rows[i].Cells[1].Value + "|";
}
for more information about loop pattern here
(edited to avoid empty value and out of range from DiplomacyNotWar's commented)

Need help for insert data from multiple check-box-list into database

for (int i = 0; i < CheckBoxList3.Items.Count + 1; i++)
{
if (CheckBoxList3.Items[i].Selected || CheckBoxList4.Items[i].Selected || CheckBoxList5.Items[i].Selected)
{
str1 += CheckBoxList3.Items[i].Text.ToString() + "," + CheckBoxList4.Items[i].Text.ToString() +"," + CheckBoxList5.Items[i].Text.ToString() + ",";
I receive the error call "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index". How to solve this issue?
What I want to do was collect the data from checkedbox in multiple check-box-list and combine together and store it into database.
Any suggestion or help would be great. Thanks
UPDATE : This is one of my check box list. The error point to my second line of the for loop.
<asp:CheckBoxList ID="CheckBoxList3" runat="server" RepeatDirection="Horizontal" Width="900px">
<asp:ListItem>Chicken</asp:ListItem>
<asp:ListItem>Tomato</asp:ListItem>
<asp:ListItem>Garlic</asp:ListItem>
</asp:CheckBoxList>
Wrong line 1: for (int i = 0; i < CheckBoxList3.Items.Count + 1; i++
Wrong line 2: CheckBoxList3.Items[i].Selected || CheckBoxList4.Items[i].Selected || CheckBoxList5.Items[i].Selected
Wrong line 3: str1 += CheckBoxList3.Items[i].Text.ToString() + "," + CheckBoxList4.Items[i].Text.ToString() +"," + CheckBoxList5.Items[i].Text.ToString() + ",";
You need to change your for loop condition from CheckBoxList3.Items.Count + 1 to CheckBoxList3.Items.Count
Also your condition checking only CheckBoxList3.Items.Count but you are using CheckBoxList4.Items.Count and CheckBoxList5.Items.Count
It seems CheckBoxList4 and CheckBoxList5 don't have enough item like CheckBoxList3.
Please Check your all individual CheckboxList objects to use same indexer.
Hope this helps.
//EDIT
Best practice:
string result = CombineCheckboxLists(checkBoxList1, checkBoxList2, checkboxlist3);
private string CombineCheckboxLists(params CheckBoxList[] list)
{
StringBuilder builder = new StringBuilder();
string result = string.Empty;
if (list?.Length > 0)
{
int minItemsCount = list.Min(l => l.Items.Count);
if (minItemsCount > 0)
{
for (int i = 0; i < minItemsCount; i++)
{
builder.Append(string.Join(",", list
.Where(l => l.Items[i].Selected)
.Select(l=> l.Items[i].Text)));
//if you want to merge all iteration via ",", please use following lines instead of above -- [marked as **]
// ** //builder.Append($"{string.Join(",", list.Select(l => l.Items[i].Value))},");
}
}
}
result = builder.ToString();
// ** // result = result.TrimEnd(',');
return result;
}
Just change your for loop like below:
for (int i = 0; i < CheckBoxList3.Items.Count; i++)
if (CheckBoxList3.Items[i].Selected)
str1 += CheckBoxList3.Items[i].Text.ToString() + ",";
for (int i = 0; i < CheckBoxList4.Items.Count; i++)
if (CheckBoxList4.Items[i].Selected)
str1 += CheckBoxList4.Items[i].Text.ToString() + ",";
for (int i = 0; i < CheckBoxList5.Items.Count; i++)
if (CheckBoxList5.Items[i].Selected)
str1 += CheckBoxList5.Items[i].Text.ToString() + ",";
UPDATE
#Caner LENGER has a good idea. But as I mentioned in the comment of his answer it's partially true, because list.Min returns minimum value of a list that in your case it's not gonna work. Why?
Imagine you have 3 CheckBoxLists that each one has 3 items in it except one of them that has 4. Using list.Min you'll get 3, No matter what's inside the 4th value of CheckBoxList.
So, I changed his code and here is the tested result:
public static string CombineCheckboxLists(params CheckBoxList[] list)
{
StringBuilder sb = new StringBuilder();
foreach (CheckBoxList checkBoxList in list)
{
int counter = checkBoxList.Items.Count;
for (int i = 0; i < counter; i++)
sb.Append(checkBoxList.Items[i].Selected ? checkBoxList.Items[i].Text + "," : "");
}
return sb.ToString();
}

Outputting a _ for each letter of a string in an array (hangman)

public void Dashes()
{
for (int i = 0; i < SelectedWord.Length; i++)
console.WriteLine("_");
}
this is the only thing i can think of and whenever I go to run it puts it on all separate lines when I want it on 1 line
public void Dashes()
{
for (int i = 0; i < SelectedWord.Length; i++)
console.Write("_");
}
Thanks to #Grant Winney
Try this
string output = "";
for (int i = 0; i < SelectedWord.Length; i++)
{
output += "_";
}
console.WriteLine(output);
The console,writeline prints starts a new line everytime its called in your loop.
public void Dashes()
{
string dashes ="";
for (int i = 0; i < SelectedWord.Length; i++)
dashes +="_";
console.WriteLine(dashes);
}
You can also ditch the loop entirely if running .NET 4 or above and use the following single line (as per this answer):
console.WriteLine(String.Concat(Enumerable.Repeat("_", SelectedWord.Length)));

For loop in forms

I want my form to display 1,2,3,4,5
but all it does is replace the text again and again.
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text = Convert.ToString(i);
}
I know it's because of the .Text that it always overrides itself. But how can i leave them in the form so it will display:
1
2
3
4
5
The problem is that in your loop, you're completely replacing the text with each iteration. So the text is left with whatever the last value of i was.
Try putting adding to the current text (with +=) and putting a new line (Environment.NewLine or "\n") between each number:
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text += Environment.NewLine + Convert.ToString(i);
}
Or alternatively, a little Linq can make your life a lot easier:
richTextBox1.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 5));
Try this:
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text += Convert.ToString(i) + Environment.NewLine;
}
Edit:
Just noticed that you want to print one number per line. \n is the NewLine character and will give you a carriage return on the end of the line.
Environment.NewLine is also a good choice, because it will give you the newline character based on the environment the app is running in.
append text, don't just assign it.
richTextBox1.Text += Convert.ToString(i);
this is equivelant of
richTextBox1.Text = richTextBox1.Text + Convert.ToString(i);
richTextBox1.AppendText(i.ToString()+Environment.NewLine);
This will also do:
for (int i = 1; i <= 5; i++)
{
richTextBox1.AppendText(i + Environment.NewLine);
}

how to display values stored in multi dimenssional array?

I have created a multidimensional array.I wants to display it in the .aspx page. please help.my code is here.It doesnt shows the array values
string[,] array_questions = new string[dt.Rows.Count, dt.Columns.Count];
for (i = 0; i < dt.Rows.Count; i++)
{
for (j = 0; j < dt.Columns.Count; j++)
{
array_questions[i, j] = dt.Rows[i][j].ToString();
//TextBox1.Text = array_questions[i,j];
}
}
Console.Write(array_questions[i, j] + " ");
you can also use Response.write
Console.Write is used for console applications.
If you want to display the data on the aspx webpage you need to create a webcontrol (for example label) and set its text property with an appropriate value.
If you want to write the values ​​in the table in the console visual studio, call the method
Console.Out.Write() and not Console.Write().
Otherwise call Response.Write() to write on the page
// display the array
for (int i = 0; i < array_questions.GetLength(0); i++)
{
for (int j = 0; j < array_questions.GetLength(1); j++)
{
Response.Write("i="+i+" "+"j="+j);
Response.Write("<br>");
}
}

Categories