views:

792

answers:

5

Hi,

How to show the text in bold in messagebox.show for c#?
Any suggestions?

Thanks, Karthick

A: 

No can do. You'll have to build your own box. I'm assuming that this is WinForms, if it's ASP.NET I'm not qualified to answer.

bmargulies
Thanks, please suggest, its for winforms.
Karthick
+2  A: 

You can't. This is a wrapper for the API MessageBoxEx.

Create your own custom messagebox to do it.


You can follow this tutorial, as an example of how to implement one.

The basics steps of creating such a form:

  1. Create a new form
  2. Add a label and two buttons
  3. Set the label font to Bold
  4. add handler to both buttons, closing the form and setting some property for which button was pressed.
Am
Thanks, can you suggest some steps for that?
Karthick
He did. The tutorial;.
bmargulies
yup, even added some basics steps as a guide line.
Am
MessageBox is a form that wraps the MessageBoxEx. If you look closely, it is either a modal or modeless dialog box based on a Form. You can create your own class yourself to emulate the same behavior
0A0D
I remember trying to do my own MessageBox() once upon a time in MFC...as I recall, it was exceedingly difficult to *exactly* replicate the standard behaviors. As one example, did you know that you can Ctrl-C the text?
Dan
+6  A: 

It is possible, a message box is a regular window that can be messed with like any other. The code to do so is however a bit gritty. Add a new class to your project and paste this code:

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class BoldMessageBox : IDisposable {
  private int mTries = 0;
  private Form mOwner;
  private Font mFont;

  public BoldMessageBox(Form owner) {
    mOwner = owner;
    owner.BeginInvoke(new MethodInvoker(findDialog));
  }

  private void findDialog() {
    // Enumerate windows to find the message box
    if (mTries < 0) return;
    EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
    if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero)) {
      if (++mTries < 10) mOwner.BeginInvoke(new MethodInvoker(findDialog));
    }
  }
  private bool checkWindow(IntPtr hWnd, IntPtr lp) {
    // Checks if <hWnd> is a dialog
    StringBuilder sb = new StringBuilder(260);
    GetClassName(hWnd, sb, sb.Capacity);
    if (sb.ToString() != "#32770") return true;
    // Got it, get the STATIC control that displays the text
    IntPtr hText = GetDlgItem(hWnd, 0xffff);
    if (hText != IntPtr.Zero) {
      // Get the current font
      IntPtr hFont = SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
      Font font = Font.FromHfont(hFont);
      // And make it bold (note the size change to keep enough space!!)
      mFont = new Font(font.FontFamily, font.SizeInPoints - 1f, FontStyle.Bold);
      SendMessage(hText, WM_SETFONT, mFont.ToHfont(), (IntPtr)1);
    }
    // Done
    return false;
  }
  public void Dispose() {
    mTries = -1;
    mOwner = null;
    if (mFont != null) mFont.Dispose();
  }

  // P/Invoke declarations
  private const int WM_SETFONT = 0x30;
  private const int WM_GETFONT = 0x31;
  private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
  [DllImport("user32.dll")]
  private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
  [DllImport("kernel32.dll")]
  private static extern int GetCurrentThreadId();
  [DllImport("user32.dll")]
  private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
  [DllImport("user32.dll")]
  private static extern IntPtr GetDlgItem(IntPtr hWnd, int item);
  [DllImport("user32.dll")]
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

And use it like this:

private void button1_Click(object sender, EventArgs e) {
  using (new BoldMessageBox(this)) {
    MessageBox.Show("Nobugz waz here");
  }
}

There is one flaw in this approach. After making the font bold, the text must still fit in the static control that the message box reserved for the text. That required me to make the font smaller. You may have to tweak this value.

Hans Passant
A: 

Extended MessageBox .NET Assembly XMSG .NET web page: more info, download

Adjusts on the fly wide variety of MessageBox visual settings.

Adjustable features include message font and color, button captions, fonts and tooltips, dialog background, dialog position, dialog icon, timeout and more. Depending on message font selected, the dialog window automatically resizes itself to accommodate the message.

Additional controls that can be optionally displayed: check box, text input, web link, up to 3 extra buttons.

In your .NET code you still call regular MessageBox.Show. Extended MessageBox is not a custom-made dialog. This is still regular MessageBox with extended features added.

OS supported: XP, 2000, 2003, 2008 Vista, Win7 -- 32 or 64-bit.

Downloads include fully-functional trial version, and regular version with complete C# source code.

Anatoliy Mogylevets
A: 

The XMSG product in the previous answer is cool, but it doesn't solve the problem that I created my HTML MessageBox product (www.HTML-MessageBox.com) for, which is to add selective emphasis to the text of a longer messagebox, so that for instance one sentence in the text, which is the most important part, could be in boldface.

The HTML MessageBox supports simple limited HTML in the body of the message: fonts, bold, underline, italic, and colour. The idea is to be able to improve the readability and usability of your messageboxes. It doesn't have all the extra features of XMSG; all it does is virtually extactly replace the standard MessageBox API with one that allows you to set a default font for all of your MessageBoxes, then use that simplified HTML where desired in the message text.

The HTML MessageBox has APIs for standard DLL calling, COM, .NET, and PowerBuilder (which is what I originally needed it for). Like the standard MessageBox API, it does things like auto-centering and auto-sizing of the MessageBox dialog, which makes it a whole lot easier to use than rolling your own each time!

Dan Cooperstock
Maybe your MessageBox is good, but the price is very high for simple message box. Take a look at http://www.nevron.com/Gallery.UserInterfaceFor.NET.TaskDialogsGallery.aspx. This i can already get for 370$ (yours for 150$) and i got a **lot** of other UI elements!
Oliver