Access to listbox of form in the main class

Access to listbox of form in the main class

Anonymous
Not applicable
1,129 Views
2 Replies
Message 1 of 3

Access to listbox of form in the main class

Anonymous
Not applicable

Hello ,

I have programmed using c# a form which contains a listbox called "ListOfObjects" , 

i want to access it from the main class called "class1" 

i have made in the "class1" an instance of the form where the class of the form called "mshtry" 

{

mshtry dataff = new mshtry();
dataff.Show();

}

when i try to access of the listbox of the "dataff" , it doesn't appear.

so how can i reach the lisbox ""ListOfObjects" in the "class1"

 

0 Likes
Accepted solutions (1)
1,130 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Typically, we try to separate concerns, we use the Form constructor(s) to pass data from the main class to the Form class and public properties in the Form class which can be accessed by the instance of the Form created in the main class.

 

Here's a simple example:

in the Form class

 

    public partial class MshTry : Form
    {
// get the selected item in the list box public string Layer => (string)listOfObjects.SelectedItem; public ModalDialog(List<string> objects) { InitializeComponent();

// Set the DialogResult property of the OK and Cancel buttons
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = Dialogresult.Cancel;

// fill the list box with the input list listOfObjects.DataSource = objects; } }

 

in the main class

 

            var objects = new List<string>() { "One", "Two", "Three" };

// create a new instance of MshTry passing it the lis of objects
using (var dialog = new MshTry(objects)) { // show the dialog box var dlgResult = Application.ShowModalDialog(dialog);
// check for the way the dialog was closed by the user (typically OK or Cancel) if (dlgResult == System.Windows.Forms.DialogResult.OK) { // get the selected item in the dialog selectedObject = dialog.Layer;

// do your stuff... } }

 

Note you should use Application.ShowModalDialog or Application.ShowModelessDialog methods to show your form from AutoCAD, see these topics:

https://www.keanw.com/2008/08/the-right-way-t.html

https://adndevblog.typepad.com/autocad/2012/06/displaying-modal-and-modeless-forms-in-autocad-net.ht...

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable

your reply really helped me , 

but the links you have provided don't work.

0 Likes