Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#257879 - 08/06/2005 06:25 Contents of variables to modify controls (C#)
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Ok, not sure if this is possible or not, but I can't figure out how to get this working if it is. First, some quick code to show what I want to do:
Code:

public void DeleteControl(int controlnumber) {
this.Controls.Remove(this.textbox + controlnumber);
}



I know the code above won't work, but it should give a rough idea of what I want to do. I want to be able to pass say 4 to that function, and have control named "textbox4" be removed. How do I transform the contents of a variable into the proper type for the controls part to see it as vaild and remove textbox4?

Top
#257880 - 08/06/2005 09:11 Re: Contents of variables to modify controls (C#) [Re: drakino]
andym
carpal tunnel

Registered: 17/01/2002
Posts: 3996
Loc: Manchester UK
I don't know about C# but if I had a bunch of controls in Qt I wanted to address logically I usually just use an array of pointers which I populate during the creation/construction phase of the window.
_________________________
Cheers,

Andy M

Top
#257881 - 08/06/2005 10:21 Re: Contents of variables to modify controls (C#) [Re: drakino]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
There are a couple of ways to do this, but if you are going to follow a convention where you know the first part of the name and only need to pass in the number, here is something that would work


Code:

private void DeleteControl(int controlNumber)
{
foreach (Control control in this.Controls)
{
String compareString = "textBox" + controlNumber.ToString();
if (control.Name == compareString)
{
this.Controls.Remove(control);
break;
}
}
}



BTW, if you're going to loop through with a "ForEach", you NEED the "break" line so it doesn't continue to loop and start referencing null values.

