Path from label.Text C#

Path from label.Text C#

k005
Advisor Advisor
2,007 Views
14 Replies
Message 1 of 15

Path from label.Text C#

k005
Advisor
Advisor

Hello

in the following codes

 

How can I get path from label.Text in Form1?

 

For example;

 

var path = @Form1.label1.Text;  ???

 

 

 

[CommandMethod("OPSV")] 
        public  void KaydetToplu ()
       {
        try
            {
                
                Stopwatch watch = new Stopwatch();
                
                
                watch.Start();
               
                var path = @"C:\TestBack\";
                DirectoryInfo d = new DirectoryInfo(path);

 

 

 

0 Likes
Accepted solutions (1)
2,008 Views
14 Replies
Replies (14)
Message 2 of 15

HJohn1
Advocate
Advocate

I am really not sure what you are asking for, but if what you need is to access a label control on a form from a class then first on the form change the access modifier of the label control to public, then create an instance of the form class to access the label control.

private Form1 frmOne = new [ProjectName].Form1();
frmOne.label1.Text = path;

 

0 Likes
Message 3 of 15

_gile
Consultant
Consultant

Hi,

Rather than changing the access modifier of the controls, a better practice is to change the form constructor so that it accepts arguments and/or use a public properties in the form class.
You can find an example in the Modal dialog box section of this tutorial.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 15

k005
Advisor
Advisor

@HJohn1 

 

Where do I write this code?

 

I made a few tries, it didn't work ..

0 Likes
Message 5 of 15

k005
Advisor
Advisor

@_gile 

 

I am trying to access the objects (label, textbox etc.) in Form1 in Mycommands.cs.

 

how can I do that ?

0 Likes
Message 6 of 15

_gile
Consultant
Consultant
Accepted solution

To pass values from the command to the dialog box, you can use the parameters of the constructor of the form (you will overload it accordingly).

And to pass the values specified by the user in the dialog box to the command, you can define properties in the class of the form whose values will be accessible via the instance of the form created in the command.

This is some basical OOP practice you should learn before trying to program AutoCAD.

 

A trivial example with some comments.

The DialogBox class:

using System.Windows.Forms;

namespace SimpleDialogSample
{
    /// <summary>
    /// Describes a dialog box which only contains:
    /// - a Label (label1)
    /// - a Textbox (textBox1)
    /// - a OK button (buttonOK with DialogResult = OK)
    /// - a Cancle button (buttonCancle with DialogResult = Cancel)
    /// </summary>
    public partial class DialogBox : Form
    {
        /// <summary>
        /// Creates a new instance of DialogBox.
        /// </summary>
        /// <param name="label">Value of the label to display.</param>
        public DialogBox(string label)
        {
            InitializeComponent();
            // Assign the 'label' parameter value to label1.
            label1.Text = label;
        }

        /// <summary>
        /// Gets the textBox1.Text value.
        /// </summary>
        public string Content
        {
            get { return textBox1.Text; }
        }
    }
}

The Command class:

using Autodesk.AutoCAD.Runtime;

using System.Windows.Forms;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace SimpleDialogSample
{
    public class Command
    {
        [CommandMethod("TEST")]
        public static void Test()
        {
            // Creates a nes instance of DialogBox
            using (var dialog = new DialogBox("Some text from the TEST CommandMethod"))
            {
                // Shows the dialog box and gets its DialogResult (i.e. the way it has been closed)
                if (AcAp.ShowModalDialog(dialog) == DialogResult.OK)
                {
                    // Shows the content which were entered in the dialog text box.
                    AcAp.ShowAlertDialog(
                        "The content of the dialog box TextBox is: " + dialog.Content);
                }
                else
                {
                    AcAp.ShowAlertDialog("Cancellled");
                }
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 15

k005
Advisor
Advisor

@_gile 

 

Thank you so much. Thanks to you, I accessed the Label.

 

Yes I'm new to C # ... trying to understand the build. I have shortcomings. I am aware.

 

Thank you so much.

 

🤗

0 Likes
Message 8 of 15

k005
Advisor
Advisor

Hello

 

Let me continue with the same topic;

 

I print the path textbox1 with the button on Form1.

but I can't call this path from Mycommand.cs. ?

More precisely, the message is blank. where am I doing wrong?

 

 

 

//Form1 in

 

 private void btnYol_Click(object sender, EventArgs e)// Yol
        {

            var openFileDialog1 = new FolderBrowserDialog();
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {

                lblYol.Text = openFileDialog1.SelectedPath;
               
                textBox1.Text = openFileDialog1.SelectedPath.ToString();
            }
        }

 

 

 

//Form1 in

 public string Yol1
        {
            get { return textBox1.Text; }
         
        }

 

 

 

 

//mycommand.cs

 

 [CommandMethod("Toplu2010kaydet", CommandFlags.Session)]
        public void KaydetToplu2()
        {

            Form1 frm = new Form1();
            Stopwatch watch = new Stopwatch();
        
            // var slh = @"\";

          
         Application.ShowAlertDialog(frm.Yol1);
////

 

 

0 Likes
Message 9 of 15

_gile
Consultant
Consultant

You did not attentively read the code i provided. To show a dialog box from AutoCAD you have to use the ShowModalDialog method.

using (form1 frm = new Form1())
{
    DialogResult result = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm);
    // ...
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 15

k005
Advisor
Advisor

@_gile 

 

Where will this code you send be written?

 

To Form1.cs?
To Mycommand.cs?

 

I open form1 like this, I'm triggering ..

 

[assembly: CommandClass(typeof(KalayCAD.Mycommands))]

namespace KalayCAD
{
    
    public class Mycommands
    {
     
       [CommandMethod("qwe")]// 1.Komut Form1 Açılır... Tetikleyici
        public void FormuAc ()
        {
        Form1 frm = new Form1();
        frm.Show();
        }
         

////

 

Is this wrong? I did not know.

0 Likes
Message 11 of 15

_gile
Consultant
Consultant

Please read the link I provided for the ShowModalDialog method and the example in the Modal dialog box section of this tutorial.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 12 of 15

k005
Advisor
Advisor

 

I have looked at these examples before. But it seemed a bit complicated to me.

 

I could not fully understand. anyway I guess I'll look at it later.

 

Thanks.

0 Likes
Message 13 of 15

_gile
Consultant
Consultant

@k005  a écrit :

But it seemed a bit complicated to me.


Doesn't that mean that the task you are trying to accomplish is "a little complicated for you"?
As I've told you elsewhere, you should learn the basics of .NET/C# outside of AutoCAD, starting with simple tasks before trying to create an AutoCAD plugin with a "sophisticated" dialog box.

By my side, I'm afraid I cannot help you further. My English is not that good to explain this better than i did here and in the little tutorial. Sorry.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 14 of 15

k005
Advisor
Advisor

 

I understand. no problem.

 

I'll do the process you mentioned later as an example ... I think there is a problem at a very small point.

The plug-in status will continue, there is no problem with it. And I can say that it has developed very well right now. You have helped too.

 

Thanks a lot., I will solve.

0 Likes
Message 15 of 15

k005
Advisor
Advisor

@_gile 

 

Hello
I have acquired a book about C #. and the "Path" event
I solved it. With static class ..

Thank you very much.

 

 static class deneme
    {
        public static string yol { get; set; }
    }
0 Likes