Author Topic: winform with combobox  (Read 3112 times)

0 Members and 1 Guest are viewing this topic.

gvgbabu

  • Guest
winform with combobox
« on: April 23, 2014, 12:53:44 PM »
hi

i am creating a program of class library(Dll) using createmethod for autocad.
i have an array  in the class. i want to create a form with combobox and populate the array items. If user selects one item from the combobox items then my code proceeds on user's input.

is it possible to do? i have tried but not succeeded. i am struggling to do it
please guide me how to do it

Thanks in advance.
gvg

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: winform with combobox
« Reply #1 on: April 23, 2014, 01:05:25 PM »
This question is not about the AutoCAD .NET API. At first you need select a technology of GUI creating: Windows Forms, WPF or JavaScript.

gvgbabu

  • Guest
Re: winform with combobox
« Reply #2 on: April 23, 2014, 01:08:43 PM »
hi andrey

thanks for quick reply.

i am creating my program in vb.net.
i am very new to autocad .net
please guide me


gvg

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: winform with combobox
« Reply #3 on: April 23, 2014, 01:18:44 PM »
If you use AutoCAD older than AutoCAD 2009, then you can use Windows Forms only. If you use AutoCAD newer than AutoCAD 2008, then you can use Windows Forms or WPF. WPF is newer than Windows Forms and is more powerful impressive technology. JavaScript can be used in AutoCAD 2014 and newer (as I remember). I prefer WPF.

gvgbabu

  • Guest
Re: winform with combobox
« Reply #4 on: April 23, 2014, 01:20:43 PM »
i am using autocad 2007.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: winform with combobox
« Reply #5 on: April 23, 2014, 01:24:02 PM »
i am using autocad 2007.
So this book is for you.
« Last Edit: April 23, 2014, 01:33:02 PM by Andrey Bushman »

gvgbabu

  • Guest
Re: winform with combobox
« Reply #6 on: April 23, 2014, 02:07:02 PM »
i have tried with some examples
i am displaying the windows form in autocad
but how to populate combobox which in the form with an array which in the class library
please guide me

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: winform with combobox
« Reply #7 on: April 23, 2014, 02:12:53 PM »
Different ways. A Binding is a most right preference. Detailed info you can find in the book that I point to you in my previous message, or in the Google.

