Welcome to Laser Pointer Forums - discuss green laser pointers, blue laser pointers, and all types of lasers

LPF Donation via Stripe | LPF Donation - Other Methods

Links below open in new window

ArcticMyst Security by Avery

Some visual C# coding help

Joined
Feb 5, 2008
Messages
6,252
Points
83
Hey guys,

I'm trying to code up something basic in Visual C# as a learning project, hoping to step up my coding game and eventually be of some use to those fine folks over at ME3 Explorer project. But anyway,

What I'm trying to accomplish is sort of a Window 7 style taskbar.

There is a toolset, still work in progress, made for modifying various files of an Unreal Engine 3 game, Mass Effect 3.

Interface of the toolset (Form1) offers a variety of tools, and I'd like to add a small taskbar-like panel to the bottom so you can easily keep track of what tools do you currently have open.

Two parts of my goal are:
When an icon in taskbar is clicked, the corresponding took is brought up to the top and focus switched to it. That I already have completed.

Second part is, when the tool (form) is closed, the corresponding icon from the taskbar should be removed. That is what I'm having problems with.

I have created a new Panel to act as a taskbar. It will create Controls, specifically Image Boxes, as tools are ran.

Controls have an event handler when click is made.

My class "taskbar" which contains the most of my code (as not to disturb the original code as much as possible), defines these public variables:

Code:
public static Image symbol;
private static PictureBox tool_icon;
public static List<Form> tools = new List<Form>();

A public function is made which when used, will add a control to the panel:
In class taskbar:
Code:
public static Control AddTool(Form tool, Image symbol)
        {            
            
            tool_icon = new PictureBox(); //new control initialized
            tool_icon.Location = new Point(x_pos, 2);
            x_pos += 50; //set position to stack against the previously made control
            tool_icon.Size = new Size(50, 60);            
            tool_icon.Click += new EventHandler(tipek_onclick); //event handler for when control is clicked
            tool_icon.Image = symbol;     //tool specific image       
            tool_icon.Tag = tools.Count().ToString(); //specifying tag which will later be used in click event
            tools.Add(tool); //adding an originating Form to the list
            
            return tool_icon; //finally, returning the Control to originating form
        }

This function is called within Form1 like this:
In Form 1:
Code:
PCCExplorer px = new PCCExplorer();
            lang.SetLang(px); //ignore this
            px.MdiParent = this; //ignore this
            px.WindowState = FormWindowState.Maximized; //ignore this
            panel1.Controls.Add(taskbar.AddTool(px, imageList1.Images[0])); //creating a new control in current Form1 with AddTool function, forwarding the PCCExplorer form tool and it's icon to the function
...


Later when a click event is being made:
In class taskbar:
Code:
public static void tipek_onclick(object sender, EventArgs e)
           {
               Control m = sender as Control;  //sender of argument is control, ImageBox from form1          
              
               Form active = tools[Convert.ToInt32(m.Tag)]; //gets a corresponding Form from the list of forms, by the Tag count
               active.Focus();             

           }

Now here's where I'm stuck.

I'm trying to create new function in class taskbar:
Code:
public static void RemoveTool()
        {

        }

I know for a certain I will have this in :
Code:
public static void RemoveTool()
        {




            x_pos -= 50;
        }

Firstly I'd like to know how would I go on about to manipulate Form1's Panel controls from my Class taskbar.

This obviously will not work:
public static void RemoveTool()
{

Form1.panel1.Controls.Remove( ... control ...);


x_pos -= 50;
}

As I cannot reference a panel in Form1, giving me error:
Error 4 An object reference is required for the non-static field, method, or property 'ME3Explorer.Form1.panel1'

How would I go about referencing it?

Second, how would I match the corresponding Control in the panel, with the Form that just closed?

For example, in previous example of PCCExplorer tool, let's create a new event handler for FormClose event:
Code:
private void PCCExplorer_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

How would I use this to match it to the icon it created when it got called from Form1?

Sorry for the long-ish thread, and I'd really appreciate any help. I'm really trying hard to learn some coding because it definitely looks like a very useful skill to have, and that's not taking into account later college subjects requiring this.

Thanks in advance!
 





ARG

