I just started with ASP.NET and I'm having trouble with displaying a result from a loop. For example:
int x = 0;
while (x < 10) {
Label1.Text = x+""; // This will show only result 9 ( last result ).
x++;
}
How do I show all results instead of only one?
Instead of:
Label1.Text = x+"";
Do:
Label1.Text = Label1.Text + x;
This will show only result 9 ( last result ).
Yes because you assign a new value to Label1.Text property in every iteration.
Try this instead;
int x = 0;
while (x < 10)
{
Label1.Text = Label1.Text + x;
x++;
}
Or instead define a string value outside of while and add it this int values inside of your loop and assign your .Text value outside of your loop like;
int x = 0;
string s = "";
while (x < 10)
{
s += x;
x++;
}
Label1.Text = s;
Or use StringBuilder would be better if you use a lot of numbers;
int x = 0;
StringBuilder s = new StringBuilder();
while (x < 10)
{
s.Append(x);
x++;
}
Label1.Text = s.ToString();
int x = 0;
while (x < 10) {
Label1.Text += x+""; // This will show "123456789".
x++;
}
You need to add to the text in each iteration.
If you want to show a list of them :
Label1.Text += "," + x.ToString();
Or
Label1.Text = Label1.Text + "," + x.ToString();
Either way will produce the result :
0,1,2,3,4,5,6,7,8,9
You should accumulate the values of each element, something like this:
int x = 0;
while (x < 10) {
Label1.Text = Label1.Text + x;
x++;
}
int x = 0;
while (x < 10) {
Label1.Text += x.ToString();
x++;
}
You could use string builder
try this:
StringBuilder sb = new StringBuilder();
int x = 0;
while (x < 10) {
sb.Append(x);
sb.Append(" ");
x++;
}
Label1.Text = sb.ToString();
Please use the code as below, you gotta assign a new id to Label1.Text in every iteration.
int x = 0;
while (x < 10)
{
label1.Text += x.ToString();
x++;
}
Replace
Label1.Text = x+"";
with
Label1.Text = Label1.Text + x.ToString();
+= append string to variable instead of replacing so,
int x = 0;
while (x < 10) {
Label1.Text += x+" "; //append space to separate
x++;
}
Related
int i = 0;
string[] sum = new string[256];
for (i = 1; i < 256; i++)
{
sum[i] = sum[i].ToString();
sum[i] = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")", string.Empty));
if(sum[i] == 0)
{
dt.Columns.Remove("BIN"+i.ToString()+"");
}
}
There is a problem where you're trying to assign an int to sum[i], because sum is a string[], and you can't assign an int to one of the items.
Instead, it seems you want to get the return value from the method and save it in a variable so you can compare it. In order to do this, we can modify the code like:
string[] sum = new string[256];
for (int i = 1; i < 256; i++)
{
int result = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")", string.Empty));
if (result <= 0)
{
dt.Columns.Remove("BIN" + i.ToString() + "");
}
}
This can be simplified further by using the return value of the method directly in the if condition. We can also use interpolated strings instead of concatenation:
string[] sum = new string[256];
for (i = 1; i < sum.Length; i++)
{
if(Convert.ToInt32(dt.Compute($"Sum(BIN{i})", string.Empty)) <= 0)
{
dt.Columns.Remove($"BIN{i}");
}
}
I want my label2.Text to display each concecutive result of the multiplication table, but only the last result gets displayed.
I made that each checkbox equals only one math table. checkbox1 = multiplication table 1, checkbox2 = multiplication table 2 and so on...
Why is only the last result being displayed in my label2.Text property in my Windows Form?
P.S. I am working through an introduction course of C#.
int multiplyWith;
int multiplyNumber;
for (multiplyNumber = 1; multiplyNumber <= 12; multiplyNumber++)
{
if (checkBox1.Checked == true)
{
multiplyWith = 1;
int sum = multiplyNumber * multiplyWith;
label2.Visible = true;
label2.Text = sum + "\n";
}
else if (checkBox2.Checked == true)
{
multiplyWith = 2;
int sum = multiplyNumber * multiplyWith;
label2.Visible = true;
label2.Text = sum + "\n";
}
}
You are not concatenating the result but only setting the current value.
This would work but is not the most clean/efficient way to do it:
label2.Text += sum + "\n";
Try using a StringBuilder to generate the result first and at the end assign the text box the StringBuilder value.
StringBuilder sum = new StringBuilder();
int multiplyWith = checkBox1.Checked ? 1 : 2;
for (int multiplyNumber = 1; multiplyNumber <= 12; multiplyNumber++)
{
sum.AppendLine(multiplyNumber * multiplyWith);
}
label2.Visible = true;
label2.Text = sum.ToString();
If you want you could change it to something like:
int multiplyWith;
int multiplyNumber;
var results = string.Empty;
for (multiplyNumber = 1; multiplyNumber <= 12; multiplyNumber++)
{
if (checkBox1.Checked == true)
{
multiplyWith = 1;
int sum = multiplyNumber * multiplyWith;
label2.Visible = true;
results += sum + "\n"
}
else if (checkBox2.Checked == true)
{
multiplyWith = 2;
int sum = multiplyNumber * multiplyWith;
label2.Visible = true;
results += sum + "\n"
}
}
Then after your loop exits:
label2.Text = results;
you could just edit this line from
label2.Text = sum + "\n";
to
label2.Text += sum + "\n";
I want to store all listbox control values in a string with "," separated so that I can show it in the label. I'm using for loop but giving error
for (int i = 0; i < ListBox2.Items.Count; i++){
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0){
string projectnames += ListBox2.Items[i].ToString();
}
}
string projectnames = "";
bool firstValue = true;
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0)
{
if(!firstValue)
{
projectnames += ", " + ListBox2.Items[i].ToString();
}
else
{
projectnames += ListBox2.Items[i].ToString();
firstValue = false;
}
}
}
Instead of the loop i'd use LINQ + String.Join:
var selected = ListBox2.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.ToString());
string projectnames = String.Join(",", selected);
On that way it's much better to read and you don't need to care about trailing commas.
This will generate a string of all selected items in the list.
string projectnames = "";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
projectnames += ListBox2.Items[i].ToString() + ", ";
}
}
The most succinct method I can think of is:
var label = string.Join(",", listBox2.Items.Cast<string>());
(This uses System.Linq)
Try this, will help you!
protected void btnSave_Click(object sender, EventArgs e)
{
string selectedItem=string.Empty;
if (ListBox1.Items.Count > 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
selectedItem += ListBox1.Items[i].Text.ToString() + ", ";
//insert command
}
}
}
}
i have made textbox 1 and 2 to create a row of x's starting with one x, then enter and then xx and so on..
now i need textbox 3 and 4 to display the same but it has to do it beginning from 10 x's.
this is what i have:
namespace Vierkant
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click_1(object sender, RoutedEventArgs e)
{
string x = "X";
for (int i = 0; i < 10; i++)
{
if ( i == 0)
{
x = "X";
}
else
{
x += "X";
}
txt_box1.Text += (x) + "\n";
txt_box2.Text += (x) + "\n";
}
for (int j = 10; j > 0; j--)
{
if (j == 10)
{
x = x.Remove(x.Length - 1);
}
else
{
x = x.Remove(x.Length - 1);
}
txt_box3.Text += (x) + "\n";
txt_box4.Text += (x) + "\n";
// txt_box4.Text displays correct but starts from 9 x's?
}
}
}
}
You are removing one of the x's before displaying it.
if (j == 10)
{
x = x.Remove(x.Length - 1);
}
Perhaps a better solution would be:
if (j < 10)
{
x = x.Remove(x.Length - 1);
}
Try with this:
private void button_Click_1(object sender, RoutedEventArgs e)
{
int i;
string allLines = "";
for (i = 1; i <= 10; i++)
allLines += new string('X', i) + (i < 10 ? "\n" : "");
txt_box1.Text = allLines;
txt_box2.Text = allLines;
allLines = "";
while (--i > 0)
allLines += new string('X', i) + (i > 1 ? "\n" : "");
txt_box3.Text = allLines;
txt_box4.Text = allLines;
}
This way is much clearer and more efficient, you are creating MUCH LESS strings in memory.
This is a simple code I have written to solve a mathematical problem.
namespace StoreCredit
{
class Program
{
private static int No1;
static void Main(string[] args)
{
string[] text = System.IO.File.ReadAllLines(#"input.txt");
int nocases = int.Parse(text[0]);
int J = 1;
int No1=0;
int No2=0;
for (int x = 1; x <= nocases;x++ )
{
int Amount = int.Parse(text[J]);
int NoItem = int.Parse(text[J+1]);
char[] delimiterChars = { ' ' };
string[] Values = text[J+2].Split(delimiterChars);
int z = 0;
bool found = false;
while ((z < NoItem) && !found)
{
int Item = int.Parse(Values[z]);
int Remaining = Amount - Item;
int y = 0; bool found2 = false;
while ((y < NoItem) && !found2)
{
if (Remaining == int.Parse(Values[y])&&!(y==z))
{
Console.WriteLine("Found a match");
found = true;
found2 = true;
Console.WriteLine("Value 1 = {0} and Value 2={1}",(z+1),(y+1));
}
y++;
}
z++;
}
string lines = "Case #" + x + ": ", z, y;
StreamWriter file2 = new StreamWriter(#"output.txt", true);
file2.WriteLine(lines);
file2.Close();
J = J + 3;
}
}
}
}
I wanted to write the out put of this program to a text file. But I get an error saying z,y cannot be declared in this scope because it would give an different meanings to z,y. Can you please explain me the reason for this.
Thank you
This line near the end attempts to re-declare z and y, since you've separated them by commas:
string lines = "Case #" + x + ": ", z, y;
Did you intend to concatenate them instead? Such as:
string lines = "Case #" + x + ": " + z + y; // notice the + instead of comma