• 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 26/04/2017 at 00:36, 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!

Expand  

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

    • Save 90% on a 12min Premium lifetime subscription, and power through books by Steven Parker Today's highlighted deal comes via our Apps + Software section of the Neowin Deals store, where you can save 90% on a lifetime premium subscription to 12min Micro Book Library. Power through important books and interesting topics in just 12 minutes each. 12min gives you access to hundreds of micro books in text and narrative form for you to explore. Every month, you'll get 30 new titles that are designed to be read and digested in just 12 minutes so you can learn on the go. Search for any book title in the extensive library or suggest new breakdowns to the 12min experts and they'll add it to the library. You can choose micro books from many different genres, send them to your Kindle Account, and enjoy reading them even while not having internet access. Expand your knowledge despite a busy schedule by reading micro books from different topics Select a book that you want & the 12min team will synthesize it into a short but comprehensive micro book Access micro books in text or audio forms Read anywhere with or without internet connection Good to know Length of access: lifetime This plan is only available to new users Redemption deadline: redeem your code within 30 days of purchase Note: Offer must be redeemed on desktop before being useable on mobile A Premium subscription to 12min normally costs $399.90, but you can pick this up for just $39.99 for a limited time. For a full description, terms and license click the link below. Get a 12min Premium lifetime subscription for just $39.99 (90% off) Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • The $100 million mark was rejected a couple of days ago.
    • This super-powerful GaN charger with four ports is 50% off by Taras Buria Some time ago, I reviewed the Cuktech 10, a powerful 100W GaN charger with three ports. It left positive impressions, and since then, it has served me as my primary charger for my phone, watch, laptop, and tablet. Recently, Cuktech offered me the opportunity to take a look at the model 15, a more powerful 140W GaN charger, this time, with four ports. Right now, this powerful charger is available with a massive discount at just $50.99 (with coupon applied). The Cuktech 15 is the same as the Cuktech 10, just slightly bigger, more powerful, and with one extra port. It has a light metallic finish, but overall, it retains the brand's identity and features. The four ports are well spaced out, and the black plastic insert with a cyan rim has a ribbed texture for better grips. Although I live in Europe, Cuktech sent me a US variant with a retractable plug. Okay, I guess. The ability to retract the plug makes it extra portable, which is nice. A high-power 240W five-foot cable is also included. The charger measures 3.11 x 2.56 x 1.26 inches and weighs 0.737 lbs. The Cuktech 15 has four ports: two high-power Type-C ports, one Type-C port with a lower output, and one Type-A port. The first two Type-C ports can deliver a full 140W in single-port mode (PD 3.1 supported), which is nice—no asterisk or caveats here with combined power or something. If you need the full 140W for one device, you get it. Ports are capable of working in the following modes: Single-port Type-C1 / C2: 140W max 5V 2A, 5V 3A, 9V 3A, 11V 6.1A, 12V 3A, 15V 3A, 20V 5A, 28V 5A Type-C3: 33W max 5V 2A, 5V 2.4A, 9V 2A, 12V 1.5A, 11V 3A Type-A: 18W max 5V 2A, 5V 3A, 9V 2A, 12V 1.5A Multi-Port Type-C1 + C2: 100W + 33W or 65W + 65W Type-C1/C2 + Type-C3: 100W + 33W Type-C1/C2 + Type-A: 100W + 18W Type-C1 + Type-C2 + Type-C3: 65W + 60W + 7.5W or 45W + 45W + 18W Type-C1 + Type-C2 + Type-C3 + Type-A: 65W + 60W + 7.5W As you can see, the charger is pretty robust, and it can power two pretty powerful laptops at once and even have enough oomph to charge a smartphone, albeit at a lower power. Another thing worth mentioning is that the Cuktech 15 delivers "clean" power with pretty low pulsations at about 25-50 mV. The rule of thumb is that the lower the pulsations, the better the charger is for your device's battery health. In this area, Cuktech's charger does not disappoint, and they deliver way better results than 100 mV, which is considered a standard for a good charger. Cuktech uses gallium nitride technology, which enables smaller, more powerful and efficient charging. Speaking of efficiency, the charger is rated for 78% average or 64% at a 10% load. When charging at 120W, I received an average of 80-85%, which is good. Of course, when charging at peak power, it gets hot, but not too much. The Cuktech 15 140W usually costs $99.99, which is undoubtedly not cheap. However, right now, you can get it for half the price, which is a very good deal, considering you get a high-quality charger with plenty of ports and very high power output. Like with the Cuktech 10, you cannot go wrong with this one. CUKTECH 15 140W four-port GaN charger - $50.99 | 30% off + a 20% off coupon As an Amazon Associate, we earn from qualifying purchases.
    • Awesome book, just hope they don't screw up Rocky and the story
    • KDE's KClock is getting Wayland Picture-In-Picture support by David Uzondu The KClock app for KDE Plasma over the years has received a number of updates, like better integration with KRunner and a dedicated background service (kclockd) for managing alarms. Now, it looks like KDE devs want to add something cool: pop-out timers using the new Picture-in-Picture protocol for Wayland. Image: Kai Uwe Broulik This all started from a simple observation. Kai Uwe Broulik, a KDE developer, saw someone using a small timer window during a presentation and thought it was a good idea. The problem is that achieving this kind of "always on top" behavior is handled differently between the old X11 display server and the newer Wayland. With X11, an application could pretty much do whatever it wanted. If a program wanted to draw a drop-down menu, it would just create a borderless window, place it in a specific spot, and grab all user input. Wayland operates on a different philosophy. As Broulik notes, under Wayland, the application describes what it wants, and the compositor gets to decide how to handle it. A drop-down menu is an XDG Popup. The application tells the compositor which button spawned it, and the compositor handles the placement and behavior. This is much more secure and consistent. It also means an application cannot just decide to keep its window on top of everything else. This restriction prevents a web browser from implementing an overlay video player under Wayland. To get around this in a standardized way, a proper Wayland support model for Picture-in-Picture, or PiP, was needed. Enter the xx-pip-v1 protocol. It is a new protocol designed specifically for creating floating PiP windows, and KWin, Plasma's compositor, recently gained support for it. Because it is an experimental protocol, its use is gated behind an environment variable, KWIN_WAYLAND_SUPPORT_XX_PIP_V1. A new protocol is fine for demos, but it needs a real application to find its weaknesses. So Broulik implemented it in KClock. This work allows KClock to offer pop-out timers and even a pop-out stopwatch in a small PiP window. The user could get system-wide options to control where the PiP window appears, or if it appears at all, and have that setting apply to every single application that uses the protocol. You can check out the merge request on GitLab for more technical details about this feature.
  • Recent Achievements

    • One Month Later
      CHUNWEI earned a badge
      One Month Later
    • Week One Done
      TIGOSS earned a badge
      Week One Done
    • First Post
      henryj earned a badge
      First Post
    • First Post
      CarolynHelen earned a badge
      First Post
    • Reacting Well
      henryj earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      475
    2. 2
      +FloatingFatMan
      195
    3. 3
      ATLien_0
      163
    4. 4
      Xenon
      80
    5. 5
      Som
      76
  • Tell a friend

    Love Neowin? Tell a friend!