0
Joined
Feb 27, 2011
Messages
6,772
Points
113
Code:
public static void RemoveTool(Form1 form)
{

form.panel1.Controls.Remove( ... control ...);


x_pos -= 50;
}

Think I figured it out. You're referencing the class when you should be referencing the object?
 
Joined
Feb 5, 2008
Messages
6,252
Points
83
I figured out that part.

Code:
public static void RemoveTool(object sender)
        {
            x_pos -= 50;
           
            
            Form1 f = new Form1();
            f.panel1.Controls.Remove(match);

            
        }

What I can't figure out is how to match the icon to the form when it's closing...

I'm trying this,

In public variables, new list is added:
Code:
public static List<Control> taskbar_items = new List<Control>();

Then, in creation of the Control:
Code:
...
tool_icon.Tag = tools.Count().ToString();
            tools.Add(tool);
            taskbar_items.Add(tool_icon);
            return tool_icon;
...

Then, in function that's supposed to delete the icon when form is closed:

Code:
public static void RemoveTool(object sender)
        {
            x_pos -= 50;
            Form original = sender as Form;
            Control match = taskbar_items[Convert.ToInt32(original.Tag)];
            
            Form1 f = new Form1();
            f.panel1.Controls.Remove(match);

            
        }

And in the FormClosing event of PCCexplorer, I am running this function with argument "this",

But no icons get removed from taskbar.

Hmm...
 
Last edited:
Joined
Feb 5, 2008
Messages
6,252
Points
83
Alright guys I figured everything out.

Ended up using a toolstrip, not a panel.

Anyway, here's a thread:
me3explorer.freeforums.org • View topic - Taskbar added in new version

Including me, there's 4 people working on this project.

If you're any good with coding, texturing, modelling, animation (basically, code / 2D art / 3D Art), please consider joining the ME3 Explorer forums and help us out with the project.

We're basically developing a Mass Effect 3 Software Dev Kit, ground up. Ultimate modding tool :p

We'd greatly appreciate it!
http://www.facebook.com/pages/Creating-new-end-for-Mass-Effect-3/145902408865659

http://www.youtube.com/watch?v=kTqaSllnhsI

And finally, the forum:
http://me3explorer.freeforums.org/index.php
 
Last edited:
Joined
Jun 22, 2011
Messages
2,431
Points
83
A better option might be to inherit the Form class to make a ToolForm class that will do the stuff common to all tool forms. Then you can override OnLoad and OnClosing on that class to add/remove the button on the taskbar. The button on the taskbar would also ideally inherit from whatever class you're using, that way you don't have to set every button's properties every time - on a similar situation I used CheckBox because it could show an image and stay pressed/unpressed depending on the tool being activated or not.

If the tool button is always created after the form then you could just pass the form's reference on the button's constructor:
Code:
ToolButton tb = new ToolButton( this ); // this being the form
toolPanel.Controls.Add( tb );
this.TaskbarButton = tb; // a member/property of ToolForm

Then on OnClosing you'd do something like
Code:
toolPanel.Controls.Remove( this.TaskbarButton );
this.TaskbarButton.Dispose(); // don't forget to dispose, windows forms' controls won't be garbage collected automatically

Another option is to always keep all the buttons on the taskbar and make them invisible ;) But only do that if there's a limited number of tools and if they can't be created multiple times. It's sort of ugly but you can lay out the buttons on the editor.

And if you really want to set the button's properties & events instead of overriding the class it'll look much better with closures and object initializers:
Code:
Button b = new Button{ Name = "tool'", new Size( 50, 60 ) }; // set all properties here

b.Click += ( o, e ) =>
{
	// your event code here, you can reference outside variables at will!
};


I've done a fair share of stuff like this in C# so feel free to ask!
 
Last edited:
Joined
Feb 5, 2008
Messages
6,252
Points
83
I've done a fair share of stuff like this in C# so feel free to ask!
We already worked it out and moving on to larger issues, but thank you a lot anyway!

The only thing I'm gonna ask you is take a quick look at my signature :)

We can definitely use any help we can.
 
Joined
Jun 22, 2011
Messages
2,431
Points
83
Cool, just remember it's easier to write your code with modularity in mind than to adapt it later on :)

I'd love to help but my spare time is being completely used by my electronics/Arduino/high voltage projects. Good luck with you project!
 




Top