Ascending-Descending in window application?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2682 Views - Last Post: 12 August 2009 - 10:49 PM Rate Topic: -----

#1 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Ascending-Descending in window application?

Post icon  Posted 08 August 2009 - 04:26 AM

hi i m making a window application in which i want to make a program of ascending or descending the numbers.
in console it is quite easy, but in window application it will create a problem 4 me.
what i want is when i enter some numbers in textbox like 23, 5, 7, 987, 4567,....and when i click on button so it will give me a sorted list(in ascending or decending order) in another textbox.
can anyone tell me how can i do it in C# window application.
kindly give me code, if possible.!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Ascending-Descending in window application?

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 04:37 AM

So, the user has to give the numbers separated with comma? Then you could use String.Split() to get the separated numbers:

note: the following code isn't written in any editor so it might have wrong syntax in some lines.
// str = 23, 5, 7, 987, 4567
string str = textbox1.Text;
string[] numbers = str.Split(new string[] { "," });
int[] inumbers = new int[numbers.Length];
for (int i = 0; i < numbers.Length; i++) {
	inumbers[i] = int.Parse(numbers[i]);
}



And after that piece of code, inumbers[] will have all the numbers user wrote in textbox1. Now you just apply the same sorting logic as you did in console application.

And the following code is placed behind the form's button's click event.
Was This Post Helpful? 0
  • +
  • -

#3 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 04:58 AM

[quote name='janne_panne' date='8 Aug, 2009 - 03:37 AM' post='728569']
thanks 4 ur rply.............
but it give so many errors....
Was This Post Helpful? 0
  • +
  • -

#4 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 05:04 AM

Can you copy paste your code then? That way we can help you better.
Was This Post Helpful? 0
  • +
  • -

#5 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 05:13 AM

i m making a textbox and enter some numbers like 2, 5, 8, 567, 543,...
what i want when i click on button it will show 567,543,8,5,2....including commas.
button coding is:
 string str = textBox1.Text;
		   string[] numbers = str.Split(',');
			int[] inumbers = new int[numbers.Length];
			for (int i = 0; i < numbers.Length; i++)
			{
				inumbers[i] = int.Parse(numbers[i]);
			}
			this.textBox2.Text = "inumbers";

now in this code error is removed but it will not print any value in textbox2
Was This Post Helpful? 0
  • +
  • -

#6 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 05:36 AM

Well the code you have there doens't actually sort the items yet. The easiest way to solve this is to change textbox2 to a listbox, since that allows for sorting by itself. Then the code becomes something like:

			string str = textBox1.Text;
			string[] numbers = str.Split(',');
			int[] inumbers = new int[numbers.Length];
			for (int i = 0; i < numbers.Length; i++)
			{
				inumbers[i] = int.Parse(numbers[i]);
				listBox1.Items.Add(inumbers[i]);
				
			}
			listBox1.Sorted = true;

If you want to stick to a textbox, you need to sort the inumbers array, then write them all to a single string, and that you can display on the textbox.Edit: this sorts them alphabetically, not numerical, hang on while i change it

Right this works better :D sorry for the little error, just realised it after I posted it.
			string str = textBox1.Text;
			string[] numbers = str.Split(',');		   
			List<int> lnumbers = new List<int>();
			for (int i = 0; i < numbers.Length; i++)
			{			  
				lnumbers.Add(int.Parse(numbers[i]));

			}
			lnumbers.Sort();
			foreach (int i in lnumbers)
				listBox1.Items.Add(i);

This adds the int values to a List<int> which gets sorted and then puts the sorted values in the listbox. You could still write them to a string and add them to a textbox if you like.

This post has been edited by Renagado: 08 August 2009 - 05:48 AM

Was This Post Helpful? 1
  • +
  • -

#7 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 06:24 AM

thanks 4 ur help.
it works..
well can u tell me one more thing related to this..
what i want is when it shows the sorted list, so the numbers are automatically show in a numeration table.which has one,ten,hundred,thousand,tenthosand.....
like my sorted list is 2,5,89,100,5678...
so 2 will display on one's position in table, 5 also display on one's position, 89 display on hundred position in table and so on................means the number will display on its related category in table.
Was This Post Helpful? 0
  • +
  • -

