J2KLF

# SOMETHING ABOUT STACK

myStack.cs

  using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.Windows.Forms;

  namespace myStack {
      public partial class MainForm : Form {
          List<string> _items = new List<string>();
          int x = 0;
          
          public MainForm() {
              InitializeComponent();
              lbStack.DataSource = _items;
          }
          
          void bnPushClick(object sender, EventArgs e)    {
              if (txtValue.Text.Length > 0) {
                  _items.Insert(0, txtValue.Text);
                  lbStack.DataSource = null;
                  lbStack.DataSource = _items;
                  lbStack.SelectedIndex = 0;
              }
          }
          
          void bnPopClick(object sender, EventArgs e) {
              int selectedIndex = lbStack.SelectedIndex;
              try {
                  _items.RemoveAt(selectedIndex);
              }
              catch {
              }
      
              lbStack.DataSource = null;
              lbStack.DataSource = _items;
              if (lbStack.Items.Count > 0) lbStack.SelectedIndex = 0;
          }
      }
  }

EOF

# LINKS

  1. https://www.guru99.com/c-sharp-windows-forms-application.html#3
  2. https://www.dotnetperls.com/listbox
