Author Topic: C# Controls  (Read 6095 times)

0 Members and 1 Guest are viewing this topic.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
C# Controls
« 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.

Code: [Select]
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
« Last Edit: January 21, 2008, 04:33:35 pm by Dale »

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: C# Controls
« Reply #1 on: January 21, 2008, 06:38:38 pm »
Do you need to set it visible in C#?
And like a fool I believed myself, and thought I was somebody else...

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #2 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.
« Last Edit: January 21, 2008, 07:09:56 pm by Dale »

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: C# Controls
« Reply #3 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.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #4 on: January 21, 2008, 07:18:27 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?

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: C# Controls
« Reply #5 on: January 21, 2008, 07:21:25 pm »
What do you mean one local to scope of the method?

Code: [Select]
        public void AddNewGame(string GameName, string GamePicPath, string GameExePath)
        {
                PictureBox newGame = new PictureBox();
                ...
        }

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #6 on: January 21, 2008, 07:23:40 pm »
What do you mean one local to scope of the method?

Code: [Select]
        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.

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: C# Controls
« Reply #7 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.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #8 on: January 21, 2008, 07:34:33 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? :)
« Last Edit: January 22, 2008, 01:58:10 pm by Dale »

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: C# Controls
« Reply #9 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

Code: [Select]
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);
}

Code: [Select]
public void AddTabControl(Control c)
{
     this.tabPage1.Controls.Add(c);
}

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #10 on: January 22, 2008, 07:24:03 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

Code: [Select]
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);
}

Code: [Select]
public void AddTabControl(Control c)
{
     this.tabPage1.Controls.Add(c);
}

Mine is being taken place in another class.

EDIT:

Here is my code..

Code: [Select]
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
Code: [Select]
public void tabMyGames_Enter(object sender, EventArgs e)
        {
            clsGameManager gMgr = new clsGameManager();
            gMgr.AddNewGame("LOL", "base.jpg", "lol2");
        }
« Last Edit: January 22, 2008, 07:27:03 pm by Dale »

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: C# Controls
« Reply #11 on: January 22, 2008, 07:29:38 pm »
What type is "tabMyGames"?

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #12 on: January 22, 2008, 07:31:25 pm »
What type is "tabMyGames"?

Public, if thats what you mean.

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: C# Controls
« Reply #13 on: January 22, 2008, 08:00:04 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.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: C# Controls
« Reply #14 on: January 22, 2008, 08:33:30 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.