473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

inexplicable problem with GroupBox(es)

Hi all,

my program is trying to add group boxes with radio buttons at run time.
While at one point it was able to draw the group boxes without the
radio buttons, now it encounters problems just getting the radiobuttons
out of the group box.

/// area where radio buttons added to groupbox

/// this code is wrapped in another loop creating group boxes
for (int j= 0; j < TactonManager.noTactons(); j++)
{
// create a radio button
RadioButton rb= new RadioButton();
rb.CreateControl();
Console.WriteLine("going to create a radio button");
rb= createRadioButton(j,yVal,tactonMids);
gb.Controls.Add(rb);
Console.WriteLine("created radio button {0} and added it to the
controls of the group box",j);
Console.WriteLine("rb location= {0}",rb.Location.ToString());
} // radio button for

// update the current bottom position before drawing next group box
bottomY= gb.Location.Y + gb.Size.Height;

grpBxs.Add(gb);
/// end of enclosing loop

// groupBoxes is a member of the class
groupBoxes= grpBxs;
/// area where radio buttons accessed from groupBox

int count = groupBoxes.Count;
Console.WriteLine("no group boxes= {0}",count);
// loop round group boxes
foreach (Object o in groupBoxes)
{
GroupBox g = (GroupBox)o;
Console.WriteLine("location of GroupBox=
{0}",g.Location.ToString());
int temp= g.Controls.Count;
this.Controls.Add(g);
Console.WriteLine("group boxes.controls= {0}",g.Controls.Count);
// loop round radiobuttons
int j = 0;
foreach (Control c in g.Controls)
{
Console.WriteLine("going to draw a radio button {0}",j);
Console.WriteLine("RadioButton location=
{0}",((RadioButton)c).Location);
this.Controls.Add((RadioButton)c);
j++;
}
/// <debug>
/// note: this only seems to try to draw radio buttons 0,2 (at
posns 45, 127)
/// it misses out those at 86 and 168, and still does with a normal
loop.
/// plus it doesn't actually draw them
/// </debug>
Console.WriteLine("exited the loop for the radio buttons");
}

it only finds two of my radio buttons (on the 3rd (index 2) I get an
error.

Below is all of the relevant output from the program and compiler

going to create a radio button
creating radio buttons
created radio button 0 and added it to the controls of the group box
rb location= {X=45,Y=287}
going to create a radio button
creating radio buttons
created radio button 1 and added it to the controls of the group box
rb location= {X=86,Y=287}
going to create a radio button
creating radio buttons
created radio button 2 and added it to the controls of the group box
rb location= {X=127,Y=287}
going to create a radio button
creating radio buttons
created radio button 3 and added it to the controls of the group box
rb location= {X=168,Y=287}
no group boxes= 1
location of GroupBox= {X=16,Y=267}
group boxes.controls= 4
going to draw a radio button 0
RadioButton location= {X=45,Y=287}
going to draw a radio button 1
RadioButton location= {X=127,Y=287}
going to draw a radio button 2
An unhandled exception of type 'System.NullReferenceException' occurred
in SimpleSimonTactile.exe

Additional information: Object reference not set to an instance of an
object.

The thread '<No Name>' (0xab8) has exited with code 0 (0x0).

Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.
at SimpleSimonTactile.UserGuessUI.drawSelectors() in c:\documents
and settings\mcparlaj\my documents\degree course\4th
year\project\software\simplesimontactile\simplesim ontactile\userguessui.cs:line
345
at SimpleSimonTactile.UserGuessUI..ctor() in c:\documents and
settings\mcparlaj\my documents\degree course\4th
year\project\software\simplesimontactile\simplesim ontactile\userguessui.cs:line
96
at SimpleSimonTactile.UserGuessUI.ShowForm() in c:\documents and
settings\mcparlaj\my documents\degree course\4th
year\project\software\simplesimontactile\simplesim ontactile\userguessui.cs:line
396

as you may notice that while only two radio buttons are able to be
"drawn" (at x posns 45 and 127) those at posns 16 and 168 are missed
out. Why?

Please help

John

Jan 10 '06 #1
8 2896
It's pretty difficult to diagnose what's going on here, because there
are some odd bits in this code that I don't really understand. Maybe if
I point out some oddities you'll either find your problem or help me
understand.

First off, you have

RadioButton rb= new RadioButton();
rb.CreateControl();
Console.WriteLine("going to create a radio button");
rb= createRadioButton(j,yVal,tactonMids);
gb.Controls.Add(rb);

Why do you create a RadioButton only to replace it with the result of
calling "createRadioButton" on the fourth line? Whatever was in rb
before (a reference to the RadioButton created on the first line) will
be replaced by whatever createRadioButton returns. I really hope that's
a RadioButton, and I hope that it's never null....

Down farther, the code seems a little too sure of itself:

foreach (Control c in g.Controls)
{
Console.WriteLine("going to
draw a radio button {0}",j);
Console.WriteLine("RadioButton
location=
{0}",((RadioButton)c).Location);

this.Controls.Add((RadioButton)c);
j++;
}

Casting every control to a RadioButton? Brave soul. What if some other
control ends up inside one of your GroupBoxes. This would be safer:

foreach (Control c in g.Controls)
{
Console.WriteLine("going to
draw a radio button {0}",j);
RadioButton rb = c as
RadioButton;
if (rb == null)
{
Console.WriteLine("Got a
{0} instead of a RadioButton!", c.GetType());
}
else
{

Console.WriteLine("RadioButton location=
{0}",rb.Location);
this.Controls.Add(rb);
}
j++;
}

But then I don't understand why you're doing this.Controls.Add(rb);?!?
You're removing the radio buttons from their group boxes and putting
them directly on the parent container?

Jan 11 '06 #2
thanks for your reply,

well I got some help from a friend who said that I needed to specify
the size of the radio buttons to fix the problem: and it did!

In answer to your queries;

"createRadioButton" is a poor name for a method which sets the radio
button's name, location etc. While it does make a call to new
RadioButton() it does not use createControl().

as for the danger that someone may add a different type of control,
while indeed I should have checked for this, I haven't as this is a
small project and only I will edit the code. Also all the controls and
any way to access them are private. None-the-less I will put this
check in as I should have.

finaly, it seems that I need to add the radio buttons to the group box
so that when I check one, the rest in that groupBox are unchecked. But
after doing this, if I do not add them to the form directly, they are
not displayed when I add the groupBox to the form. Therefore I must
add the radio buttons to both the groupBox and the Form, and add the
groupBox to the form.

hope this helps anyone else with this bewildering problem.

Jan 11 '06 #3

jo************@googlemail.com wrote:
"createRadioButton" is a poor name for a method which sets the radio
button's name, location etc. While it does make a call to new
RadioButton() it does not use createControl().
OK, but the code still doesn't make sense. createRadioButton creates a
whole new radio button and then you assign that to rb, throwing away
the radio button you created a few lines above. There's something wrong
there.
finaly, it seems that I need to add the radio buttons to the group box
so that when I check one, the rest in that groupBox are unchecked. But
after doing this, if I do not add them to the form directly, they are
not displayed when I add the groupBox to the form. Therefore I must
add the radio buttons to both the groupBox and the Form, and add the
groupBox to the form.


No, this isn't true. You do _not_ need to add the radio buttons to both
the GroupBox and the Form. At least, not in WinForms. I don't program
in ASP.NET, but I would assume that the same rule applies there. If
you're having a problem with things not appearing correctly when you
add the radio buttons to the GroupBox and the GroupBox to the Form,
then there is some other problem cropping up there.

If you add the RadioButtons to the Form directly, then they will be
removed from the GroupBoxes. A control can have only one containing
control, or Parent. A RadioButton cannot be simultaneously directly
contained within a GroupBox and a Form.

So, something else is happening in your program. Unfortunately I don't
have enough information to tell you what might be wrong.

Jan 11 '06 #4
thanks for the help, but I'm still having problems

yea I've noticed, as you say that adding to the group box and then to
the form doesn't work. unfortunately if I do not add the radio buttons
to the form, I can't see them at all.

ok, here's an improved version of the above code;

private void createSelectors()
{
// calculate the neccessary positions
// tactonMids contains the x positions for each radio button
int[] tactonMids= new int[TactonManager.noTactons()];
Size gbSize= new Size();
calculatePositions(out tactonMids, out gbSize);

// generate a group box for every tacton in the sequence
for (int i= 0; i < sqg.sequenceLength(); i++)
{
// create a group box
GroupBox gb= new GroupBox();
setGroupBox(i, gbSize, ref gb); // sets the size, name, text and
location of gb
this.Controls.Add(gb);
// generate radio buttons for every tacton in the TactonManager

// this is the y value for all radio buttons in this group box
int yVal= (gb.Size.Height/2) + gb.Location.Y;

// noTactons-1 is the number of radio buttons needed. same as
tactonMids array

for (int j= 0; j < TactonManager.noTactons(); j++)
{
// create a radio button
RadioButton rb= new RadioButton();
// set rb's text, name, size, location
setRadioButton(j,yVal,tactonMids, ref rb);
gb.Controls.Add(rb);
} // radio button for

// update the current bottom position before drawing next group box
bottomY= gb.Location.Y + gb.Size.Height;

// groupBoxes is a static variable in the class, i need it
later
groupBoxes.Add(gb);
} // groupBox for
}

as mentioned, after doing this, the groupBoxes show up but not the
radio buttons.

Jan 16 '06 #5
If I had to hazard a quick guess (and I do right now), I'd say that the
problem is your x,y coordinates for the radio buttons.

Do you realize that if you're adding radio buttons to a group box, then
the x,y coordinates of the radio button are relative to the top left of
the group box? That is, you're adding in the group box's coordinate
system, not the form's coordinate system.

So, if your group box is at location 200, 400 on you form, and the
group box is 500 by 100 pixels, you add radio buttons starting at, say,
10, 10 in the group box, and then add the group box at 200, 400 in the
form. You don't add the radio buttons at 210, 410 in the group box, or
they'll be off the bottom of it.

I predict that if you adjust your radio buttons accordingly, they will
show up properly.

Jan 17 '06 #6
Do you realize that if you're adding radio buttons to a group box, then
the x,y coordinates of the radio button are relative to the top left of
the group box?

-> no I didn't d'oh! thanks very much! it works!

Jan 17 '06 #7
Just as a hint to make this sort of thing easier in the future.

Whenever I have to generate controls in code and add them to a panel,
user control, form, whatever... I use the Visual Studio Designer first.

Yeah, I know: the Designer can't write your code for you because you
need to calculate things at runtime. However, you _can_ mock up a quick
example in the Designer and then go and look at the code it generated.
You can even copy and paste the generated code into a loop (or
whatever) and then butcher the Designer-generated code to your
purposes.

I find that writing WinForms code from scratch, by hand, is a
nightmare. There are so many little details that you need to take care
of... it can get ugly. That's why I prefer to do a quick mock-up in the
Designer and then crib off _its_ code... because I know that at least
what the Designer generated will work.

Jan 17 '06 #8
thanks Bruce, I'll keep that in mind for the future.

Jan 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wendy S | last post by:
I have this working, but I don't think it's done efficiently or the best way: <a href="javascript:setAllAccounts(true)">check all</a> <a href="javascript:setAllAccounts(false)">clear all</a> ...
37
by: Patrik Huber | last post by:
Hello! I got the following Code in Assembler (NASM), which prints out "5" in realmode: mov ax, 0xB800 mov es, ax mov byte , '5' I want to do the same in gcc now, but I'm stuck. GCC...
5
by: josephrthomas | last post by:
hi.. i am using ASP.NET with VB.NET to connect to a MS Access database... can someone pls tell me how do i make the sql statement to insert a new record into the existing table pls?? also...
27
by: Javier Martinez | last post by:
Hi I have asp application in a machine with a virtual directory referring a shared directory in another machine When I try to load any aspx page of my portal I get the following error: ...
2
by: Bernard Bourée | last post by:
I have defined a Class called EntSor (see code ) with a procedure Sub Définit which assign the values of this class. Then I have defined an Arraylist with Dim colEntSor As New ArrayList() ...
8
by: Marc | last post by:
Hi! I'm calling a web service using C# and a wrapper class generated by wsdl tool. The web service specification contains some nillable parameters which types are Value Types in .NET (long, int,...
1
by: Nader | last post by:
I have the next simple code and it doesn't work: ResourceManager gStrings = new ResourceManager("MyApp.Strings", Assembly.GetExecutingAssembly()); //Get the user's language. CultureInfo ci =...
8
by: Rasmus Kromann-Larsen | last post by:
The With Conundrum I'm currently writing a master thesis on (preparations for) static analysis of JavaScript, and after investigating the with statement, it only even more evident to me that the...
33
by: =?Utf-8?B?RE9UTkVUR1VZ?= | last post by:
Hello, In vb.net there is a with statement, Is there are similar constructor in c#?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.