.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Passing data between classes

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
676 Views, 9 Replies

Passing data between classes

I have a command class and a form1 class associated with a form. The form
includes several textboxes. I activate the form from the command class and
enter text into the textboxes. I can then access the textbox strings within
the form1 class, but how do I access the textbox values from the command
class?
9 REPLIES 9
Message 2 of 10
AdammReilly
in reply to: Anonymous

From within the command class, when you can use form1.[textbox1].Text
[textbox1] being whatever you named the textbox

Adam Reilly

Dell Precision 55400
Windows 10 Pro
Intel Xeon E-2276M 2.80GHz, 64bit
NVidia Quadro T2000 Mobile/Max-Q


LinkedIn | Twitter


Group Leader badge


Austin CADD User Group


LinkedIn | Discord

Message 3 of 10
Hallex
in reply to: Anonymous

I would do by this way

Declare second form as class with arguments

See attached sample

~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 10
Anonymous
in reply to: Anonymous

Thanks for your insight.

That's was what I thought. But I get an error that "Reference to a
non-shared member requires an object reference". I'm sure I am missing
something simple but I can;t nail it!

wrote in message news:6318347@discussion.autodesk.com...
From within the command class, when you can use form1.[textbox1].Text
[textbox1] being whatever you named the textbox
Message 5 of 10
_gile
in reply to: Anonymous

Hi,

isn't your TextBox 'private' ? If so, set it 'internal'.


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 10
chiefbraincloud
in reply to: Anonymous

you have a form. For sake of argument call it form1, with TextBox Tb1
dim txtVal as string
dim ThisForm1 as New form1
Application.ShowModalDialog(ThisForm1)

'Make sure you just hide the form in the closing event, dispose of it in the calling method
txtVal = ThisForm1.Tb1.Text
ThisForm1.Close
ThisForm1.Dispose

Doing it this way gives you the Object Reference that your error message is griping about.

(BTW, if you want a form to persist its data across multiple documents, you must declare the ThisForm1 as Shared, at the Class Level)
Dave O.                                                                  Sig-Logos32.png
Message 7 of 10
Anonymous
in reply to: Anonymous

The best way to deal with that problem you have is to use data binding.

When you use data binding, you define a class that serves as a container for the
data you want to show/edit, and just tell your form that you want to use the
controls on it to display/edit that data.

You can see an example of how that works (in C#), in this sample:

http://www.caddzone.com/DataBindingSample.zip

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6317990@discussion.autodesk.com...
I have a command class and a form1 class associated with a form. The form
includes several textboxes. I activate the form from the command class and
enter text into the textboxes. I can then access the textbox strings within
the form1 class, but how do I access the textbox values from the command
class?
Message 8 of 10
Anonymous
in reply to: Anonymous

I knew it had to be a simple solution. I added your suggested code and it
came up instantly! Now 10 more commands and 10 more forms to go.

Thanks for your help.

wrote in message news:6318516@discussion.autodesk.com...
you have a form. For sake of argument call it form1, with TextBox Tb1
dim txtVal as string
dim ThisForm1 as New form1
Application.ShowModalDialog(ThisForm1)

'Make sure you just hide the form in the closing event, dispose of it in the
calling method
txtVal = ThisForm1.Tb1.Text
ThisForm1.Close
ThisForm1.Dispose

Doing it this way gives you the Object Reference that your error message is
griping about.

(BTW, if you want a form to persist its data across multiple documents, you
must declare the ThisForm1 as Shared, at the Class Level)
Message 9 of 10
mgorecki
in reply to: Anonymous

Ok, so lets say you have a form (2 textboxes and a "Start" button) and a command class.  When I hit the Start button, how does the form go back to the command class to execute the functions there?  I've set it up like you describe, but I'm not sure how to make it jump from the form to the command class.  Maybe a silly question, but I'm kinda new to .Net and appreciate the help.

 

Mark

Message 10 of 10
norman.yuan
in reply to: mgorecki

Here is some pseudo code:

 

public class MyCommandClass
{
...
[CommandMethod("DoSomething")]
public static void DoWork()
{
  string txt1=null;
  string txt2=null;

  //Show form to get user input
  using (Form1 frm=new Form1())
  {
    //The form is shown and the command is held until
    //the form is dismissed
    DialogResult res=Autodesk.AutoCAD.ApplicationServices.
            Application.ShowModalDialog(frm);
    //Only if the user clicks "OK/Start"
    //The user input is obtained
    if (res=DialogResult.OK)
    {
      txt1=frm.TextValue1;
      txt2=frm.TextValue2;
    }
  }

  if (txt1!=null && txt2!=null)
  {
    //Do your work with the user input acquired with the form
  }
}
...
}

public class Form1:System.Windows.Forms.Form
{
  //Assume the form has 2 text boxes and 
  //a button "Start" and a button "Cancel"

  //You add 2 public read-only properties to the form
  public string TextValue1
  {
    get { return TextBox1.Text; }
  }

  public string TextValue2
  {
    get { return TextBox2.Text; }
  }

  //You set "Start" button's DialogResult property to "OK"
  //Set "Cancel" button's DialogResult property to "Cancel"
  //So, when clicking on the buttons, the form will closed (Hidden),
  //No code is needed
}

 Well, you of course would do some inout validation beofre the form turns hidden.

 

You might wan to learn more on .NET basics on windows forms, modelss form and modal form.

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost