Author Topic: Application.ShowAlertDialog() with bold or underline or..  (Read 4477 times)

0 Members and 1 Guest are viewing this topic.

teslaxx

  • Guest
Application.ShowAlertDialog() with bold or underline or..
« on: December 06, 2010, 02:15:47 AM »
Can I set the a part of the text which appear in Application.ShowAlertDialog() to be bold.
Something like this:
Quote
Mary has two lambs.
« Last Edit: December 06, 2010, 02:36:39 AM by teslaxx »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #1 on: December 06, 2010, 03:38:52 AM »

No, not that I'm aware of.

But you could use a RichTextBox on a custom built form ( may need to prohibit editing so it looks like an alert message.

http://msdn.microsoft.com/en-us/library/aa287595(VS.71).aspx
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #2 on: December 06, 2010, 09:57:09 AM »
Or simply override the OnPaint() or handle Paint event to direct draw the text with desired font in desired color/weight/size, or even your own icon/image, onto the form (or a user control). If you do it carefully from beginning, you would soon realize that you are going to have your own custom generic message/alerting box built.



No, not that I'm aware of.

But you could use a RichTextBox on a custom built form ( may need to prohibit editing so it looks like an alert message.

http://msdn.microsoft.com/en-us/library/aa287595(VS.71).aspx

teslaxx

  • Guest
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #3 on: December 07, 2010, 03:25:17 AM »
Could you post a basic example?
 I'm pretty new to programming and I don't know how to manage what you suggested.

kaefer

  • Guest
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #4 on: December 07, 2010, 04:37:08 AM »
Could you post a basic example?
 I'm pretty new to programming and I don't know how to manage what you suggested.

The following F# wouldn't help you then, but it does show the basic principle:
Code: [Select]
type Label1() =
    inherit System.Windows.Forms.Label()
   
    let regularFont = new System.Drawing.Font("Helvetica", 8.f, System.Drawing.FontStyle.Regular)
    let boldFont = new System.Drawing.Font("Helvetica", 8.f, System.Drawing.FontStyle.Bold)

    override me.OnPaint e =
        me.Width <-
            me.Text.Split 'x'
            |> Array.fold
                (fun (isBold, pos) t ->
                    let font = if isBold then boldFont else regularFont
                    let newpos =
                        new System.Drawing.PointF(
                            float32 me.Location.X + pos, float32 me.Location.Y )
                    e.Graphics.DrawString(
                        t, font, System.Drawing.Brushes.Black, newpos )
                    not isBold, pos + e.Graphics.MeasureString(t, font).Width )
                (false, 0.f)
            |> snd
            |> int

let f = new System.Windows.Forms.Form()
let l = new Label1(Text = "Maryx hasx twox Lambsx.")
f.Controls.Add l

[<System.STAThread>]
#if INTERACTIVE
    f.Show()
#else
    Application.Run(f)
#endif

The bold format is applied by overriding the OnPaint method, one could attach to the Paint event instead. And you have to handle the running widths of the partial strings yourself.



Cheers, Thorsten

teslaxx

  • Guest
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #5 on: December 07, 2010, 05:34:43 AM »
Where should I paste this code in my CSharp project and how can I call the function from Autocad? :)

kaefer

  • Guest
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #6 on: December 07, 2010, 08:01:57 AM »
Where should I paste this code in my CSharp project and how can I call the function from Autocad? :)

You shouldn't. It's a standalone F# script, which can be executed in the F# Interactive window and which I like therefore for prototyping.

Since Kerry and Norman did already provide the necessary starters for you, just dig into the docs and learn how to build a custom message box.
Try this http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx for example and remember to use the Application.ShowModalDialog Method to show your form from AutoCAD.

Good luck, Thorsten

