Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: abc on January 21, 2008, 02:53:44 PM

Title: C# Controls
Post by: abc on January 21, 2008, 02:53:44 PM
What I'm trying to accomplish:
          On runtime, I want my program to scan a destination and if it finds a certain file, it creates a new object, (a picturebox) on a TabPage on a form.

Problem:
          I don't get any errors, or anything but my picturebox doesn't seem to show up, and I don't have a clue why, I'm trying to add this control to a tabpage, so I figure some how, thats giving me the problem... Below is my code for adding a new game to the tabpage. I tried this in another program just in the same class as a form, and on a form and it works, but some reason the tabpage isn't quite working out for me.


LeagueMain main = new LeagueMain();
        private PictureBox newGame;

        public void AddNewGame(string GameName, string GamePicPath, string GameExePath)
        {
            newGame = new PictureBox();
            newGame.SizeMode = PictureBoxSizeMode.AutoSize;
            newGame.Location = new System.Drawing.Point(50, 40);
            newGame.Image = System.Drawing.Image.FromFile(GamePicPath);
            main.tabMyGames.Controls.Add(newGame);
        }


Thanks for any help
Title: Re: C# Controls
Post by: Blaze on January 21, 2008, 06:38:38 PM
Do you need to set it visible in C#?
Title: Re: C# Controls
Post by: abc on January 21, 2008, 06:58:32 PM
When you say visible, you mean the visibility property, right? Because as far as I know it should be default at true.


EDIT: Yeah..That's not it.
Title: Re: C# Controls
Post by: Sidoh on January 21, 2008, 07:16:18 PM
Why are you using an instance variable for the PictureBox?  Just use one local to the scope of the method.  Try that and see what happens.
Title: Re: C# Controls
Post by: abc on January 21, 2008, 07:18:27 PM
Quote from: Sidoh on January 21, 2008, 07:16:18 PM
Why are you using an instance variable for the PictureBox?  Just use one local to the scope of the method.  Try that and see what happens.

What do you mean one local to scope of the method?
Title: Re: C# Controls
Post by: Sidoh on January 21, 2008, 07:21:25 PM
Quote from: Dale on January 21, 2008, 07:18:27 PM
What do you mean one local to scope of the method?


        public void AddNewGame(string GameName, string GamePicPath, string GameExePath)
        {
                PictureBox newGame = new PictureBox();
                ...
        }
Title: Re: C# Controls
Post by: abc on January 21, 2008, 07:23:40 PM
Quote from: Sidoh on January 21, 2008, 07:21:25 PM
Quote from: Dale on January 21, 2008, 07:18:27 PM
What do you mean one local to scope of the method?


        public void AddNewGame(string GameName, string GamePicPath, string GameExePath)
        {
                PictureBox newGame = new PictureBox();
                ...
        }


ah, yeah that's what I originally coded, but it still didn't work, then I read a little bit up on it and I ended up having what I showed you.
Title: Re: C# Controls
Post by: Sidoh on January 21, 2008, 07:26:04 PM
Try adding other controls to the tab page.  If that doesn't work, nuke the tab control, put a new one on there without changing any properties and see if that works.
Title: Re: C# Controls
Post by: abc on January 21, 2008, 07:34:33 PM
Quote from: Sidoh on January 21, 2008, 07:26:04 PM
Try adding other controls to the tab page.  If that doesn't work, nuke the tab control, put a new one on there without changing any properties and see if that works.

I tried that, (both) and no new outcome, but I do have an idea of what It could be but I don't know how to fix it. My guess is it's not using the same tabpage that I want it to use, since I use the "new" keyword.


EDIT: MyndFyre? :)
Title: Re: C# Controls
Post by: Sidoh on January 22, 2008, 06:58:22 PM
Seriously, it has to be the case that you're making some sort of oversight because I had this working with hardly any C# experience a while ago.

I still don't understand why you have a private variable for the picturebox.  There's no reason for that.

The new keyword where?  There's no reason that should have any impact on anything.

If "tabMyGames" is the tab page you want to access, then it will work if you're not doing anything dumb.  If that's the tab control itself, you're doing it wrong.  You need to access the tab page object (which are "tabPagen" by default, I'm pretty sure).

I got it to work in two minutes...

