private void btn_Click(object sender, EventArgs e)
{
int quantity = Convert.ToInt32(txtseq.Text);
int[] array = new int[lbox.Items.Count];
if (txtseq.Text.Length > 0)
{
for (int i = 2; i < array.Length; i++)
{
array[0] = 0;
array[1] = 1;
array[i] = array[i--] + array[i];
lbox.Items.Add(array[0].ToString() + array[1].ToString() + array[i].ToString());
}
}
else
MessageBox.Show("Insert something first");
}
Trying to generate a fibonacci sequence and sending it to a listbox item however i dont understand why its not being added to the listbox as a item in the for
Related
How to achive searching for multiple values in a textbox.
Ex: Using~s
This code can achieve only a single search in the text box.
private void button1_Click(object sender, EventArgs e)
{
int index = 0;
string temp = richTextBox1.Text;
richTextBox1.Text = "";
richTextBox1.Text = temp;
while (index < richTextBox1.Text.LastIndexOf(textBox1.Text))
{
richTextBox1.Find(textBox1.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None);
richTextBox1.SelectionBackColor = Color.Yellow;
index = richTextBox1.Text.IndexOf(textBox1.Text, index) + 1;
}
this.timer1.Start();
}
This can be achieved by using the split keyword.
private void button1_Click(object sender, EventArgs e)
{
int index = 0;
string temp = richTextBox1.Text;
richTextBox1.Text="";
richTextBox1.Text = temp;
this.timer1.Start();
string[] names = textBox1.Text.Split('~');
int count = 0;
while (index < richTextBox1.Text.LastIndexOf(names[0]))
{
richTextBox1.Find(names[0], index, richTextBox1.TextLength, RichTextBoxFinds.None);
richTextBox1.SelectionBackColor = Color.Yellow;
index = richTextBox1.Text.IndexOf(names[0], index) + 1;
count++;
}
int count2 = 0;
while (index < richTextBox1.Text.LastIndexOf(names[1]))
{
richTextBox1.Find(names[1], index, richTextBox1.TextLength, RichTextBoxFinds.None);
richTextBox1.SelectionBackColor = Color.LightBlue;
index = richTextBox1.Text.IndexOf(names[1], index) + 1;
count2++;
}
}
Currently the user is able to input numbers into the ListBox, and I want the ListBox to be sorted by the bubble sort below when a sort checkbutton is clicked. However, it only outputs the index number e.g. 0,1,2,3... I am not allowed to use any array or containers just the items property and parsing and converting.
private void sorted()
{
int a = Convert.ToInt32(lstHoldValue.Items.Count);
int temp;
for (int i = 0; i < a; i++)
{
for (int j = i + 1; j < a; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[i]) >
Convert.ToInt32(lstHoldValue.Items[j]))
{
temp = Convert.ToInt32(lstHoldValue.Items[i]);
(lstHoldValue.Items[i]) = Convert.ToInt32(lstHoldValue.Items[j]);
(lstHoldValue.Items[j]) = temp;
}
}
}
lstHoldValue.Items.Clear();
for (int i = 0; i < a; i++)
{
Convert.ToInt32(lstHoldValue.Items.Add("\t" + i));
}
}
This is more like a Bubble sort algorithm:
private void BubbleSort()
{
int a = lstHoldValue.Items.Count;
for (int i = 0; i < a - 1; i++)
{
var k = 0;
for(var j = 1; j < a - i; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[j]) < Convert.ToInt32(lstHoldValue.Items[k]))
{
var temp = lstHoldValue.Items[j];
lstHoldValue.Items[j] = lstHoldValue.Items[k];
lstHoldValue.Items[k] = temp;
k = j;
}
else
{
k++;
}
}
}
}
It will sort the numbers in the Items collection of your lstHoldValue listBox control
If the item in the ListBox is not an integer, then it is sorted as zero (0).
This sets bubbleUp to true indicating that a swap has been made in the list box. This variable is used to indicate that a swap has/has not been made in the last comparison. Entering the while(bubbleUp) sets bubbleUp to false to indicate that no swaps have been made. Then a loop through each item in the list box to compare adjacent items and swap if needed. If a swap is made, bubbleUp gets set to true indicating that the sort is not finished. bubbleUp only needs to be set once in the for loop to indicate another iteration is necessary. CheckItem is the conversion from string to integer. Hope this helps.
private void sorted()
{
bool bubbleUp = true;
string temp = "";
while (bubbleUp)
{
// bubble up adjacent values
bubbleUp = false;
for (int i = 0; i < _ListBox.Items.Count - 1; i++)
{
if (CheckItem(_ListBox.Items[i].ToString()) > CheckItem(_ListBox.Items[i + 1].ToString()))
{
temp = _ListBox.Items[i].ToString();
_ListBox.Items[i] = _ListBox.Items[i + 1];
_ListBox.Items[i + 1] = temp;
bubbleUp = true;
}
}
}
}
private int CheckItem(string inItem)
{
int value;
if (int.TryParse(inItem, out value))
return value;
return 0;
}
private void button1_Click(object sender, EventArgs e)
{
sorted();
}
This is my code but I'm not sure what to put in a certain area (see below) as I keep getting an error there. I am basically loading up a text file and then deleting any values that are repeated and then outputting the updated copy of the text file.
The text file looks like,
5
5
3
2
2
3
My code is
{
public partial class Form1 : Form
{
//Global Variable
int[] Original;
public Form1()
{
InitializeComponent();
}
//Exit Application
private void mnuExit_Click_1(object sender, EventArgs e)
{
this.Close();
}
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//Code to Load the Numbers From a File
OpenFileDialog fd = new OpenFileDialog();
//Open the File Dialog and Check If A File Was Selected
if (fd.ShowDialog() == DialogResult.OK)
{
//Open File to Read
StreamReader sr = new StreamReader(fd.OpenFile());
int Records = int.Parse(sr.ReadLine());
//Assign Array Sizes
Original = new int[Records];
//Go Through Text File
for (int i = 0; i < Records; i++)
{
Original[i] = int.Parse(sr.ReadLine());
}
}
}
private void btnOutput_Click(object sender, EventArgs e)
{
//Store Original Array
string Output = "Original \n";
//Output Original Array
for (int i = 0; i < Original.Length; i++)
{
Output = Output + Original[i] + "\n";
}
//Create TempArray
int[] TempArray = new int[Original.Length];
//Set TempArray Equal to Original Array
for (int i = 0; i < Original.Length; i++)
{
TempArray[i] = Original[i];
}
//Current Index
int Counter = 0;
//Loop Through Entire Array
for (int i = 0; i < TempArray.Length; i++)
{
for (int j = i + 1; j < TempArray.Length; j++)
{
//Replace Duplicate Values With '-1'
if (TempArray[i] == TempArray[j])
{
TempArray[j] = -1;
Counter++;
}
}
}
//Set Size of Original Array
Original = new int[Original.Length - Counter];
//Counter = 0;
//Remove -1 Values
//error begins here
for (int i = 0; i < Original.Length; i++)
{
for (int j = i + 1; j < Original.Length; j++)
{
//Set Original Array Equal to TempArray For Values Not Equal To '-1'
if (j != -1)
{
Original[j] = TempArray[j];
//Counter++;
}
}
}
//error ends here
//Final Output -- The New Array
Output = Output + "Original Without Duplicates\n";
for (int i = 0; i < Original.Length; i++)
{
Output = Output + Original[i] + "\n";
}
lblOutput.Text = Output;
}
}
}
I understand your logic, but wer'e all lazy programmers. You could simply use LINQ in order to prevent duplication. Load the array as you did already and use the Distinct method somthing like this:
int[] newArray = Orginal.Distinct().ToArray();
Goodluck.
I would like to output only a part of items from listBox1 like:response.response.items[counter].first_name to lable_name.Text. How can I do that?
Part of method:
for (int counter = 0; counter < response.response.items.Count; counter++)
{
listBox1.Items.Add(response.response.items[counter].first_name + " " + response.response.items[counter].last_name + Environment.NewLine);
}
The way I output items to lable:
private void listBox1_DoubleClick(object sender, EventArgs e)
{
int count = listBox1.Items.Count -1;
for (int counter = count; counter >= 0; counter--)
{
if (listBox1.GetSelected(counter))
{
lable_name.Text= listBox1.Items[counter].ToString();
}
}
}
lable_name.Text = listbox1.Items[counter].ToString().Split(' ')[0];
could be one of the options ...
just make sure what charackter are qou spliting on as well as the text in string.
This is an easy question but I'm still learning this language.
How we can write program that has parameters so that if the the number is 5, it will write
*
**
***
****
*****
I can do this:
*
*
*
*
Using this:
private void button1_Click(object sender, EventArgs e)
{
string message = " ";
for (int count = 0; count < numericUpDown1.Value; count++)
{
for (int m = 0; m < numericUpDown1.Value; count)
{
message += "*" + "\r\n";
}
}
}
I think I need the second for-loop, but I'm not sure what to do next.
if that's not a conceptual homework it would be much easier to solve this way:
for(int i=1; i<=n; i++)
Console.WriteLine(new string('*',i));
You need two loops (see note).
First (a) counts from 1 to 5.
Second (b) counts from 1 to a and adds a "*" each time.
private void button1_Click(object sender, EventArgs e)
{
string message = " ";
for (int count = 0; count < numericUpDown1.Value; count++)
{
for (int m = 0; m < count; m++)
{
message += "*";
}
message += "\r\n"
}
}
Note You can do it with one for loop. But personally I think the two loop version is clearer.
private void button1_Click(object sender, EventArgs e)
{
string line = "";
string message = " ";
for (int count = 0; count < numericUpDown1.Value; count++)
{
line += "*";
message += "\r\n" + line;
}
}
Try this :
private void button1_Click(object sender, EventArgs e)
{
string message = "";
for (int count = 0; count < numericUpDown1.Value; count++)
{
for (int m = 0; m <=count ; m++)
{
message += "*" ;
}
message += "\r\n";
}
}
Yo do not need two for-loops, try this instead
private void button1_Click(object sender, EventArgs e)
{
string message = "";
for (int count = 1; count < numericUpDown1.Value + 1; count++)
{
message += "".PadLeft(count,'*') + Environment.NewLine;
}
}
Try this, just another way to do it for learning:
private static void PrintStars(int num)
{
for (int i = 1; i <= num; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}