c# - 将值放入字典并在文本框中显示它们

标签 c# dictionary textbox

我希望这不是一个愚蠢的问题,但我正在尝试创建一个简单的表单,其中有 2 个数字上调和一个按钮,我获取这些值并将它们作为两个字符串存储在字典中。现在我认为一切顺利,但我想在按下按钮后也在单独的文本框中显示这些值。

namespace TestWayPointEditor
{
    public struct Coordinate
    {
    public string Latitude { get; set; }
    public string Longitude { get; set; }

    public Coordinate(string latitude, string longitude)
    {
    this.Latitude = latitude;
    this.Longitude = longitude;
    }

}
public partial class Form1 : Form
    {

    HashSet<Coordinate> WayPoints = new HashSet<Coordinate>();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        //Set lat and lon values in a string
        string Lat = LatUpDown.Value.ToString();
        string Lon = LongUpDown.Value.ToString();

        WayPoints.Add(new Coordinate(Lat, Lon));

    }
}
}

enter image description here My form looks like this to give an idea of what I am doing

因此,如果我有纬度和经度并按添加,它应该出现在文本框中并将其保存在背景中。如果我更改值并按添加,第二行应该被填充,这应该继续下去。除此之外,我希望当我关闭此表单(这是第二种形式的项目)并再次打开它时,我并没有失去我的值(value)观。

我想我必须为此创建一个类,它应该是公开的。但我实际上不知道从哪里开始。希望有人指导。

最佳答案

当我们可能需要使用键获取值时,字典更适合具有键值结构的数据......

显然 latitude不是 longitude因此 Dictionary除非 key 包含有意义的内容,否则集合不是最佳选择。

我们可以创建一个自定义结构 Coordinate来存储我们的值(value)。此类结构的一个示例是 System.Drawing.Point .

public struct Coordinate
{
    public string Latitude  { get; } 
    public string Longitude { get; }

    public Coordinate(string latitude, string longitude)
    {
        this.Latitude = latitude;
        this.Longitude = longitude;
    }
}

因为我们不希望我们的集合允许将相同的值添加两次 List<T>也不是最佳选择。

我们可以从使用 HashSet<T> 中受益它具有高性能的集合操作并且不包含重复元素:

HashSet<Coordinate> WayPoints = new HashSet<Coordinate>();

public Form1()
{
    InitializeComponent();
}

...

WayPoints.Add(new Coordinate(.. , ..));

现在如果我们需要 WayPoints即使在表单关闭后仍然存在,我们必须将它移到 Form 之外进入其他一些对象。

如果WayPoints集合应该在我们重新启动应用程序后具有值 - 需要使用持久存储,我们可以将我们的值序列化并保存到数据库或文件中。

一个非常简单的静态存储的例子:

public static class DataStorage
{
    public static HashSet<Coordinate> WayPoints { get; }

    static DataStorage()
    {
        WayPoints = new HashSet<Coordinate>();
    }

    public static Coordinate? TryGetCoordinate(string latitude, string longitude)
    {
        var coordinate = new Coordinate(latitude, longitude);
        return WayPoints.Contains(coordinate) ? (Coordinate?)coordinate : null;
    } 
}

附言

我们可以遍历所有 WayPoints使用 foreach并将每个坐标分配给一些 Textbox.Text .

以防我们需要确定Coordinate来自 WayPoints我们需要做的就是创建 Coordinate 的新实例并检查它是否存在于 WayPoints 中收藏。我们在方法 TryGetCoordinate 中实现了它.

像这样使用它:

Coordinate? foundInStorage = DataStorage.TryGetCoordinate("123.5", "300");
if(foundInStorage != null)
{
    // something with foundInStorage.Value
}

关于c# - 将值放入字典并在文本框中显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50664495/

相关文章:

c# - Log4net 滚动每日文件名,文件名中包含日期

c# - Xamarin.iOS 与第三方框架绑定(bind)

python - 在Python中用列表列表压缩列表

java - 数据结构与算法实现-字典

c++ - 尝试显示我的 Graph 类,STL 映射迭代器有问题

c# - 如何使 Entity Framework 6 (DB First) 显式插入 Guid/UniqueIdentifier 主键?

c# - 按下按钮后的表单 "lags"

asp.net - 无法获取asp.net中只读文本框的值

asp.net - ASP :textbox array

javascript - 如何检测文本框的内容已更改