-1

In a Windows form applications I want to get a wordy (500 word) output into a window/mini window (not just into a textbox control like in tools). For example:

I am trying to execute the output of pkg.pkgs*. This could generate a huge output in my windows form application. Please give me an idea on it. I don't want messagebox.show().

d219
  • 2,707
  • 5
  • 31
  • 36
  • 5
    I think the best approach is to create a very simple form with a read only text area on it and an ok/close button. and then use that instead of a messageBox. see http://stackoverflow.com/questions/16463599/popup-window-in-winform-c-sharp – meganaut Dec 02 '16 at 05:37
  • If the message is too long for `MessageBox`, create your own dialog. A dialog is a `Form` which you show using `ShowDialog`. Also you can put some buttons on the form and set `DialogResult` for them. Then they will close the form returning the `DialogResult` which you set for them. Just don't forget to use the form in a `using` block. – Reza Aghaei Dec 02 '16 at 05:37

2 Answers2

1

You can create a overload constructor to your window and pass the wordy into that.

 public class Window1 : Form
{
    public Window1(string wordy) 
    {
        textbox.text = wordy;
    }
}

You can call the window as

Form wi = new Window1(message);
wi.showdialog();
Anurag
  • 557
  • 6
  • 14
0

In case you can't create your own MessageBox with text area, I suggest trimming the wordy text. We can try two modes:

  • Trim on white space, e.g. My favorite wordy text is it into My favorite... in order to spare words

  • However, it's not always possible/reasonable. If we want, say, just 10 symbols in My favorite wordy text is it we'd rather not left My... but My favorit...

As a criterium for modes switching, let use a rule of thumb that the text trimmed should be at least of 2/3 of maximum possible length. Implementation:

public static string TrimWordyText(string source, int totalLength = 200) {
  if (string.IsNullOrEmpty(source))
    return source;
  else if (source.Length <= totalLength)
    return source;

  // let's try trimming on white space (sparing mode)
  int index = 0;

  for (int i = 0; i <= totalLength; ++i) {
    char ch = source[i];

    if (char.IsWhiteSpace(ch))
      index = i;
  }

  // can we save at least 2/3 of text by splitting on white space 
  if (index > totalLength * 2 / 3)
    return source.Substring(0, index) + "...";
  else
    return source.Substring(0, totalLength) + "...";
}

....

// "My favorite..." 
myTextBox.Text = TrimWordyText("My favorite wordy text is it", 15); 
// "My favorit..." 
myTextBox.Text = TrimWordyText("My favorite wordy text is it", 10); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215