【问题标题】:Drag and drop words into a graph WindowsForms将单词拖放到图形中 WindowsForms
【发布时间】:2019-05-25 12:23:02
【问题描述】:

当我对 word 文档中的字符串类型使用拖放功能时,如何在 C# 中制作图形?这个想法是我想制作一个图形来表示我应该拖放的单词的长度。

在我编写的程序中,功能是拖放数字并制作一个显示它们值之间差异的图形。

例如,我想在启动程序时将“Book, Pen”拖放到我的 WindowsForms 上已有的图形上,并且条形图反映的第一列大于第二列。书-> 4 个字母;笔-> 3个字母

public class Grafic: Control
{
    int[] valori;

    private void Grafic_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(string)))
        {
            string valoriString =
                (string)e.Data.GetData(typeof(string));
            //MessageBox.Show(valoriString);

            Valori = valoriString
                    .Split(',')
                    .Select(val => int.Parse(val))
                    .ToArray();
        }
    }        

所以这是我的问题,我必须以某种方式修改 int.Parse(val) 所在的代码部分。

我希望图形接受字符串类型并反映我所描述的内容。

【问题讨论】:

  • 非常不清楚。你能添加一个你想看的草图吗?什么会被丢弃?选定的文本职位?控制?项目?单词摘录? Parse 会做什么?得到 val.Length?
  • 这不能回答我的任何问题。
  • pastebin.com/EyvhXwRZ 这是我的第一个问题,我无法发布代码。这是整个代码。我想将拖放与单词长度的属性一起使用。当我运行程序时,我想将 .docx 文档中的单词拖到我的图表上,并且图表会转换为一个,它通过条形图反映我删除的单词数,高度代表单词的长度。

标签: c# visual-studio drag-and-drop windows-forms-designer


【解决方案1】:

以下是帮助您入门的内容:

public partial class Grafic : Control
{

    private SortedDictionary<int, int> WordLengthCounts = new SortedDictionary<int, int>();

    public Grafic()
    {
        InitializeComponent();
        this.DragEnter += Grafic_DragEnter;
        this.DragDrop += Grafic_DragDrop;
        this.Paint += Grafic_Paint;
    }

    private void Grafic_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = e.Data.GetDataPresent(typeof(string)) ? DragDropEffects.All : DragDropEffects.None;
    }

    private void Grafic_DragDrop(object sender, DragEventArgs e)
    {
        string input = (string)e.Data.GetData(typeof(string));
        // Regex by TheCodeKing: https://stackoverflow.com/a/7311811/2330053
        var matches = System.Text.RegularExpressions.Regex.Matches(input, @"((\b[^\s]+\b)((?<=\.\w).)?)");
        foreach (var match in matches)
        {
            int wordLength = match.ToString().Length;
            if(!WordLengthCounts.ContainsKey(wordLength))
            {
                WordLengthCounts.Add(wordLength, 1);
            }
            else
            {
                WordLengthCounts[wordLength]++;
            }
        }
        this.Invalidate();
    }

    private void Grafic_Paint(object sender, PaintEventArgs e)
    {
        Graphics G = e.Graphics;
        foreach(var wordLengthCount in WordLengthCounts)
        {
            Console.WriteLine("Length: " + wordLengthCount.Key.ToString() + ", Count: " + wordLengthCount.Value.ToString());

            // ... put in your drawing code to visualize this data ...

        }
    }

}

【讨论】:

  • 让我知道您是否需要有关该绘图代码的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
相关资源
最近更新 更多