Chaps,
My sole use for VBA was to provide a very pretty dialogue box in which to enter information which is then passed to a LISP routine via a text file.
This has worked fine for many versions of AutoCAD, but since upgrading to 2015 from 2014 (on the same 64-bit laptop) this hasn't worked. Looks like I would need to re-write part or all of my programs so I've decided to bin VBA and switch to, arbitrarily, C#.
A very big ask, but could some kind soul start me off? I'd like a "UserForm" with two "TextBoxes". The first box is filled with the first line of D:\Test-In.txt and the contents of the second is written to D:\Test-Out.txt when the [OK] button is pressed.
Is this possible?
S
C# is definitely the preferred long term skill, however you may consider a "quick fix" and get into the .NET family via VB.NET. I don't have the link to hand, but google:
AutoCAD VBA to VB.NET Migration Basics by Augusto Goncalves ADN
I think it is an ADN DevTV video and material.
Below is sample to write a text file in C#:
Dale
<code>
// from MSDN sample
// DJB overloaded version - for single text string
public void WriteTextFile(string pstrFileNameFull, bool blnAppend, string pstrText)
{
if (blnAppend == true)
{
// Example #4: Append new text to an existing file
using (System.IO.StreamWriter file = new System.IO.StreamWriter(pstrFileNameFull, true))
{
file.WriteLine(pstrText);
}
}
else
{
// Example #2: Write one string to a text file.
System.IO.
File.WriteAllText(pstrFileNameFull, pstrText);
}
}
<code>
Since you're starting off, definitely read up on Windows Forms applications using MSDN or other sources. I'm not where I can look, but maybe someone will throw out a few references. I'm making a small assumption that you know how to set this up, but just in case here's a brief set of steps.
Jerry Winters has a nice book too on programming with AutoCAD that's really good. It uses VB, but that can easily be translated to C#. (http://www.vbcad.com/)
Since the application will be started via an AutoCAD command, here's the command code:
[assembly:CommandClass(typeof(Commands))] namespace AutoCADApp2 { public class Commands { [CommandMethod("RunWindowsApp")] public void RunWindowsApp() { MyUserForm form = new MyUserForm(); form.Show(); } } }
Here's the form code behind... it's super generic using poor programming practices, but hopefully will help you get kickstarted in the right direction.
public partial class MyUserForm : Form { public MyUserForm() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { //poor coding practices but should hopefully get the point across... //needs processes separated, better exception handling, etc... //Read Text-In and update textbox FileInfo fileTextIn = new FileInfo(@"C:\acad\Text-In.txt"); if (!fileTextIn.Exists) MessageBox.Show("File Text-In.txt does not exist."); using (StreamReader reader = fileTextIn.OpenText()) { //read first line string line = reader.ReadLine(); //if no data in the file it will return null... therefore if (line != null) textBox1.Text = line; } //Now write to the Text-Out.txt FileInfo fileTextOut = new FileInfo(@"c:\acad\Text-Out.txt"); //Create if not already there... //There should be more elegant ways to handle this, but hopefully this is straight forward if (!fileTextOut.Exists) { fileTextOut.Create(); fileTextOut.Refresh(); //since the file was just created, refresh state of FileInfo object } using (StreamWriter writer = fileTextOut.AppendText()) { if (!string.IsNullOrEmpty(textBox2.Text)) writer.WriteLine(textBox2.Text); } } }