• 0

Text not appearing in textBox


Question

Here is what the form looks like and its respective toolbox item name

 

Form and it's names

 

Here's the code for the Form and the class

 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void addFlapjacks_Click(object sender, EventArgs e)
        {
            Flapjack food;
            if (crispy.Checked == true)
                food = Flapjack.Crispy;
            else if (soggy.Checked == true)
                food = Flapjack.Soggy;
            else if (browned.Checked == true)
                food = Flapjack.Browned;
            else
                food = Flapjack.Bannana;

            Lumberjack currentLumberjack = breakfastLine.Peek();
            currentLumberjack.TakeFlapjacks(food, (int)howMany.Value);
            RedrawList();
        }

        private Queue<Lumberjack> breakfastLine = new Queue<Lumberjack>();

        private void addLumberjack_Click(object sender, EventArgs e)
        {
            breakfastLine.Enqueue(new Lumberjack(name.Text));
            name.Text = "";
            RedrawList();
        }

        private void RedrawList()
        {
            int number = 1;
            line.Items.Clear();
            foreach (Lumberjack lumberjack in breakfastLine)
            {
                line.Items.Add(number + ". " + lumberjack.Name);
                number++;
            }
            if (breakfastLine.Count == 0)
            {
                groupBox1.Enabled = true;
                Lumberjack currentLumberjack = breakfastLine.Peek();
                nextInLine.Text = currentLumberjack.Name + " has " + currentLumberjack.FlapjackCount + " flapjacks.";
            }
        }

        private void nextLumberjack_Click(object sender, EventArgs e)
        {
            Lumberjack nextLumberjack = breakfastLine.Dequeue();
            nextLumberjack.EatFlapjacks();
            nextInLine.Text = "";
            RedrawList();
        }
    }

CLASS:

 

 enum Flapjack
    {
        Crispy,
        Soggy,
        Browned,
        Bannana
    }
    class Lumberjack
    {
        private string name;
        public string Name { get { return name; } }
        private Stack<Flapjack> meal;
        public Lumberjack(string name)
        {
            this.name = name;
            meal = new Stack<Flapjack>();
        }
        public int FlapjackCount { get { return meal.Count; } }

        public void TakeFlapjacks(Flapjack food, int howMany)
        {
            for (int i = 0; i < howMany; i++)
            {
                meal.Push(food);
            }
        }

        public void EatFlapjacks()
        {
            Console.WriteLine(name + "'s eating flapjacks");
            while(meal.Count > 0)
            {
                Console.WriteLine(name + " ate a " + meal.Pop().ToString().ToLower() + " flapjack");
            }
        }
    }

The problem here i'm getting is in Form1 under RedrawList() ---> nextInLine TextBox should display what ever nextInLine.Text line is equal to but nothing is being displayed in the textbox during execution, up on button click of Add flapjacks it will call RedrawList () and a mesage should be displayed in the textbox like if name = ed and howMany = 3 text displaayed should be "Ed has 3 flapjacks" not no display upon button click.

I have set the property of the textbox to Read only, 

Any Idea why there is no text in textbox ?

Link to comment
https://www.neowin.net/forum/topic/1329156-text-not-appearing-in-textbox/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

 

if (breakfastLine.Count == 0)
{
    // if we get here, breakfastLine.Count == 0, right? 
    groupBox1.Enabled = true;
    // so what do you expect this line to do? There are no items to peek at, it's empty, you just verified it was
    Lumberjack currentLumberjack = breakfastLine.Peek(); 
    // Let's look at the documentation for Queue.Peek https://msdn.microsoft.com/en-us/library/system.collections.queue.peek.aspx
    // | Exception	                | Condition
    // | InvalidOperationException	| The Queue is empty. 
  
    // So an exception is always thrown and this next line is never executed
    nextInLine.Text = currentLumberjack.Name + " has " + currentLumberjack.FlapjackCount + " flapjacks.";
}

On a side note, you would do well to learn basic debugging techniques (i.e. start your program by using F5 "Start Debugging" in Visual Studio). By default Visual Studio stops execution on exceptions, highlights the offending line and shows you the details of the exception. That would have shown you right where the problem was.

 

Another basic technique would be to put a breakpoint on the line where you expect the change to happen; you would have seen it was never hit.

 

These are essential skills to develop!

  • 0
On 4/26/2017 at 6:06 AM, Andre S. said:

 


if (breakfastLine.Count == 0)
{
    // if we get here, breakfastLine.Count == 0, right? 
    groupBox1.Enabled = true;
    // so what do you expect this line to do? There are no items to peek at, it's empty, you just verified it was
    Lumberjack currentLumberjack = breakfastLine.Peek(); 
    // Let's look at the documentation for Queue.Peek https://msdn.microsoft.com/en-us/library/system.collections.queue.peek.aspx
    // | Exception	                | Condition
    // | InvalidOperationException	| The Queue is empty. 
  
    // So an exception is always thrown and this next line is never executed
    nextInLine.Text = currentLumberjack.Name + " has " + currentLumberjack.FlapjackCount + " flapjacks.";
}

