Revit API provides a TaskDialog class for programmers to call when necessary to show some messages to users. It is kind of replacement for the Windows MessageBox API but more powerful and Revit look.
However, none provides a similar and simple means to help collect data input, like such:
In this post, we are going to create such a small tiny creature with C#.
Most people may think of deriving from the Windows Form class to achieve the goal. It certainly works but has a lot of overheads as we know, for example, resources, designers, classes, behind-scene code, events handling, form driving, and, most important of all, synchronization work among all these. It may just scare API amateurs away.
Ok, here is another simple and functional and green idea, creating an instance of the Form itself, settings its properties to meet the need, creating an even handler, and attaching it to the form on the fly.
Here we go!
public static bool CollectDataInput(string title, out int ret)
{
Form dc = new Form();
dc.Text = title;
dc.HelpButton = dc.MinimizeBox = dc.MaximizeBox = false;
dc.ShowIcon = dc.ShowInTaskbar = false;
dc.TopMost = true;
dc.Height = 100;
dc.Width = 300;
dc.MinimumSize = new Size(dc.Width, dc.Height);
int margin = 5;
Size size = dc.ClientSize;
TextBox tb = new TextBox();
tb.TextAlign = HorizontalAlignment.Right;
tb.Height = 20;
tb.Width = size.Width - 2 * margin;
tb.Location = new Point(margin, margin);
tb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
dc.Controls.Add(tb);
Button ok = new Button();
ok.Text = "Ok";
ok.Click += new EventHandler(ok_Click);
ok.Height = 23;
ok.Width = 75;
ok.Location = new Point(size.Width / 2 - ok.Width / 2, size.Height / 2);
ok.Anchor = AnchorStyles.Bottom;
dc.Controls.Add(ok);
dc.AcceptButton = ok;
dc.ShowDialog();
return int.TryParse(tb.Text, out ret);
}
private static void ok_Click(object sender, EventArgs e)
{
Form form = (sender as Control).Parent as Form;
form.DialogResult = DialogResult.OK;
form.Close();
}
So WYSIWYG is not always good as it bears a lot of cost and sometimes is not really easy to use. We will not talk about coding details here, but it may be interesting to describe a bit the behaviour of the simple data collector.
• It is resizable. When the whole from is resized the TextBox and the OK button will be automatically adjusted as well and still look good.
• Pressing the ENTER key will close the dialog just as we click the OK button.
• It stays on top of all other windows.
• It does not have any redundant stuff like the normal Form does, e.g. minimum box, maximum box, and the default icon.
In case of further information is needed, please check on the MSDN documentations. It is very easy to use, as the following code demonstrates:
…
int data;
bool ret = CollectDataInput("Please input an integer:", out data);
MessageBox.Show(string.Format("Successful: {0}\nData: {1}", ret, data));
…
The Data Collector coder of RevitAddinWizard creates some code automatically to help collect various types of data input like integer we talked about here, double, string, and more.
Related posts:
Recent Comments