n.yuan

  • Bull Frog
  • Posts: 348
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #7 on: December 08, 2010, 04:00:49 PM »
Well, overriding OnPaint() of in Win Form is fairly simple, using WFP window might be another option, if you use Acad2010 or later (even you are not now, you'll certainly go to it one day anyway). I did a quick sample out of it.

Here is the WPF window's XAML code (In acad DLL project, VS2008/2010 only let you add a WPF UserControl. Go ahead to add a WPF UserControl, and then change the "UserControl" tag to "Window" and change the the code behind class file to let it inherit from System.Windows.Window, instead of System.Windows.UserControl):

Code: [Select]
<Window x:Class="MyMessageBox.MsgBoxView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="244"
        d:DesignWidth="397"
        SizeToContent="WidthAndHeight"
        ResizeMode="CanResize"
        WindowStartupLocation="CenterOwner"
        ShowInTaskbar="False"
        WindowStyle="ToolWindow"
        Title="My Message Box">
   
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*" />
            <RowDefinition Height="98*" />
            <RowDefinition Height="57*" />
        </Grid.RowDefinitions>
        <Button Content="OK" Grid.Row="2" Height="23"
                HorizontalAlignment="Left" Margin="281,9,0,0"
                Name="button2" VerticalAlignment="Top"
                Width="75" Click="button2_Click" />
        <StackPanel Grid.Row="1" Name="msgPanel"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    Orientation="Horizontal"/>
        <StackPanel Name="stackPanel1">
            <TextBlock FontSize="20"
                       FontWeight="ExtraBold"
                       Foreground="#FF1313D8"
                       Text="Title of the message" />
        </StackPanel>
    </Grid>
</Window>

Here is the MsgBoxView class (WPF Window) code behind:

Code: [Select]
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyMessageBox
{
    /// <summary>
    /// Interaction logic for MsgBoxView.xaml
    /// </summary>
    public partial class MsgBoxView : Window
    {
        public MsgBoxView()
        {
            InitializeComponent();
        }

        public void AddMessage(string[] msgStrings)
        {
            msgPanel.Children.Clear();

            TextBlock txt;

            this.DataContext = msgStrings.ToList<string>();

            int i = 1;
            Brush br = Brushes.Black;

            foreach (string s in msgStrings)
            {
                txt = new TextBlock();
                txt.SetValue(TextBlock.TextProperty, s);

                double fSize = 10.0;

                switch (i)
                {
                    case 1:
                        fSize = 16.0;
                        br = Brushes.Red;
                        break;
                    case 2:
                        fSize = 14.0;
                        br = Brushes.Blue;
                        break;
                    case 3:
                        fSize = 12.0;
                        br = Brushes.Green;
                        break;
                }

                i++;

                txt.SetValue(TextBlock.FontFamilyProperty, new FontFamily("Arial"));
                txt.SetValue(TextBlock.FontSizeProperty, fSize);
                txt.SetValue(TextBlock.ForegroundProperty, br);
                txt.SetValue(TextBlock.MarginProperty, new Thickness(0.0, 0.0, 2.0, 0.0));
               
               
                msgPanel.Children.Add(txt);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

Then in the Acad Dll project, I added a class "MyMessageBox":

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;

namespace MyMessageBox
{
    public class MyMessageBox
    {
        private static MsgBoxView _view = null;

 
        public static void ShowMyMessageBox(string[] messageStrings)
        {
            _view = new MsgBoxView();
            _view.AddMessage(messageStrings);

            Application.ShowModalWindow(_view);
            _view = null;
        }
    }
}

And finally, I added a command class in the Acad DLL project:

Code: [Select]
using Autodesk.AutoCAD.Runtime;

namespace MyMessageBox
{
    public class MyCommands
    {
        [CommandMethod("MyMsg")]
        public static void ShowMyMessageBox()
        {
            MyMessageBox.ShowMyMessageBox(
                new string[]
                {
                    "Welcome",
                    "to",
                    "My",
                    "Message",
                    "Box"
                });
        }
    }
}

As you can see, with WPF window/UserControl, you can easily control text label (TextBlock in WPF) with its font, color, sizeand/or other properties...

This is just showing a concept that how much  and how easy control you have to the things showing on a WPF window. Attached in the image of the message box created by the code shown here.



Where should I paste this code in my CSharp project and how can I call the function from Autocad? :)

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #8 on: December 08, 2010, 08:08:20 PM »
Wow

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #9 on: December 08, 2010, 09:08:45 PM »

Quote
As you can see, with WPF window/UserControl, you can easily control text label (TextBlock in WPF) with its font, color, sizeand/or other properties...


Very interesting Norman .. Thanks for posting your solution :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

kaefer

  • Guest
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #10 on: December 09, 2010, 04:32:38 AM »
Code: [Select]
            Application.ShowModalWindow(_view);

This is great.

One tiny addendum: To reinforce the pedagogical approach we may stress that all kinds of MessageBoxen should return a DialogResult, if only to ignore it afterwards. In case of WPF this comes as System.Nullable<bool>, which might be set by the OK button's Click handler.

Greetings, Thorsten
(Completely unrelated rant: I don't like inheritance through XAML and the practice of code behind, because those are the two concepts of WPF that do not port to F#)

n.yuan

  • Bull Frog
  • Posts: 348
Re: Application.ShowAlertDialog() with bold or underline or..
« Reply #11 on: December 09, 2010, 10:03:08 AM »
Yes, I completely agree that the ShowMessageBox() method should return a value (Nullable<bool>). I was focus on how to make text being shown in different font/size/color... in a rush.

Granted, using code behind for WPF window in this case is just one way to do things, not necessarily the best way. We can certainly avoid it and only use code to build a data view model and bind it to XAML window definition. For example, in the XAML, adding 10 (more or less) blank TextBlock (or TextBlock.Text=" "). Set their properties you want to configure to a binding path, which is the view model's properties. Then the Window is done. No code at all, or at most, one line code to set the Window's binding DataContext to the passed in view data model in an public method.

Now the work is completely outside the Window: create a view data model, something like:

Code: [Select]
class MsgBoxViewModel
{
public string Text01_Text { get; set; }
public string Text01_FontFamily { get; set; }
public doulbe Text02_Size { get; set;}
...
public string Text10_Text { get; set; }
public string Text10_FontFamily { get; set; }
public double Text10_Size { get; set; }

public MsgBoxViewModel(){}
public MsgBoxViewModel(string[] text, string[] fontFamily, double[] txetSize, ...){}
}

Now to use the messagebox, one simply pass the desired settings to the view model.

Obviously, we can build a completely configurable custom MessageBox with great control to the presenting text message/icon/button

Code: [Select]
            Application.ShowModalWindow(_view);

This is great.

One tiny addendum: To reinforce the pedagogical approach we may stress that all kinds of MessageBoxen should return a DialogResult, if only to ignore it afterwards. In case of WPF this comes as System.Nullable<bool>, which might be set by the OK button's Click handler.

Greetings, Thorsten
(Completely unrelated rant: I don't like inheritance through XAML and the practice of code behind, because those are the two concepts of WPF that do not port to F#)