http://ulkesh.sidoh.org/~sidoh/addcontrol.png

private Form2 f;

private void Form1_Load(object sender, EventArgs e)
{
     this.f = new Form2();
     this.f.Show();
}

private void button1_Click(object sender, EventArgs e)
{
     PictureBox p = new PictureBox();
     p.ImageLocation = this.textBox1.Text;
     p.SizeMode = PictureBoxSizeMode.AutoSize;
     this.f.AddTabControl(p);
}


public void AddTabControl(Control c)
{
     this.tabPage1.Controls.Add(c);
}
Title: Re: C# Controls
Post by: abc on January 22, 2008, 07:24:03 PM
Quote from: Sidoh on January 22, 2008, 06:58:22 PM
Seriously, it has to be the case that you're making some sort of oversight because I had this working with hardly any C# experience a while ago.

I still don't understand why you have a private variable for the picturebox.  There's no reason for that.

The new keyword where?  There's no reason that should have any impact on anything.

If "tabMyGames" is the tab page you want to access, then it will work if you're not doing anything dumb.  If that's the tab control itself, you're doing it wrong.  You need to access the tab page object (which are "tabPagen" by default, I'm pretty sure).

I got it to work in two minutes...

http://ulkesh.sidoh.org/~sidoh/addcontrol.png

private Form2 f;

private void Form1_Load(object sender, EventArgs e)
{
     this.f = new Form2();
     this.f.Show();
}

private void button1_Click(object sender, EventArgs e)
{
     PictureBox p = new PictureBox();
     p.ImageLocation = this.textBox1.Text;
     p.SizeMode = PictureBoxSizeMode.AutoSize;
     this.f.AddTabControl(p);
}


public void AddTabControl(Control c)
{
     this.tabPage1.Controls.Add(c);
}


Mine is being taken place in another class.

EDIT:

Here is my code..


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace GameLeague
{
    class clsGameManager : Main
    {
        public void AddNewGame(string GameName, string GamePicPath, string GameExePath)
        {
            PictureBox newGame = new PictureBox();
            newGame.SizeMode = PictureBoxSizeMode.AutoSize;
            newGame.Location = new System.Drawing.Point(50, 40);
            newGame.Image = System.Drawing.Image.FromFile(GamePicPath);
            AddTabControl(newGame);
        }

        public void AddTabControl(Control c)
        {
            tabMyGames.Controls.Add(c);
        }
    }
}


Main.cs

public void tabMyGames_Enter(object sender, EventArgs e)
        {
            clsGameManager gMgr = new clsGameManager();
            gMgr.AddNewGame("LOL", "base.jpg", "lol2");
        }
Title: Re: C# Controls
Post by: Sidoh on January 22, 2008, 07:29:38 PM
What type is "tabMyGames"?
Title: Re: C# Controls
Post by: abc on January 22, 2008, 07:31:25 PM
Quote from: Sidoh on January 22, 2008, 07:29:38 PM
What type is "tabMyGames"?

Public, if thats what you mean.
Title: Re: C# Controls
Post by: Sidoh on January 22, 2008, 08:00:04 PM
Quote from: Dale on January 22, 2008, 07:31:25 PM
Quote from: Sidoh on January 22, 2008, 07:29:38 PM
What type is "tabMyGames"?

Public, if thats what you mean.

No, I mean the type as in "tabMyGames.GetType().ToString()".  I'm hoping our IM conversation cleared up your issue, though.
Title: Re: C# Controls
Post by: abc on January 22, 2008, 08:33:30 PM
Quote from: Sidoh on January 22, 2008, 08:00:04 PM
Quote from: Dale on January 22, 2008, 07:31:25 PM
Quote from: Sidoh on January 22, 2008, 07:29:38 PM
What type is "tabMyGames"?

Public, if thats what you mean.

No, I mean the type as in "tabMyGames.GetType().ToString()".  I'm hoping our IM conversation cleared up your issue, though.

Certainly did, works great, I just now have to solve the problem of adding objects side by side, instead of on a fixed value. I think I'm doing to use the "Flow Layout Panel" for help.
Title: Re: C# Controls
Post by: abc on January 22, 2008, 10:04:20 PM
The flow panel works great, I setup a random value, setup i the flow layout, and it goes to exactly where I want it too. It's a masterpiece  :D