#8 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Ascending-Descending in window application?

Posted 08 August 2009 - 08:56 AM

Well, you can use a listview and add columns for example, or maybe make several lisboxes next to each other depending what style you want. then change the last part into something like:


foreach (int i in lnumbers)
{
if (i<10)
listBox1.Items.Add(i);
else if(i<100 && i>9)//gets numbers smaller then 100 AND bigger then 9 (so 10-99)
listbox2.Items.Add(i)
//etc
}

Something like this will make you seperate the numbers so you can add them in the different columns/listboxes. Didnt write this in visual studio so there might be small errors in it.
Was This Post Helpful? 0
  • +
  • -

#9 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 09 August 2009 - 10:18 PM

well i want to add table( numbers) dynamically.
the table size may vary as the size of question will vary,
so can this is possible with the help of your previous code....?
Was This Post Helpful? 0
  • +
  • -

#10 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Ascending-Descending in window application?

Posted 10 August 2009 - 04:39 AM

Not in a pretty way no. I figured you only had smaller numbers so then this way would be ok.

But if you want a wider range I suppose you could make a loop that sorts them according to the number of digits they have. Also you have to think about how big the numbers are you want to use?And do you wants to use negative numbers, decimals maybe?

This post has been edited by Renagado: 10 August 2009 - 04:40 AM

Was This Post Helpful? 1
  • +
  • -

#11 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 10 August 2009 - 06:07 AM

Thanks 4 ur rply...............
Was This Post Helpful? 0
  • +
  • -

#12 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Ascending-Descending in window application?

Posted 10 August 2009 - 08:49 AM

You're welcome. Good luck coding and let us know if you hit another problem again.
Was This Post Helpful? 0
  • +
  • -

#13 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 11 August 2009 - 05:56 AM

Hi renagado........

can u tell me one more thing.

i m making a window application in which i want to find n-digit number in all.

i mean like i have a textbox in which i can enter any digit, like 1,2,3,4,5........

suppose i m entering 7 and after that when i click on button then it will show the total 7 digit numbers in another textbox.

like the max. number of 7 digit is 9999999 and min 7 digit number is 1000000 , so the total 7 digit number will be

((max-min)+1) this is the formula.

so can u tell me how can i do this with the help of C# window application?
Was This Post Helpful? 0
  • +
  • -

#14 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Ascending-Descending in window application?

Posted 11 August 2009 - 07:02 AM

Sure you can do that. The general formulae would be something like y=10^(x)-10^(x-1)where x is the number of digits and y the number of possibilities. For example if x=3 this gives y=10^3-10^2=1000-100=900 which represents then numbers 100 to 999. This is what you mean right? Now in c# code this would look like:(for example)

		   int x = Convert.ToInt32(textBox1.Text);
			 double y = Math.Pow(10, x) - Math.Pow(10, (x-1));
			 textBox2.Text = Convert.ToString(y);


Edit: changed the code so that it actually works:P

This post has been edited by Renagado: 11 August 2009 - 07:47 AM

Was This Post Helpful? 1
  • +
  • -

#15 avirag  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 92
  • Joined: 29-June 09

Re: Ascending-Descending in window application?

Posted 11 August 2009 - 11:02 PM

Hi Renagado.......
thanks 4 ur previous rply,
well i hav some more problems, can u plz resolve these.

1) i made a treeview in my application, i want that when i expand one node of the tree , so at that time if any other nodes are open, so those nodes will automatically decomposed.only one node expand at a time. can u tell me what is the property 4 this, or i have to write some code 4 this?

2) in this application i have another problem that is.
when i enter 6*10000+4*1000+7*100+3*1 in one textbox and when i click on button it will give its result in another textbox like its result is : 64703.
And smae in reverse case that when i enter 64703 in one textbox , so on button click it will show its expanded form in another textbox...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2