Hi,

I'm working on C# with COM. I have a COM OLE control that I wish to create on the form as control array during runtime, as the number of cotrols depends on the number of files in the corresponding folder. How do I go about it? I'm attaching a pic of the form as to how I want it to be.

Thanks

Like you create an array from a simple data type, Get the number of files in the folder, and create the array at runtime.

I have code to get the number of files in the folder. But, don't know how to create array. Could you help me please?

Same as you would for any variable,

int[] i = new int[9];

Tried all methods, but not working. Attached is the ocx file.

The ocx.

How about you post the code you have, not the ocx, its most likely the issue

Here's the code.

namespace BM
{
    public partial class MainFrm : Form
    {
        private SLIDELib.Slide[] slide;
        private int sldNum;

        public MainFrm()
        {
            InitializeComponent();
           
            DirectoryInfo di = new DirectoryInfo(@"C:\Test");
            DirectoryInfo[] subDirs = di.GetDirectories();

            foreach (DirectoryInfo subDir in subDirs)
            {
                cboDept.Items.Add(subDir.Name);
            }

            cboDept.Sorted = true;
            cboDept.SelectedIndex = 0;
        }

        private void cboDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            TV.Nodes.Clear();
            TreeNode rootNode;

            DirectoryInfo info = new DirectoryInfo(@"C:\Test\" + cboDept.Text.ToString());
            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                TV.Nodes.Add(rootNode);
            }

            TV.Sort();
        }

        private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
        {
            TreeNode aNode;
            DirectoryInfo[] subSubDirs;
            foreach (DirectoryInfo subDir in subDirs)
            {
                if (!subDir.Name.StartsWith("_"))
                {
                    aNode = new TreeNode(subDir.Name, 0,1);
                    aNode.Tag = subDir;
                    subSubDirs = subDir.GetDirectories();
                    if (subSubDirs.Length != 0)
                    {
                        GetDirectories(subSubDirs, aNode);
                    }
                    nodeToAddTo.Nodes.Add(aNode);
                }
            }
                
        }

        private void TV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode newSelected = e.Node;
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;

            int numFile = 0;
            
            sldNum = nodeDirInfo.GetFiles("*.sld").Count();
            //sldNum = sldNum / 15;

            foreach (FileInfo file in nodeDirInfo.GetFiles())
            {
                if (file.Extension==".sld")
                {
                    numFile++;
                    slides[numFile] = new SLIDELib.Slide();
                    slide[numFile].Width = 125;
                    slide[numFile].Height = 115;
                    slide[numFile].Left = 9 + slide[numFile].Width;
                    slide[numFile].Top = 23 + slide[numFile].Height;

                    slides[numFile].FileName = file.FullName;
                }
            }
        }
   }
}

so
Out of the code that was relevant, eg

slides[numFile] = new SLIDELib.Slide();
                    slide[numFile].Width = 125;
                    slide[numFile].Height = 115;
                    slide[numFile].Left = 9 + slide[numFile].Width;
                    slide[numFile].Top = 23 + slide[numFile].Height;

                    slides[numFile].FileName = file.FullName;

The obvious thing is you never added the control to anything, so I bet none are showing up.

this.Controls.Add(......)

oh yea....

But the error starts at
slide[numFile].Width = 125;

Probably because slide[numFile] is still null.

WOW! Great! It worked! Thank you soooooo much......

Here's the code.

namespace BM
{
    public partial class MainFrm : Form
    {
        //private SLIDELib.Slide[] sldArray;
        private AxSLIDELib.AxSlide[] sldArray;
        private int sldNum;

        public MainFrm()
        {
            InitializeComponent();
           
            DirectoryInfo di = new DirectoryInfo(@"Q:\Details");
            DirectoryInfo[] subDirs = di.GetDirectories();

            foreach (DirectoryInfo subDir in subDirs)
            {
                cboDept.Items.Add(subDir.Name);
            }

            cboDept.Sorted = true;
            cboDept.SelectedIndex = 0;
        }

        private void cboDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            TV.Nodes.Clear();
            TreeNode rootNode;

            DirectoryInfo info = new DirectoryInfo(@"Q:\Details\" + cboDept.Text.ToString());
            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                TV.Nodes.Add(rootNode);
            }

            TV.Sort();
        }

        private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
        {
            TreeNode aNode;
            DirectoryInfo[] subSubDirs;
            foreach (DirectoryInfo subDir in subDirs)
            {
                if (!subDir.Name.StartsWith("_"))
                {
                    aNode = new TreeNode(subDir.Name, 0,1);
                    aNode.Tag = subDir;
                    subSubDirs = subDir.GetDirectories();
                    if (subSubDirs.Length != 0)
                    {
                        GetDirectories(subSubDirs, aNode);
                    }
                    nodeToAddTo.Nodes.Add(aNode);
                }
            }
                
        }

        private void TV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode newSelected = e.Node;
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;

            int numFile = 0;
            
            sldNum = nodeDirInfo.GetFiles("*.sld").Count();
            ShowSld();

