Reversed for loop - c#

I made this small nested for loop, and it shows no error in C# whatsoever,
but when I try to run my small program I get the following error in my TextBox:
System.Windows.Forms.TextBox, Text: System.Windows.Forms.TextBox,
Text: Syst...
Here is my code:
int number = textBox.Text..ToString();
for (int row = 0; row < number; row++)
{
for (int x = number - row; x > 0; x--)
{
textBox2.Text = textBox2.Text + "X";
}
textBox2.Text = textBox2 + Environment.NewLine;
}
My result should be something like this:
XXXX
XXX
XX
X
I can't figure out what may cause this error.

You can't assign a string to a number. You need to convert it:
// int number = textBox.Text..ToString();
int number;
if (!int.TryParse(textBox.Text, out number)
{
// Handle improper input...
}
// Use number now
In addition, when you add the newline, you need to actually append to the Text property, not the TextBox itself:
textBox2.Text = textBox2.Text + Environment.NewLine;

Instead of
textBox2.Text = textBox2 +
use
textBox2.Text = textBox2.Text +
in the last line.
That's it ;-)

textBox2.Text = textBox2 + Environment.NewLine;
Should be
textBox2.Text = textBox2.Text + Environment.NewLine;
System.Windows.Forms.TextBox is just class name

You're missing a .Text in the second to last line. It should be:
textBox2.Text = textBox2.Text + Environment.NewLine;
^^^^^
or just:
textBox2.Text += Environment.NewLine;

int number = textBox.Text..ToString();
Suppose that was a typo? Either way, check if the value is numeric first.
if (int.TryParse(textBox.Text, out number))
{
//run your loop here
}
Also,
textBox2.Text = textBox2 + Environment.NewLine;
should be:
textBox2.Text = textBox2.Text + Environment.NewLine;

You cannot assign string to an int , what you are doing as:
int number = textBox.Text..ToString();
Better option is to use int.TryParse(textBox.Text, out number)
AND
Change
textBox2.Text = textBox2 + Environment.NewLine;
to
textBox2.Text = textBox2.text + Environment.NewLine;
Edit:
Even if you change 2 dots to 1, it will give error for int number = textBox.Text.ToString(); - you can't assign string to int

You have two dots here.
textBox.Text..ToString();
This should throw a compilation error by the way. And you can't assign it to a variable of type integer.
textBox2.Text = textBox2 + Environment.NewLine;
You have to call a method of the textbox here, presumably textBox2.Text.

This might inspire you to think about this problem differently:
// I created a simple textbox class so I could do this in a console app
var textBox = new TextBox();
var textBox2 = new TextBox();
textBox.Text = "4";
var number = Convert.ToInt32(textBox.Text);
var descendingXStrings = Enumerable.Range(1, number)
.Select(n => new string('X', n))
.Reverse();
textBox2.Text = string.Join(Environment.NewLine, descendingXStrings);
Console.WriteLine(textBox2.Text);
CW as this does not answer the question directly.

Try this
int number = int.Parse(textBox1.Text);
for (int row = 0; row < number; row++)
{
for (int x = number - row; x > 0; x--)
{
textBox2.Text = textBox2.Text + "X";
}
textBox2.Text = textBox2.Text + Environment.NewLine;
}
Convert TextBox Value in integer
Change textBox2 to textBox2.Text in second last line
Use textBox as multiline textbox

Related

How to make listview begin with certain word in all raws?

I'm a beginner with c#, so if anyone can please help.
I download things like this all the time
"http://ourquraan.com/quran/3bdalrahman_al3osy/001.mp3",
so I thought why not make an app to make it easy for me.
The idea is I want to make an editor program to give the program the first part of the link, then I insert the range of numbers from 1 to 114 to be put after it.
Now I managed to get the ranges correct but what I can't do is to make the first part of the link a constant in every raw of the listview.
In a brief way I want the result to be:
"link/ " + "1" + ".mp3"
"link/" + "2" + ".mp3"
.
.
.
.
"link/ " + "114" + ".mp3"
what i get is
"link/" + "1" + ".mp3"
2
3
4
.
.
.
.
114
The current code is :
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text=="" |textBox2.Text=="")
{
MessageBox.Show("please specifi a multiple and upper limtit.", "Error");
}
else
{
int number = 0 , limit = 0 ;
string currentnumber = "";
number = Convert.ToInt32(textBox2.Text);
limit = Convert.ToInt32(textBox1.Text);
do
{
number++;
if (number == limit)
{
}
else
{
currentnumber += number + "\n";
}
} while (number < limit);
foreach (int item in currentnumber)
richTextBox1.Text = textBox3.Text + textBox2.Text + "\n" + currentnumber + textBox1.Text + "\n";
}
}
textBox1 has the first range
textBox2 has the second range
textBox3 has the link
Please help.
Thanks.
This will help you to generate the required list as List you can bind the ListView with this List:
string linkFormat=#"\Link\{0:000}.mp3";
List<string> LinkList = Enumerable.Range(1, 114)
.Select(x => String.Format(linkFormat, x))
.ToList();
To get the List Displayed in the richTextBox1 as its Text you can use the Following code:
richTextBox1.Text = String.Join("\n", LinkList);