P.S. I recommend to you to spend some your time for reading it. This information will be useful for you often (if you are interested in quality of your program code).
« Last Edit: April 23, 2014, 02:22:26 PM by Andrey Bushman »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: winform with combobox
« Reply #8 on: April 24, 2014, 03:26:44 AM »
I didn't work long ago with this technology. Very simple example:
Code - C#: [Select]
  1. // © Andrey Bushman, 2014
  2. // Windows Forms: the binding sample.
  3. // Written for the topic: http://www.theswamp.org/index.php?topic=46873.0
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace WinFormsBindingSample {
  15.   class Program {
  16.  
  17.     static List<Person> data;
  18.  
  19.     [STAThread]
  20.     static void Main(string[] args) {
  21.  
  22.       data = new List<Person>();
  23.       data.Add(new Person("Vasya", "Vasilyev"));
  24.       data.Add(new Person("Petya", "Petrov"));
  25.       data.Add(new Person("Kolya", "Nikolaev"));
  26.       data.Add(new Person("Ksenia", "Sidorova"));
  27.  
  28.       Form form = new Form();
  29.       form.Text = "WinForms: the binding sample.";
  30.       form.Width = 500;
  31.       form.Height = 300;
  32.       form.MinimumSize = new Size(300, 200);
  33.       form.StartPosition = FormStartPosition.CenterScreen;
  34.  
  35.       FlowLayoutPanel panel = new FlowLayoutPanel();
  36.       panel.FlowDirection = FlowDirection.LeftToRight;
  37.       panel.WrapContents = true;
  38.       panel.Parent = form; // Also you can use it: form.Controls.Add(panel);
  39.       panel.Dock = DockStyle.Fill;
  40.       panel.BorderStyle = BorderStyle.FixedSingle;
  41.  
  42.       Label label = new Label();
  43.       label.Name = "label";
  44.       label.Text = "User names:";
  45.       label.TextAlign = ContentAlignment.MiddleCenter;
  46.       label.AutoSize = true;
  47.       label.Parent = panel;
  48.  
  49.  
  50.       ComboBox cbx = new ComboBox();
  51.       cbx.Name = "cbx";
  52.       cbx.DataSource = data;
  53.       cbx.ValueMember = ".";
  54.       cbx.DisplayMember = "FullName";
  55.       cbx.Parent = panel;
  56.       cbx.SelectedIndexChanged += cbx_SelectedIndexChanged;
  57.  
  58.       Button btn = new Button();
  59.       btn.Name = "btn";
  60.       btn.Text = "Change data";
  61.       btn.AutoSize = true;
  62.       btn.Parent = panel;
  63.       btn.Click += btn_Click;
  64.  
  65.       Application.EnableVisualStyles();
  66.       Application.Run(form);
  67.     }
  68.  
  69.     static void cbx_SelectedIndexChanged(object sender, EventArgs e) {
  70.       ComboBox cbx = sender as ComboBox;
  71.       if (cbx.SelectedIndex < 0)
  72.         return;
  73.       Person person = cbx.SelectedItem as Person;
  74.       MessageBox.Show(person.FullName, "Selection changed",
  75.         MessageBoxButtons.OK, MessageBoxIcon.Information);
  76.     }
  77.  
  78.     static void btn_Click(object sender, EventArgs e) {
  79.  
  80.       Button btn = sender as Button;
  81.       FlowLayoutPanel panel = btn.Parent as FlowLayoutPanel;
  82.       Form form = panel.Parent as Form;
  83.  
  84.       // Examples: how to find a control by its name.
  85.       Boolean x1 = form.Controls.ContainsKey("cbx"); // false
  86.       Boolean x2 = panel.Controls.ContainsKey("cbx"); // true
  87.       Control[] result = form.Controls.Find("cbx", true); // items count: 1.
  88.       Control[] result2 = form.Controls.Find("cbx2", true); // items count: 0.
  89.  
  90.       Person ivan = new Person("Ivan", "Ivanov");
  91.  
  92.       if (!data.Any(n => n.FullName == ivan.FullName)) {
  93.         ComboBox cbx = result[0] as ComboBox;
  94.         Person oldValue = cbx.SelectedValue as Person;
  95.  
  96.         // Change data source:
  97.         data.Add(new Person("Ivan", "Ivanov")); // add new item
  98.         data.Remove(data[1]); // remove any item
  99.  
  100.         Person newValue = cbx.SelectedValue as Person;
  101.  
  102.         // Refresh items list
  103.         cbx.SelectedIndexChanged -= cbx_SelectedIndexChanged;
  104.         cbx.DataSource = null;
  105.         cbx.DataSource = data;
  106.         cbx.ValueMember = ".";
  107.         cbx.DisplayMember = "FullName";
  108.  
  109.         if (newValue == null || newValue != oldValue)
  110.           cbx.SelectedIndex = 0;
  111.  
  112.         cbx.SelectedIndexChanged += cbx_SelectedIndexChanged;
  113.       }
  114.       else {
  115.         MessageBox.Show(ivan.FullName, "Already exists",
  116.         MessageBoxButtons.OK, MessageBoxIcon.Information);
  117.       }
  118.     }
  119.   }
  120.  
  121.   class Person {
  122.     public Person(String name, String surname) {
  123.       Name = name;
  124.       Surname = surname;
  125.     }
  126.     public String Name { get; set; }
  127.     public String Surname { get; set; }
  128.     public String FullName { get { return ToString(); } }
  129.     public override string ToString() {
  130.       return String.Format("{0} {1}", Name, Surname);
  131.     }
  132.   }
  133. }

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: winform with combobox
« Reply #9 on: April 24, 2014, 03:48:16 AM »
AutoCAD 2007 use the .NET Framework 2.0, as I see here. .Net 2.0, 3.0 and 3.5 SP1 use the same CLR version. So you can try to use .NET Framework 3.5 (if it installed on your computer). If this will successful you can use WPF instead of WinForms (and many other useful technologies: LINQ, etc). I can't check myself because I have not  an installed AutoCAD 2007.

gvgbabu

  • Guest
Re: winform with combobox
« Reply #10 on: April 27, 2014, 12:42:31 PM »
hi andrey,

thank you very much for your advice and code.

i got it successfully with your words and advice.

But i did not get the book that you suggested.

regards

gvg