C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-21-2009, 06:23 AM   #1
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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
WDT is offline   Reply With Quote
Old 04-21-2009, 02:39 PM   #2
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 95
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.
theoobe is offline   Reply With Quote
Old 04-21-2009, 09:03 PM   #3
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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
WDT is offline   Reply With Quote
Old 04-21-2009, 11:56 PM   #4
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
Quote:
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.
nvoigt is offline   Reply With Quote
Old 04-22-2009, 03:19 AM   #5
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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
WDT is offline   Reply With Quote
Old 04-22-2009, 07:59 AM   #6
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 95
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.
theoobe is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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