HOW TO ADD WINDOWS FORMS

HOW TO ADD WINDOWS FORMS

vjohnson8HQZZ
Contributor Contributor
457 Views
5 Replies
Message 1 of 6

HOW TO ADD WINDOWS FORMS

vjohnson8HQZZ
Contributor
Contributor

Hi All,
I have a plugin .dll file written C# for .NetCore which works well.
Now I wanted to make a UI for it with Windows Forms.
Im struggling to add the reference for it.
Can someone please help me know where I am going wrong.

This is my project code.

---------------------------------------------------------------
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
 
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<OutputType>Library</OutputType>
<RootNamespace>Draw_Polyline</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
 
<ItemGroup>
<PackageReference Include="AutoCAD.NET" Version="25.0.1" />
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
</ItemGroup>
 
</Project>


------------------------------------------------
Code of a simple form

using System.Windows.Forms;


class SimpleForm : Form
{
public SimpleForm()
{
var label = new Label() { Text = "Name:", Location = new Point(10, 20) };
var textBox = new TextBox() { Location = new Point(60, 18), Width = 150 };
var button = new Button() { Text = "Greet", Location = new Point(60, 50) };

button.Click += (s, e) => MessageBox.Show($"Hello, {textBox.Text}!");

Controls.Add(label);
Controls.Add(textBox);
Controls.Add(button);
}

[STAThread]
static void Main()
{
Application.Run(new SimpleForm());
}
}

When I run my command method and try to show the form, no form appears.



0 Likes
Accepted solutions (1)
458 Views
5 Replies
Replies (5)
Message 2 of 6

ActivistInvestor
Mentor
Mentor

What CommandMethod? 

0 Likes
Message 3 of 6

vjohnson8HQZZ
Contributor
Contributor

I mean,
I make a custom command to start the form as below:

----------------------------------------------------------

using Autodesk.AutoCAD.Runtime;

public class Commands
{
[CommandMethod("ShowMyForm")]
public void ShowMyForm()
{
Form1 form = new Form1();
form.ShowDialog();
}
}

0 Likes
Message 4 of 6

ActivistInvestor
Mentor
Mentor

You use Application.ShowModalDialog() to show a Form. 

Message 5 of 6

vjohnson8HQZZ
Contributor
Contributor
Accepted solution

Hi All,
Sorry for the confusion.
There was some other problem. It is solved now. 
This code worked.
 

using Autodesk.AutoCAD.Runtime;

public class Commands
{
[CommandMethod("ShowMyForm")]
public void ShowMyForm()
{
Form1 form = new Form1();
form.ShowDialog();
}
}

0 Likes
Message 6 of 6

_gile
Consultant
Consultant

From the Developer Help:

"Custom dialog boxes or forms in the AutoCAD Managed .NET API extend the classes from the System.Windows.Forms namespace directly. However, such applications should not call the Form.ShowDialog method but instead use the Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog and ShowModelessDialog methods to display their custom forms. Using Form.ShowDialog in an AutoCAD extension application may result in unexpected behavior. "



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub