Dynamic adding of Controls

This is a discussion on Dynamic adding of Controls within the C# Programming forums, part of the General Programming Boards category; Hi. I'm still learning the VStudio Express 2008 IDE. I'm tired of outputing to console and now just adding (well ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Dynamic adding of Controls

    Hi.

    I'm still learning the VStudio Express 2008 IDE. I'm tired of outputing to console and now just adding (well trying to) controls dynamcally however this hasn't met with any success yet. The following is a piece of my code to which everything compiles with no warning but howevere the labels aren't showing:
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
    
                System.Windows.Forms.Label label1, label2;
    
                HidD_GetHidGuid(ref tGuid);
                deviceInfoSet = SetupDiGetClassDevs(ref tGuid, "USB", IntPtr.Zero, 0);
    
                
    
                label1 = new Label();
                label1.Location = new Point(10, 15);
                label2 = new Label();
                label2.Location = new Point(50, 15);
    
                label1.AutoSize = true;
                label1.Size = new Size(28, 40);
                label1.Visible = true;
                
                label2.AutoSize = true;
                label2.Visible = true;            
    
                label1.Text = "Device Info Set = " + deviceInfoSet.ToString();
                label2.Text = "HidGuid = " + tGuid.ToString();
    
                this.Controls.Add(label1);
            }
    I've even tried adding label1.show() but so far.. Zip!
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication14
    {
        public partial class Form1 : Form
        {
            private Label label1;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.label1 = new Label();
                this.label1.Location = new Point(10, 15);
                this.label1.AutoSize = true;
                this.label1.Size = new Size(28, 40);
                this.label1.Text = "Device Info Set = blah";
                this.Controls.Add(this.label1);
            }
        }
    }
    That works fine for me. The only difference I've made is declaring label1 outside of the Load event.

    Edit: By the way, do you have something already at point 10, 15? If so you would have to consider the label's position in the form's z-order. Maybe call this.label1.BringToFront();
    Last edited by theoobe; 04-21-2009 at 02:42 PM.

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thanks I just copied it over to a new form, but I now have another question.

    Given the following:
    Code:
    [DllImport("setupapi.dll", CharSet=CharSet.Auto, SetLastError = true)]
            public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, ref SP_DEVINFO_DATA devInfo,
                                                             ref Guid interfaceClassGuid, UInt32 memberIndex,
                                                             ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
    why is:
    Code:
    result = SetupDiEnumDeviceInterfaces(deviceInfoSet, 0, ref tGuid, MemberIndex, ref tDevInterfaceData);
    not valid. I can't seem to pass null or zero to argument 2.

    the type in argument 2 is defined as:
    Code:
            [StructLayout(LayoutKind.Sequential)]
            public struct SP_DEVINFO_DATA
            {
                public uint cbSize;
                public Guid ClassGuid;
                public uint DevInst;
                public ulong Reserved;
            }
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    I can't seem to pass null or zero to argument 2.
    Then don't. Pass a parameter as reference of the type that you declared. Anything else is invalid.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I need to pass a null value instead as it is possible to do so according to msdn to be able to retrieve the information I want.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I don't see why it would be possible. In your declaration it clearly states that value 2 is expecting a ref, and you're not doing so.

    You must change your declaration to:

    Code:
    public static extern bool SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid,
                                  UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
    This will allow you then to use:

    Code:
    SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref tGuid, MemberIndex, ref tDevInterfaceData);
    Last edited by theoobe; 04-22-2009 at 08:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ListView controls - Adding items .. What the beep?
    By IceDane in forum Windows Programming
    Replies: 7
    Last Post: 04-08-2008, 12:07 PM
  2. Adding a directory to a dynamic library path
    By vivharv in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2007, 07:09 AM
  3. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 02:44 PM
  4. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 11:35 AM
  5. I need help on adding tooltips to controls
    By Templario in forum Windows Programming
    Replies: 6
    Last Post: 01-03-2003, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21