[Urgent] How may I add this code to an outputLabel-VBForums
Results 1 to 7 of 7

Thread: [Urgent] How may I add this code to an outputLabel

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    5

    [Urgent] How may I add this code to an outputLabel

    Alright basically the assignment asks to use an outputLabel instead of the salesListBox Im using which im not sure how to do.

    The final thing I have left but can't figure out is how to display the average,largest value and the smallest value it should look like this

    Name:  prog.jpg
Views: 42
Size:  28.2 KB




    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] allLines = File.ReadAllLines("Sales.txt");
                    double[] numbers = new double[allLines.Length];
                    int counter = 0;
                    double sum = 0;
    
                    //populate numbers from allLines to numbers
                    foreach (string value in allLines)
                    {
                        numbers[counter] = Convert.ToDouble(value);
                        sum += numbers[counter];
                        salesListBox.Items.Add(numbers[counter]);
                        counter++;
                    }
    
                    salesListBox.Items.Add("The total is " + sum.ToString("n"));
                    salesListBox.Items.Add("The average is " 
                    salesListBox.Items.Add("The largest value is " 
                    salesListBox.Items.Add("The smallest is " 
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
    Attached Images Attached Images  
    Last edited by bob2222; Nov 13th, 2017 at 09:54 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,845

    Re: [Urgent] How may I add this code to an outputLabel

    Firstly, if you're going to post code then please post it as text inside appropriate formatting tags. That way, we can copy and paste it if we need to.

    Secondly, please always provide a FULL and CLEAR explanation of the problem. Assume that too many words is better than not enough.

    Your brief title doesn't actually make any sense as is. You don't add code to a Label. A Label exists to display text. Whatever text you want to display, you need to assign to the Text property as a String. That's it, that's all.

    What is it that you are actually asking? Do you want to know how to display a multiline String in a Label? If so then that's what you should say. That really has nothing to do with the Label. As I said, the Label just displays a String. What that String contains, including line breaks, is up to you. If you want to insert a line break into text in VB then you cannot use a C escape sequence as you can in C#. Generally, you should use Environment.NewLine, e.g.
    vb.net Code:
    1. Dim str = "First Line" & Environment.NewLine & "Second Line"
    How exactly you go about building the String depends on the specifics of the situation. If you wanted to append a line to what's already in the Label then you basically do as I've done above and use the current Text of the Label as the first String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    5

    Re: [Urgent] How may I add this code to an outputLabel

    Alright basically the assignment asks to use an outputLabel instead of the salesListBox Im using which im not sure how to do.

    The final thing I have left but can't figure out is how to display the average,largest value and the smallest value it should look like this

    Name:  prog.jpg
Views: 42
Size:  28.2 KB




    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] allLines = File.ReadAllLines("Sales.txt");
                    double[] numbers = new double[allLines.Length];
                    int counter = 0;
                    double sum = 0;
    
                    //populate numbers from allLines to numbers
                    foreach (string value in allLines)
                    {
                        numbers[counter] = Convert.ToDouble(value);
                        sum += numbers[counter];
                        salesListBox.Items.Add(numbers[counter]);
                        counter++;
                    }
    
                    salesListBox.Items.Add("The total is " + sum.ToString("n"));
                    salesListBox.Items.Add("The average is " 
                    salesListBox.Items.Add("The largest value is " 
                    salesListBox.Items.Add("The smallest is " 
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
    Last edited by bob2222; Nov 13th, 2017 at 09:55 PM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,845

    Re: [Urgent] How may I add this code to an outputLabel

    As I said, a Label just displays the contents of a single String. How you construct that String is up to you. I've shown you one way and there others too, e.g. using a StringBuilder or String.Format.

    You already have the sum so, if you've done primary school mathematics, you know how to calculate an average from that. With regards to the minimum and maximum, one way is to declare a couple of variables and initialise them with Double.MaxValue and Double.MinValue. As you loop through the values, you can then compare each one to those variables and replace one of them with the current value if appropriate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,002

    Re: [Urgent] How may I add this code to an outputLabel

    The code is C#, so the thread has been moved to the C# forum.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    5

    Re: [Urgent] How may I add this code to an outputLabel

    cool lets see if someone is decent enough to help

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,845

    Re: [Urgent] How may I add this code to an outputLabel

    Quote Originally Posted by Shaggy Hiker View Post
    The code is C#, so the thread has been moved to the C# forum.
    Good grief! The original post contained nothing but the screenshot of the single line of code and I assumed that it was being "converted" to VB. I didn't even register when the rest of the code was posted later that it was C# in the VB.NET form. Obviously you can use C escape sequences in C# but you can also use Environment.NewLine. That means that this:
    vb.net Code:
    1. Dim str = "First Line" & Environment.NewLine & "Second Line"
    would be this:
    csharp Code:
    1. var str = "First Line" + Environment.NewLine + "Second Line";
    or this:
    csharp Code:
    1. var str = "First Line" + "\r\n" + "Second Line";
    or, if you really are dealing with literals, this:
    csharp Code:
    1. var str = "First Line\r\nSecond Line";
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.