如何在 WPF C# 中修改和显示队列
How to modify and show a queue in a WPF c#
我试图在添加一些项目后在 Windows Presentation Foundation 的 TextBox 中显示一个队列,我知道这一定很简单,我已经用断点检查了代码,添加项目按钮效果很好,但是一旦我再次按下它,队列是空的,我总是只添加一个项目,一旦我添加它,我再次按下相同的按钮添加项目按钮或显示按钮队列是空的,我想为了添加项目并显示包含我添加的项目的队列,我制作了一个名为 QueueClas 的 class。下面是全部代码,先谢谢了!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Queue2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
QueueClas queuec = new QueueClas();
buttonAdd.Click += ButtonAdd_Click;
buttonShow.Click += ButtonShow_Click;
}
private void ButtonShow_Click(object sender, RoutedEventArgs e)
{
QueueClas queuec = new QueueClas();
textBoxShow.Text = queuec.ShowQueue();
}
private void ButtonAdd_Click(object sender, RoutedEventArgs e)
{
QueueClas queuec = new QueueClas();
queuec.AddQueue(int.Parse(textBoxQueue.Text));
textBoxQueue.Clear();
}
public class QueueClas
{
Queue<int> myqueue;
public QueueClas()
{
myqueue = new Queue<int> { };
}
public void AddQueue(int x)
{
myqueue.Enqueue(x);
}
public string ShowQueue()
{
return string.Join(" ", myqueue);
}
public void DeleteItem(int x)
{
myqueue.Dequeue();
}
public string NumberOfItems()
{
int counter = 0;
counter = myqueue.Count();
return "The queue contains " + counter.ToString() + " elements";
}
public string MinQueue()
{
return "The minimun value of the queue is: " + myqueue.Min().ToString();
}
public string MaxQueue()
{
return "The maximum value of the queue is: " + myqueue.Max().ToString();
}
public string FindElement(int x)
{
foreach (int item in myqueue)
{
if (x == item)
{
return "The item is in the queue";
}
}
return "The item is not in the queue";
}
}
}
}
在添加和显示按钮中,您正在使用 QueueClas queuec = new QueueClas();
初始化您的列表。这完全删除了重新开始的列表。您已经在 MainWindow
构造函数中对其进行了初始化,因此无需再次执行。
我试图在添加一些项目后在 Windows Presentation Foundation 的 TextBox 中显示一个队列,我知道这一定很简单,我已经用断点检查了代码,添加项目按钮效果很好,但是一旦我再次按下它,队列是空的,我总是只添加一个项目,一旦我添加它,我再次按下相同的按钮添加项目按钮或显示按钮队列是空的,我想为了添加项目并显示包含我添加的项目的队列,我制作了一个名为 QueueClas 的 class。下面是全部代码,先谢谢了!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Queue2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
QueueClas queuec = new QueueClas();
buttonAdd.Click += ButtonAdd_Click;
buttonShow.Click += ButtonShow_Click;
}
private void ButtonShow_Click(object sender, RoutedEventArgs e)
{
QueueClas queuec = new QueueClas();
textBoxShow.Text = queuec.ShowQueue();
}
private void ButtonAdd_Click(object sender, RoutedEventArgs e)
{
QueueClas queuec = new QueueClas();
queuec.AddQueue(int.Parse(textBoxQueue.Text));
textBoxQueue.Clear();
}
public class QueueClas
{
Queue<int> myqueue;
public QueueClas()
{
myqueue = new Queue<int> { };
}
public void AddQueue(int x)
{
myqueue.Enqueue(x);
}
public string ShowQueue()
{
return string.Join(" ", myqueue);
}
public void DeleteItem(int x)
{
myqueue.Dequeue();
}
public string NumberOfItems()
{
int counter = 0;
counter = myqueue.Count();
return "The queue contains " + counter.ToString() + " elements";
}
public string MinQueue()
{
return "The minimun value of the queue is: " + myqueue.Min().ToString();
}
public string MaxQueue()
{
return "The maximum value of the queue is: " + myqueue.Max().ToString();
}
public string FindElement(int x)
{
foreach (int item in myqueue)
{
if (x == item)
{
return "The item is in the queue";
}
}
return "The item is not in the queue";
}
}
}
}
在添加和显示按钮中,您正在使用 QueueClas queuec = new QueueClas();
初始化您的列表。这完全删除了重新开始的列表。您已经在 MainWindow
构造函数中对其进行了初始化,因此无需再次执行。