On a side note, you would do well to learn basic debugging techniques (i.e. start your program by using F5 "Start Debugging" in Visual Studio). By default Visual Studio stops execution on exceptions, highlights the offending line and shows you the details of the exception. That would have shown you right where the problem was.

 

Another basic technique would be to put a breakpoint on the line where you expect the change to happen; you would have seen it was never hit.

 

These are essential skills to develop!

Oh my bad i corrected it by changing

if (breakfastLine.Count == 0)  ---> if (breakfastLine.Count != 0)

 

OR to be precise

 

 if (breakfastLine.Count == 0)
            {
                groupBox1.Enabled = false;
                nextInLine.Text = "";
            }
            else   
            {
                groupBox1.Enabled = true;
                Lumberjack currentLumberjack = breakfastLine.Peek();
                nextInLine.Text = currentLumberjack.Name + " has " + currentLumberjack.FlapjackCount + " flapjacks.";
            }

 

THANK YOU for Replying

This topic is now closed to further replies.
  • Posts

    • YES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    • Apple are scared of their customers! They have built a brand over the years of "it just works out of the box", but that slows innovation. Samsung's master stroke was the Galaxy Ultra: "Let's cram everything into one handset, make it so stupid only real nerds will love it, some of the features will work, some won't, but the audience will have such a high tolerance they won't care". Apple has no such device and so they are constantly worrying these days about the fallout of creating a new experience that customers might not like. I know it is often cited the reason they don't build a touchscreen Mac Book is they don't want to cannibalise the the iPad market, but I think it's equally cold feet after the criticism Microsoft receive trying to make a touch compatible desktop OS
    • HandBrake 1.11.2 by Razvan Serea HandBrake is an open-source, GPL-licensed, multiplatform, multithreaded video transcoder, available for MacOS X, Linux and Windows. Handbrake can process most common multimedia files and any DVD or BluRay sources that do not contain any kind of copy protection. Here is a detailed breakdown of HandBrake’s features: Built-in Device Presets—Get started with HandBrake in seconds by choosing a profile optimized for your device, or choose a universal profile for standard or high quality conversions. Simple, easy, fast. For those that want more choice, tweak many basic and advanced options to improve your encodes. Supported Input Sources—Handbrake can process most common multimedia files and any DVD or Blu-ray sources that do not contain any kind of copy protection. Outputs: File Containers: .MP4(.M4V) and .MKV Video Encoders: H.265 (x265 and QuickSync), H.264(x264 and QuickSync), H.265 MPEG-4 and MPEG-2, VP8 and Theora Audio Encoders: AAC / HE-AAC, MP3, Flac, AC3, or Vorbis Audio Pass-thru: AC-3, E-AC3, DTS, DTS-HD, TrueHD, AAC and MP3 tracks Additional features: Title/ Chapter Selection Queue up Multiple Encodes Chapter Markers Subtitles (VobSub, Closed Captions CEA-608, SSA, SRT) Constant Quality or Average BitRate Video Encoding Support for VFR, CFR and VFR Video Filters—Deinterlacing, Decomb, Detelecine, Deblock, Grayscale, Cropping and Scaling Live Video Preview HandBrake 1.11.2 changelog: All platforms Video Fixed a crash that happened when doing a 2-pass lossless x265 encode Fixed a memory leak that happened when doing a 2-pass MPEG-4/MPEG-2/VP9/FFV1 encode Audio Updated the list of supported dithers and encoders combinations Fixed the Core Audio AAC encoder 7.1 channel layout Subtitles Fixed the VobSub palette creation in the MP4 container Build system Improved build system compatibility with older build tools Third-party libraries FFmpeg 8.0.2 (decoding and filters) SVT-AV1 4.1.0 (AV1 video encoding) Linux Added WebM MIME type to the list of the supported formats Mac Improved handling of unsupported presets Updated Sparkle automatic update library Windows Improved handling of unsupported presets Improved queue low space pause behaviour Fixed the automatic audio track name generation Fixed the summary description of HDR video Download: HandBrake 64-bit | Portable 64-bit | ~30.0 (Open Source) Download: HandBrake ARM64 | Portable Links: HandBrake Website | Other Operating Systems | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • So, an article that has nothing to do with Windows 11, still gets Windows 11 in the title and a build number as the picture? Dell have a buggy build of Support Assist HP have UEFI settings that need unlocking for the secureboot cert upgrade to take place.
    • What I can't seem to understand is that Google are paying SpaceX? Surely it would be the other way round, with SpaceX needing Google's datacenters etc.? Oh well, this level of money and power is way outside of my comprehension at the best of times.
  • Recent Achievements

    • One Month Later
      DJC50PLUS earned a badge
      One Month Later
    • Week One Done
      DJC50PLUS earned a badge
      Week One Done
    • Proficient
      Eric Biran went up a rank
      Proficient
    • Dedicated
      Conjor earned a badge
      Dedicated
    • Week One Done
      Windows Guy earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      493
    2. 2
      PsYcHoKiLLa
      243
    3. 3
      Steven P.
      72
    4. 4
      neufuse
      67
    5. 5
      ATLien_0
      67
  • Tell a friend

    Love Neowin? Tell a friend!