Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Output To a Text Box

Tags:

c#

output

textbox

This is going to sound really stupid, but I'm taking a class in C# where we are skipping around the book and working only from a console application. We were given an exercise to build sentences in strings based on arrays of articles, nouns, verbs, and prepositions, and capitalize the first letter in the first word of the string. The kicker is, it wants the output to a text box. That wouldn't be a problem except

a) we have bypassed all chapters regarding GUIs (that will come in the next quarter's C# class), and

b) I have checked the book and even Stack Overflow and other online sources, but couldn't figure it out.

Unfortunately, my instructor chose not to discuss this exercise in class last night. Since he and I aren't on the same page (not a dislike, more of a chemistry thing), I'm trying to figure this out on my own. And the deadline for turning this in has passed, so I'm only asking for personal edification at this point.

So, here's the code I created. I wrote it for output to a console just to show I had the basic mechanism of the problem. I know I have to create a separate form with a text box inside a GUI window, but I couldn't figure out how to send the output to a text box rather than a console.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _16._4_StoryWriter
    {
       class StoryWriter
       {
          static void Main(string[] args)
          {
             string[] articles = { "the", "a", "one", "some", "any" };
             string[] nouns = { "boy", "girl", "dog", "town", "car" };
             string[] verbs = { "drove", "jumped", "ran", "walked", "skipped" };
             string[] preps = { "to", "from", "over", "under", "on" };

             string articleStory = "";
             string nounStory = "";
             string verbStory = "";
             string prepStory = "";

             Random random = new Random();


             for (int counter = 1; counter <= 10; ++counter)
             {
                int randomNext = random.Next(5);
                articleStory = articles[randomNext];


                randomNext = random.Next(5);
                nounStory = nouns[randomNext];

                randomNext = random.Next(5);
                verbStory = verbs[randomNext];

                randomNext = random.Next(5);
                prepStory = preps[randomNext];

                Console.WriteLine(UppercaseFirst(articleStory) + " " + nounStory + " " + verbStory + " " + prepStory + ".");
             } // End For

             Console.Read();
          } // End Main

          static string UppercaseFirst(string s) // Borrowed from dotnetperls.com tutorial for making first letter uppercase
          {
             if (string.IsNullOrEmpty(s)) // Checks for an empty string
             {
                return string.Empty;
             }
             char[] a = s.ToCharArray(); // Creates array of characters from a string
             a[0] = char.ToUpper(a[0]); // Selects value of zeroth position and changes to upper case
     return new string(a); // Passes new string back
          } // End method

       } // End Class
    } // End Namespace        
like image 315
JBM Avatar asked Mar 13 '23 05:03

JBM


1 Answers

To create a Windows Forms Application project Start Visual Studio 2010.

On the File menu, point to New, and then select Project.

The New Project dialog box appears.

In the Installed Templates pane, expand Visual Basic or Visual C#, and then select Windows.

Above the middle pane, select the target framework from the drop-down list.

In the middle pane, select the Windows Forms Application template.

NoteNote The Windows Forms Application template in the .NET Framework 4 targets the Client Profile by default.

In the Name text box, specify a name for the project.

In the Location text box, specify a folder to save the project. Click OK.

The Windows Forms Designer opens and displays Form1 of the project.

SOURCE

Then drag textbox from toolbox and place it on the form.

Double click anywhere on the form, except for textbox, which will open code behind form and you will be in form load event.

Add:

textBox1.Text = "Your text to put in textbox";

in:

private void Form1_Load(object sender, EventArgs e)
{
    textBox1.Text = "Your text to put in textbox";
}

press F5

Youtube Form Youtube textbox

like image 196
Claudius Avatar answered Mar 25 '23 04:03

Claudius