-2

How can I make a graphic in C# when I use drag and drop functionality with string types from a word document? The idea is that I want to make a graphic which represents the length of the words that I should drag and drop.

In the program which I coded, the functionality is dragging and dropping numbers and make a graphic which shows the difference between their values.

For example, I want to drag and drop 'Book, Pen' on a graphic that I already have on my WindowsForms in the moment of starting the program and The Bar Chart reflects first column bigger than the second one. Book-> 4 letters; Pen-> 3 letters

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();
        }
    }        

So this is my problem and i have to modify in some way the part of the code where is that int.Parse(val).

I expect the graphic accept the string types and reflect what I described.

  • VERY unclear. Can you add a sketch of what you want to see? What will get dropped? Selected texts postions? Controls? Items? Word excerpts? What shall the Parse do? get val.Length? – TaW May 25 '19 at 13:00
  • This doesn't answer any of my questions. – TaW May 25 '19 at 13:22
  • pastebin.com/EyvhXwRZ This is my first question and i can t post code. This is the entire code. I want to use drag and drop with the property of the words length. I want to drag the words from a .docx document, on my graph when i run the program and the graph transforms in one which reflects by bar chart the number of words that i dropped and the height represents the words' length. – Ioana Renard May 25 '19 at 13:23

1 Answers1

0

Here's something to get you started:

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 ...

        }
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40