I don't believe there is anyway to reference a variable by name without looping. Hope this helps . . .
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257882 - 08/06/2005 10:26 Re: Contents of variables to modify controls (C#) [Re: JeffS]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
I should add that if you don't want to follow a strict naming convention like "textbox1, textbox2, . . ." then you could always use the "Tag" properly instead and just compare against it.

Code:
if (control.Tag.ToString() == controNumber.ToString())

_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257883 - 08/06/2005 11:30 Re: Contents of variables to modify controls (C#) [Re: JeffS]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
Quote:
you NEED the "break" line so it doesn't continue to loop and start referencing null values.


More importantly, it'll throw an exception when you try to iterate over a modified collection.

The other thing you need to know is that, if 'this' is a System.Windows.Forms.Panel, there's a bug in .NET 1.1 (might be fixed in 2.0) which -- if you're doing repeated Controls.Add and Controls.Remove actions -- causes your app to not exit properly (you can click on the Close button until the cows come home, and nothing happens).

To fix it, you need something like this:

Code:
void RemoveControlFromPanel(Panel p, Control c)
{
p.Controls.Remove(c);
if (!c.IsDisposed)
c.Dispose();
}

void RemoveAllControlsFromPanel(Panel p)
{
foreach (Control c in p.Controls)
{
if (!c.IsDisposed)
c.Dispose();
}

p.Controls.Clear();
}

_________________________
-- roger

Top
#257884 - 08/06/2005 12:36 Re: Contents of variables to modify controls (C#) [Re: Roger]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Quote:
More importantly, it'll throw an exception when you try to iterate over a modified collection.
Yeah, what I meant to say . . .


Edited by JeffS (08/06/2005 12:38)
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257885 - 08/06/2005 15:50 Re: Contents of variables to modify controls (C#) [Re: andym]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
To adress Andy first, pointers in C# are normally avoided. They can be used, but functions that use them must be marked "unsafe", and the entire program must be compiled with an /unsafe flag. This also has the disadvantage of forcing lots of manual garbage cleanup and disabling other niceties that .Net normally takes care of in C# code. Not sure I really want to deal with the mess that will ensue if I use pointers right now.

As far as the example Jeff, do you know perhaps a better way to be doing this to avoid loops? I don't need to pass a number, it could be a string or really any type of data I can stuff into an array to keep track of what is where. The reason I chose a number is because they are easy to increment. My goal with this as an example is:

I want to have a situation where there can be a maximum of 8 groupboxes in the form, but if they are not all needed, less will be shown. Each groupbox contains identical controls (for example sake) to modify a text file 0-7. Now, the program does some checks to see if text file 0-7 exists right now, and if one doesn't, it disables the buttons for that text file. I'm getting to a point where it would be nicer to just not have the groupbox at all if one file isn't there. So, I am invisioning an internal array where:

control[0]=0;
control[1]=1;
control[2]=3; (because text file 2 doesn't exist)
...

And thus now having the program only show controls for files 0,1,3,4,5,6,7, and with no visible gap between 1 and 3 because the controls that normally would be shown for 2 are now modifying 3 and such.

And Roger, thanks for the heads up. That might actually become an issue for me, so I'll keep it in mind if I see issues of the program not closing.

I'm kinda brute forcing myself to learn C# in a method that I teach myself what I need at a certain point in this basic program. Most things I have found answers for, but this particular situation is difficult to search books/manuals/online for an answer.

Top
#257886 - 08/06/2005 17:31 Re: Contents of variables to modify controls (C#) [Re: drakino]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
Quote:
To adress Andy first, pointers in C# are normally avoided.


Yeah, but Andy's point still applies -- except that you'd be using references. You can use an array of controls and create them at runtime. I'd post some code, but (a) I'm sitting at a computer that doesn't have VC# installed, and (b) I've got to run off and take an MS exam in about 20 minutes.
_________________________
-- roger

Top
#257887 - 08/06/2005 17:38 Re: Contents of variables to modify controls (C#) [Re: drakino]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Quote:
To adress Andy first, pointers in C# are normally avoided.
True in the strictest sense; however, the concept is that you store a REFERENCE to the object (or variable) and then use it later. While in simpler languages this can only be done with a pointer, in oop languages the concept of an "instance" came along and so did variables to reference them- essentially giving you the same power as a pointer. Think of an object reference as a pointer++, because under the hood, that's what it really is- a pointer to the memory where the data is stored plus a pointer to the structure of how to understand that data. The details of this really aren't that important, but the point is that while .net languages do not have pointers, you can still pass around references to objects. So you could create an array of controls and then track your controls with it:

Code:

myControls[0]=control1;
myControls[1]=control2;
...



Of course, that's what self.Controls is already doing- only with EVERY control, not just the one's in which you're interested.


Quote:
As far as the example Jeff, do you know perhaps a better way to be doing this to avoid loops?
My question would be, why would you not want to use a loop? Since it is self contained in the method, the calling code will be very clear and this kind of loop will not consumer many resources. IMHO, this is probably the most instutive and clear from a readability standpoint- probably more clear (and less prone to programming error) than trying to track a list of object references- if only because your code will all be isolated to one method rather than having to create and maintain an array. So I guess the short answer is that I don't know a better way (but someone else might) . . .

Quote:
I want to have a situation where there can be a maximum of 8 groupboxes in the form, but if they are not all needed, less will be shown. Each groupbox contains identical controls (for example sake) to modify a text file 0-7.
Honestly, if this were a problem I had to address, I'd probably create a User Control that was self contained and knew how to handle all of the editing. Then I'd create a class that could manage a set of these controls, position them correctly, and handle the proper files. This would allow you to prevent duplicate code and keep all of the complex logic of dealing with missing group boxes all together. However, if you are just learning this might be a bit advanced. It depends on your background with oop and creating custom controls.


Quote:
And thus now having the program only show controls for files 0,1,3,4,5,6,7, and with no visible gap between 1 and 3 because the controls that normally would be shown for 2 are now modifying 3 and such.

The one thing you can consider for handeling boxes appearing without gaps is how they are "docked". If you have a list and they are all docked to the "top", making one invisible (or deleting it) would cause the others to "snap" up. Another option might be a tabbed interface where each box goes on its own tab. In this scenerio it is easy to hide or delete any of the tabs and there would be no "gap".

I hopes this is helpful- I fear I might be confusing the issue more!
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257888 - 08/06/2005 17:42 Re: Contents of variables to modify controls (C#) [Re: Roger]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Quote:
Yeah, but Andy's point still applies -- except that you'd be using references.
Beat me to it while I was still typing!
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257889 - 08/06/2005 19:44 Re: Contents of variables to modify controls (C#) [Re: drakino]
g_attrill
old hand

Registered: 14/04/2002
Posts: 1172
Loc: Hants, UK
Quote:

I'm kinda brute forcing myself to learn C# in a method that I teach myself what I need at a certain point in this basic program. Most things I have found answers for, but this particular situation is difficult to search books/manuals/online for an answer.

That's what I'm doing at the moment - I've been handed a large website front-end to a subscription-type service and I need to wrap it in a CSS2 design that has been done over here, add some affiliate marketing code that I have done myself, then hand it back for testing and then oversee the implimentation.

The snag is that the bulk of the project has been coded in India - the code itself is quite good but not "intelligent" code, there's a lot of things that I have thought are long-winded or needlessly complex because the coder didn't consider think about it enough. It was probably quicker to code it that way bit it wasn't the best solution in the long run. Unfortunately I haven't got time to fix it nor the patience to explain it to those concerned, so it's going on my "to-do" list.

However despite not knowing any C# or even C++ more than Hello World I have found it pretty easy to work with and learn.

Gareth

Top
#257890 - 08/06/2005 20:20 Re: Contents of variables to modify controls (C#) [Re: JeffS]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Quote:
However, if you are just learning this might be a bit advanced. It depends on your background with oop and creating custom controls.


So far, this project is my experience with OOP, beyond the minor hints of OOP scripting languages like PHP try to expose. Most of my programming in the past has been console apps that take a bit of input, play around with some files, and maybe spit out input. I've wanted to learn OOP for a while, but never really had a need for a program until recently.

I'll take a better look at the suggestions here in the next week or so and continue to tinker with my code. I've already done a major restructuring once, and I have a feeling I will be doing so again with this current milestone I am trying to complete. I think this one will be a good excuse to stop using the Visual Studio GUI to design the form and start doing more myself to have more control over it.

If your interested in looking at what I am doing, the program is at http://grs.miniinfo.net . It is very much a specialized application for my situation at this point, but I am intentionally expanding it to be more general purpose to test and expand my skills. My ultimate goal for it is to have it powered by an XML document of some sort to help prevent a function specific to every game I am changing, since in many cases, games store resolutions in a very similar way to each other. My initial test of XML code in .Net came when I added the ability to check for newer versions online.

Top
#257891 - 08/06/2005 20:35 Re: Contents of variables to modify controls (C#) [Re: drakino]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Cool- I'll definitely take a look!
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257892 - 09/06/2005 06:41 Re: Contents of variables to modify controls (C#) [Re: drakino]
ninti
old hand

Registered: 28/12/2001
Posts: 868
Loc: Los Angeles
Quote:
I want to have a situation where there can be a maximum of 8 groupboxes in the form, but if they are not all needed, less will be shown.


I have done almost this exact same thing, and I actually ended up making almost exactly what JeffS suggested in his fourth post. I had a line class that contained all the controls for one line, and did all the internal formatting for that line. I had another class that contained in arraylist of line classes, and handled the (minor) formatting between lines. Each instance of the line keeps track of the controls of that line, adding them on the constructor and removing them when requested (since you can't rely on the destructor in DotNet.) It's fairly easy to do this way, the only hard part in my case was lines could add or remove the line below them, which required a lot of message passing which shouldn't be an issue for you.
_________________________
Ninti - MK IIa 60GB Smoke, 30GB, 10GB

Top
#257893 - 04/07/2005 04:12 Re: Contents of variables to modify controls (C#) [Re: JeffS]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Ok, I thought I was close, but I just can't work around this bug. This should be all the relevant code that I am working on:

Code:

public string [] GameName =
new string [] { "Game 0","Game 1","Game 2","Game 3","Game 4","Game 5","Game 6","Game 7","Game 8"};

public int[,] resarea =
new int[,] { {8, 32}, {272, 32}, {536, 32}, {8, 112}, {272, 112}, {536, 112}, {8, 192}, {272, 192}, {536, 192}};

public int GuiItem = 0;

public System.Windows.Forms.GroupBox [] gBox = new System.Windows.Forms.GroupBox[9];

public void SpawnGuiItems(int gamenum)
{
try
{
// Add the groupbox to the form
this.Controls.Add(gBox[GuiItem]);
// Set intial properties for the groupbox
gBox[GuiItem].Size = new System.Drawing.Size(256, 72);
gBox[GuiItem].Location = new System.Drawing.Point(resarea[GuiItem,0],resarea[GuiItem,1]);
gBox[GuiItem].Text = GameName[GuiItem];
gBox[GuiItem].Tag = GuiItem;
gBox[GuiItem].Click += new System.EventHandler(ClickHandler);
GuiItem++;
}
catch (Exception ex)
{
// Let the user know what went wrong.
MessageBox.Show(ex.ToString(), "Exception generating the GUI", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

public void ClickHandler(Object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("You have clicked groupbox " +
((System.Windows.Forms.GroupBox) sender).Tag.ToString());
}



The code is compiling fine, but when it tires to run, it has an exception of "System.NullReferenceException: Object reference not set to an instance of an object." The line it errors on is gBox[GuiItem].Size. Any ideas on this? I have a feeling I am close, but just can't work my way around this bug. I know this is likely a basic OO issue, but I'm still trying to get all the concepts down.

Top
#257894 - 04/07/2005 04:21 Re: Contents of variables to modify controls (C#) [Re: drakino]
andy
carpal tunnel

Registered: 10/06/1999
Posts: 5916
Loc: Wivenhoe, Essex, UK
While you are creating the array for the group boxes, you are never creating the boxes themselves. You need this:

// Create the group box object
gBox[GuiItem] = new System.Windows.Forms.GroupBox();
// Add the groupbox to the form
this.Controls.Add(gBox[GuiItem]);
_________________________
Remind me to change my signature to something more interesting someday

Top
#257895 - 04/07/2005 04:35 Re: Contents of variables to modify controls (C#) [Re: drakino]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Quote:
I know this is likely a basic OO issue
It is . . .
Quote:
but I'm still trying to get all the concepts down.
And this is probably the best way learn- by just doing it!

To answer your question, this line:
Code:
public System.Windows.Forms.GroupBox [] gBox = new System.Windows.Forms.GroupBox[9];


creates 9 REFERENCES to GroupBox objects, but it doesn't actually create the OBJECTS themselves yet. So what you have is essentially:

Code:

gBox[0]=null
gBox[1]=null
gBox[2]=null
...



Thus the line
Code:
this.Controls.Add(gBox[GuiItem]);

Works fine because the "Add" cann accept null as a value.

However, this line
Code:
gBox[GuiItem].Size = new System.Drawing.Size(256, 72);

fails because you are trying to reference an attribute ("size") of an object that hasn't been created yet.

To fix the problem, you'll have to create 9 boxes with the "new" constructor:
Code:
gBox[0] = new System.Windows.Forms.GroupBox();

gBox[1] = new System.Windows.Forms.GroupBox();
...

Now, I didn't actually compile this (don't have c# available at the moment), so the constructor may actually requires some variables, but you get the idea.

I should also mention that you can use your loop to create each of the objects rather than doing them the way I illustrated above. I was just trying to point out the concept..

In short, when you create a variable with [] after the type, it creates an array of that type, but doesn't actually create any objects. You have to create them individually.


Edited by JeffS (04/07/2005 04:37)
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257896 - 04/07/2005 04:39 Re: Contents of variables to modify controls (C#) [Re: andy]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Ah, caught me typing! I think your answer was a bit more direct!
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top
#257897 - 04/07/2005 04:49 Re: Contents of variables to modify controls (C#) [Re: JeffS]
andy
carpal tunnel

Registered: 10/06/1999
Posts: 5916
Loc: Wivenhoe, Essex, UK
Ah, but you reply was more informative...
_________________________
Remind me to change my signature to something more interesting someday

Top
#257898 - 10/07/2005 05:13 Re: Contents of variables to modify controls (C#) [Re: JeffS]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Thanks again for both your help on this. I got pased that speed bump, and have come away with a better understanding of OO (and why it is so nice). I have managed to rewrite a good portion of the program to use the new methods, shaving off about 300 lines of code that Visual Studio was generating for me automaticially.

Now to get the 0.4 milestone wrapped up in the next few days and move on to 0.5.


Edited by drakino (10/07/2005 05:57)

Top
#257899 - 10/07/2005 09:52 Re: Contents of variables to modify controls (C#) [Re: drakino]
JeffS
carpal tunnel

Registered: 14/01/2002
Posts: 2858
Loc: Atlanta, GA
Glad to help!
_________________________
-Jeff
Rome did not create a great empire by having meetings; they did it by killing all those who opposed them.

Top