Weird spacing in Hex to String

I am trying to make a hex to string converter and for some reason the spacing between bytes in the conversion is multiplied by 2.
I would like it to spit out a single space between characters,
private void button2_Click(object sender, EventArgs e)
{
try
{
textBox1.Clear();
textBox2.Text = textBox2.Text.Replace(" ", "");
string StrValue = "";
while (textBox2.Text.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(textBox2.Text.Substring(0, 2), 16)).ToString();
textBox2.Text = textBox2.Text.Substring(2, textBox2.Text.Length - 2);
textBox1.Text = textBox1.Text + StrValue + " ";
}
}
catch (Exception ex)
{
MessageBox.Show("Conversion Error Occurred : " + ex.Message, "Conversion Error");
}
}
so "41 41" converted would look like "A A", but this is what happens:
image
Does anybody see what I am doing wrong?
In this line
textBox1.Text = textBox1.Text + StrValue + " ";
you consequently append the result of calculations to your TextBox1.
So, after the first iteration the result is A, you append it and a whitespace to TextBox1.
Then, you take the second 41 and convert it. Now, StrValue is AA and you append it and space to TextBox1, and so on.
You need to move this line out of your while loop:
textBox1.Clear();
textBox2.Text = textBox2.Text.Replace(" ", "");
string StrValue = "";
while (textBox2.Text.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(textBox2.Text.Substring(0, 2), 16)).ToString();
textBox2.Text = textBox2.Text.Substring(2, textBox2.Text.Length - 2);
}
textBox1.Text = StrValue;
As some people mentioned in comments, you need to stop working with TextBoxes this way. It is pretty confusing. You may want to do the following:
private string HexToString(string hex)
{
string result = "";
while (hex.Length > 0)
{
result += Convert.ToChar(Convert.ToUInt32(hex.Substring(0, 2), 16));
hex = hex.Substring(2); // no need to specify the end
}
return result;
}
Then, in your button click event or wherever else:
textBox1.Text = HexToString(textBox2.Text.Replace(" ", ""));
As simple as that. Or you can even move replacing the whitespaces in the method. Now, this code is readable and is logically separated.
The problem seems to be caused by the accumulated value in StrValue. You should define that variable inside your while, and assign it only (don't append a new value).
while (textBox2.Text.Length > 0)
{
string StrValue = System.Convert.ToChar(System.Convert.ToUInt32(textBox2.Text.Substring(0, 2), 16)).ToString();
textBox2.Text = textBox2.Text.Substring(2, textBox2.Text.Length - 2);
textBox1.Text = textBox1.Text + StrValue + " ";
}

How to find combinations in this way in C#

I have text in textBox1 "Aplle,Orandge,Nuts,Cherries". There can be more words added in text box, even 30.
I need to find combination like this:
First click - I get textBox2.Text = "Aplle" AND textBox3.Text = "Orandge,Nuts,Cherries"
Second click textBox2.Text = "Aplle,Orandge" AND textBox3.Text = "Nuts,Cherries"
Third textBox2.Text = "Aplle,Orandge,Nuts" AND textBox3.Text = "Cherries"
First I understand that I need counter which count commas, if CommaCounter=1, than split after first comma, if CommaCounter=2, split after second comma, if CommaCounter=100, than split after comma 100.
Than I need splitter, I guess, but and don't understand how to really make it.
Why I need find every combination click by click because every combination will be used by another click witch makes another function.
I insert a pictures how it should look.
here is code I started, of course unfinished, incorrect...
int CommaCounter = 0;
string Part1, Part2;
string Vards = textBox1.Text;
char[] delimiterChars = { ',' };
string[] words = Vards.Split(delimiterChars);
string searchTerm = ",";
var matchQuery = from word in words
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
select word;
int wordCount = matchQuery.Count();
foreach (string s in words)
{
textBox2.Text = Part1.ToString();
textBox3.Text = Part2.ToString();
}
First, declare a variable (let's say its name is counter) in the form. The initial value is 0. It stores how many clicks have you done.
// stores how many clicks you have done
int counter = 0;
In the button's event handler, split the text in the first textbox using a comma.
// split the first textbox using a comma
var words = textBox1.Text.Split(',');
Then compare the counter variable with the number of words in the first textbox. If counter is lesser than the number of words, split the words into the second textbox and the third textbox.
if (counter < words.Count())
{
// increase the counter
counter++;
// take n words, where n = counter
textBox2.Text = string.Join(",", words.Take(counter));
// skip n words, then take the rest (n = counter)
textBox3.Text = string.Join(",", words.Skip(counter).Take(words.Count() - counter));
}
You can get the value of the second textbox using Take() method. The Take() method get some element from the words. For the third textbox, you need to skip some words before you take the words. You skip the words using Skip() method.
Full source.
using System;
using System.Windows.Forms;
using System.Linq;
namespace Sample
{
public partial class MainForm : Form
{
// stores how many clicks you have done
int counter = 0;
public MainForm()
{
InitializeComponent();
}
void Button1Click(object sender, EventArgs e)
{
// split the first textbox using a comma
var words = textBox1.Text.Split(',');
if (counter < words.Count())
{
// increase the counter
counter++;
// take n words, where n = counter
textBox2.Text = string.Join(",", words.Take(counter));
// skip n words, then take the rest (n = counter)
textBox3.Text = string.Join(",", words.Skip(counter).Take(words.Count() - counter));
}
}
}
}
Keeping it simple:
void Button1Click(object sender, EventArgs e)
{
string divider = ",";
if(textBox2.Text == "") // first click.
{
textBox3.Text = textBox1.Text;
divider = "";
}
int pos = textBox3.Text.IndexOf(",");
if(pos > -1)
{
textBox2.Text = textBox2.Text + divider + textBox3.Text.Substring(0, pos);
textBox3.Text = textBox3.Text.Substring(pos + 1);
}
else if (textBox3.Text != "" ) // last one
{
textBox2.Text = textBox2.Text + divider + textBox3.Text;
textBox3.Text = "";
}
}

Project almost complete, output is wrong and will not display properly, have been unable to find my error.

I am almost finished with the program, but my output is wrong, and after 5 hours, I am still unable to figure out what I did wrong, and thought it may be helpful to seek a fresh set of eyes. I have a Demo program given by my instructor, so I took a screen shot to show clearly what it should look like.
My code for that chunk is this:
//array add student data to the array
if (currentStudentArrayIndex >= constArrayMaxSize)
{
MessageBox.Show("Array is full, cannot add any more to the array");
}
else
{
arrayNames [currentStudentArrayIndex] = studentName;
arrayGrades [currentStudentArrayIndex] = studentGrade;
MessageBox.Show("Student data has been entered into array index " + currentStudentArrayIndex);
textBoxName.Text = "";
textBoxGrade.Text = "";
}
//array output
currentStudentArrayIndex = currentStudentArrayIndex + 1;
}
//array display
private void buttonDisp_Click(object sender, EventArgs e)
{
string myString = "";
string studentName = "";
int studentGrade = 0;
myString = "Student Data Entered So Far:\n";
//loop through array
for(int i = 0; i < arrayNames.Length; i++)
{
myString = myString +
"Array[" + i + "] ==> " +
"Student #" + (i + 1);
if (arrayGrades[i] == 0)
{
myString = myString + " (NOT ENTERED)";
}
else
{
myString = myString +
" Name:" +arrayNames[i] +
" Grade:" + arrayGrades[i].ToString("c");
studentName = studentName + arrayNames[i];
studentGrade = studentGrade + arrayGrades[i];
}
//output
labelDisplay.Text = myString;
}
Obviously myString isn't working, but I'm unsure why, what did I do to it? Any suggestions, useful links, or guidance is much appreciated!
Button buttonDisp was incorrect. Using the correct button name corrected the problem, allowing the information to display properly.

Multiple character casing in same TextBox

How to change character casing in TextBox? I need that 1 line character been Upper and second line character benn Lower
isv.CharacterCasing = CharacterCasing.Upper;
isv.Text = "Upper"
isv.CharacterCasing = CharacterCasing.Lower;
isv.Text = "Lower"
As Mark said, it's difficult to understand exactly what you need, but I think it's something like
string[] lines = isv.Text.Split('\n');
string finalText = string.Empty;
for (int i = 0; i < lines.length; i++)
finalText += i%2==0 ? lines[i].ToUpper() : lines[i].ToLower() + + Environment.NewLine;
isv.Text = finalText;
Keep in mind I wrote the code without the compiler :)
You can use TextBox.Lines property I guess.
something like:
private void button1_Click(object sender, EventArgs e)
{
string result = string.Empty;
result += textBox1.Lines[0].ToUpper() + Environment.NewLine;
result += textBox1.Lines[1].ToLower();
textBox1.Text = result;
}
isv.Text = isv.Text.Split(Environment.NewLine)[0].ToUpper() + isv.Text.Split(Environment.NewLine)[1].ToLower();

Categories