Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello guys,
 
There are 10 textboxes and 1 button. The user is going to enter 1 number into each textbox and then click the button to find the highest number.
 
How can I find the highes number among 10 numbers?
 
This is the code but it didn't work. Is there any different way?
 
    private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")
            {
                MessageBox.Show("Please enter a number...!");
            }
            else
            {
                int[] d = new int[groupBox1.Controls.Count];
                for (int i = 0; i < groupBox1.Controls.Count; i++)
                {
                    if (groupBox1.Controls[i] is TextBox)
                    {
                        TextBox t = groupBox1.Controls[i];
                        d[i] = int.Parse(t.Text);
                    }
                }
                Array.Sort(d);
                Array.Reverse(d);
                
            }
        }
 

Regards
 
Haluk
Posted 19-Dec-12 8:16am
Edited 19-Dec-12 8:20am
v5
Comments
joshrduncan2012 at 19-Dec-12 14:16pm
   
Search for maximum, depending on how the numbers are stored?
lewax00 at 19-Dec-12 14:21pm
   
What do you mean by "it didn't work"? Did it throw an exception, did it return the wrong value, something else?
haluk_78 at 19-Dec-12 14:25pm
   
TextBox t = groupBox1.Controls[i];
Cannot implicitly convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.TextBox'. An explicit conversion exists (are you missing a cast?)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

I agree with Christian, but I couldn't resist posting a more elegant solution than the others.
 
int max = (from c in this.Controls.Cast<Control>()
           where c is TextBox && Regex.IsMatch(c.Text.Trim(),"^[0-9]{1,10}$")
           select int.Parse(c.Text.Trim())).Max();
  Permalink  
v3
Comments
Christian Graus at 19-Dec-12 15:49pm
   
Wow - that IS nice
BC @ CV at 19-Dec-12 15:50pm
   
Thanks, that means a lot coming from you.
CPallini at 19-Dec-12 15:56pm
   
:-) 5.
BC @ CV at 19-Dec-12 15:59pm
   
Grazie signore.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

OK, so three people have done your homework for you, so I feel I can weigh in. Saying something 'didn't work', is useless. Especially when it threw an exception, how were we supposed to know that ? An exception means that your code doesn't run. It is telling you something specifically is wrong in your code. Your general idea would work ( although it's not optimal ). So, what you really wanted to know was 'how do I fix this exception' ? And the exception was very clear as to what was wrong. You used 'is' to work out that the control was a textbox, but then you accessed it, as a control. You need to use 'as' to cast it to a textbox, and then access the text in the textbox. Overall, this code was very convoluted, and used some complex topics for someone who doesn't know what an exception is. Instead of piecing code together from snippets you learn here and there, without learning basics, you should buy a book and work through it, so you learn basics first and don't miss major parts of what you need to know to be a programmer
  Permalink  
Comments
Abhishek Pant at 19-Dec-12 16:32pm
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Change:
TextBox t = groupBox1.Controls[i];
 
to:
TextBox t = (TextBox)groupBox1.Controls[i];
 
or
 
TextBox t = groupBox1.Controls[i] as TextBox;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 List<sayilar> liste = new List<sayilar>();
            foreach (Control st in panel1.Controls)
            {
                int deger = Convert.ToInt32(st.Text);
                liste.Add(new Sayilar(deger));
            }
            int enBuyuk = (from m in liste select m.Sayi).Max();
            MessageBox.Show(enBuyuk.ToString()); }
 
    public class Sayilar
    {
        public int Sayi
        {
            get;
            set;
        }
 
        public Sayilar(int sayi)
        {
            this.Sayi = sayi;
        }
    }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 410
1 Maciej Los 258
2 CHill60 222
3 OriginalGriff 210
4 Sinisa Hajnal 125


Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100