            foreach (FileInfo file in nodeDirInfo.GetFiles())
            {
                if (file.Extension == ".sld")
                {
                    numFile++;
                    sldArray[numFile].FileName = file.FullName;
                    if (numFile > 15)
                    {
                        break;
                    }
                }
            }

        }

        private void ShowSld()
        {
            int xPos = 9;
            int yPos = 23;
            AddControls("sld", sldNum); //Create 15 slides.
            int n = 1;
            while (n < sldNum + 1)
            {
                sldArray[n].Tag = n;
                sldArray[n].Width = 125;
                sldArray[n].Height = 115;
                if (yPos > 140)
                {
                    yPos = 8;
                    xPos = xPos + sldArray[n].Width + 8;
                }
                sldArray[n].Left = xPos;
                sldArray[n].Top = yPos;
                yPos = yPos + sldArray[n].Height + 8;
                groupBox2.Controls.Add(sldArray[n]);
                n++;
            }
        }

        private void AddControls(string anyControl, int cNumber)
        {
            switch (anyControl)
            {
                case "sld":

                   sldArray = new AxSLIDELib.AxSlide[cNumber + 1];
                    for (int i = 0; i < cNumber + 1; i++)
                    {
                        sldArray[i] = new AxSLIDELib.AxSlide();
                    }
                    break;

            }
        }

    }
}

The controls are not arranged in order. When it fills the first row of the groupbox, how do I write code to bring the next control to the next row?

It works fine now, when I adjusted the position. Could you help me out on how to use the Previous and Next buttons to go to the next page if more than 12 slides and to go to the previous page to view the previous set of slides.

Thanks

Well.. You have to work the logic out, much like the placing on the screen logic..

Write down on paper how you thikn it should work, and at least paste the logic and your attempt at the code here if it doesnt work....

Looks like I have solved the issue. Here is the code.

private void TV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            btnPrev.Enabled = false;
            btnNext.Enabled = false;

            TreeNode newSelected = e.Node;
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;

            int numFile = 0;

            this.groupBox2.Controls.Clear();

            sldNum = nodeDirInfo.GetFiles("*.sld").Count();
            ShowSld();

            foreach (FileInfo file in nodeDirInfo.GetFiles("*.sld"))
            {
                     numFile++;
                    sldArray[numFile].FileName = file.FullName;
             }
        }

        private void ShowSld()
        {
            int xPos = 10;// 140;
            int yPos = 23;// 140;
            AddControls("sld", sldNum); //Create slides.
            int n = 1;
            while (n < sldNum + 1)
            {
                sldArray[n].Tag = n;
                sldArray[n].Width = 125;
                sldArray[n].Height = 115;
                if (xPos > 570)
                {
                    xPos = 10;
                    yPos = yPos + sldArray[n].Height + 28;
                }
                sldArray[n].Left = xPos;
                sldArray[n].Top = yPos;
                xPos = xPos + sldArray[n].Width + 15;
                groupBox2.Controls.Add(sldArray[n]);
                n++;
            }
        }

        private void AddControls(string anyControl, int cNumber)
        {
            switch (anyControl)
            {
                case "sld":

                    sldArray = new AxSLIDELib.AxSlide[cNumber + 1];
                    for (int i = 0; i < cNumber + 1; i++)
                    {
                        sldArray[i] = new AxSLIDELib.AxSlide();
                    }
                    break;

            }
        }

But my next problem is, if there are more than 15 files in the folder, how do I display it in the form using the Previous and Next button?

Thanks

As I said before, work it out on paper, you have a few ways to do it. One is messier to code but easier on the eyes, the other a bit more maths but easier to code.

Either way, its a logic issue not a coding issue, so have a go at working out how you might do it.

If nothing else, start a new console based app, and get it to print numbers 1-15 for page 1, and then move to page 2, and back, and so on. so you can concentrate purely on that aspect.

hi u can use this sample i create it for u plz aknowledge me that it`s good for u or not ...

this is sample :

int conditionOne=0;
int counterx=0;
int countery = 0;
bool conditionOne2 = false;
for (i = 0; i < Form1.rows; i++)
{
for (j = 0; j < Form1.colomns; j++)
{
b[i,j] = new Button();
if (conditionOne == i && conditionOne2==true )
{
counterx += 60;
}
else if(conditionOne2==true)
{
countery += 40;
counterx = 0;
}
b[i,j].Location = new System.Drawing.Point(counterx + 10, countery + 10);
b[i,j].Name = "button1" + i.ToString();
b[i,j].Size = new System.Drawing.Size(50, 20);
b[i,j].TabIndex = 2;
b[i,j].Text = i.ToString()+","+j.ToString();
b[i,j].UseVisualStyleBackColor = true;
this.Controls.Add(b[i,j]);
conditionOne2 = true;
conditionOne = i;
}

if u want more explanation contact me :)

sorry here it is

BlackSun, thanks for the code. But I don't think this would be practical for images. I prefer to use 15 images at one time, then use the Next button to view the next set of images.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.