473,756 Members | 7,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.n oTactons(); j++)
{
// create a radio button
RadioButton rb= new RadioButton();
rb.CreateContro l();
Console.WriteLi ne("going to create a radio button");
rb= createRadioButt on(j,yVal,tacto nMids);
gb.Controls.Add (rb);
Console.WriteLi ne("created radio button {0} and added it to the
controls of the group box",j);
Console.WriteLi ne("rb location= {0}",rb.Locatio n.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.Coun t;
Console.WriteLi ne("no group boxes= {0}",count);
// loop round group boxes
foreach (Object o in groupBoxes)
{
GroupBox g = (GroupBox)o;
Console.WriteLi ne("location of GroupBox=
{0}",g.Location .ToString());
int temp= g.Controls.Coun t;
this.Controls.A dd(g);
Console.WriteLi ne("group boxes.controls= {0}",g.Controls .Count);
// loop round radiobuttons
int j = 0;
foreach (Control c in g.Controls)
{
Console.WriteLi ne("going to draw a radio button {0}",j);
Console.WriteLi ne("RadioButto n location=
{0}",((RadioBut ton)c).Location );
this.Controls.A dd((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.WriteLi ne("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.NullRef erenceException ' occurred
in SimpleSimonTact ile.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.NullRefe renceException: Object reference
not set to an instance of an object.
at SimpleSimonTact ile.UserGuessUI .drawSelectors( ) in c:\documents
and settings\mcparl aj\my documents\degre e course\4th
year\project\so ftware\simplesi montactile\simp lesimontactile\ userguessui.cs: line
345
at SimpleSimonTact ile.UserGuessUI ..ctor() in c:\documents and
settings\mcparl aj\my documents\degre e course\4th
year\project\so ftware\simplesi montactile\simp lesimontactile\ userguessui.cs: line
96
at SimpleSimonTact ile.UserGuessUI .ShowForm() in c:\documents and
settings\mcparl aj\my documents\degre e course\4th
year\project\so ftware\simplesi montactile\simp lesimontactile\ 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 2926
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.CreateContro l();
Console.WriteLi ne("going to create a radio button");
rb= createRadioButt on(j,yVal,tacto nMids);
gb.Controls.Add (rb);

Why do you create a RadioButton only to replace it with the result of
calling "createRadioBut ton" on the fourth line? Whatever was in rb
before (a reference to the RadioButton created on the first line) will
be replaced by whatever createRadioButt on 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.WriteLi ne("going to
draw a radio button {0}",j);
Console.WriteLi ne("RadioButto n
location=
{0}",((RadioBut ton)c).Location );

this.Controls.A dd((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.WriteLi ne("going to
draw a radio button {0}",j);
RadioButton rb = c as
RadioButton;
if (rb == null)
{
Console.WriteLi ne("Got a
{0} instead of a RadioButton!", c.GetType());
}
else
{

Console.WriteLi ne("RadioButto n location=
{0}",rb.Locatio n);
this.Controls.A dd(rb);
}
j++;
}

But then I don't understand why you're doing this.Controls.A dd(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;

"createRadioBut ton" 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:
"createRadioBut ton" 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. createRadioButt on 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.n oTactons()];
Size gbSize= new Size();
calculatePositi ons(out tactonMids, out gbSize);

// generate a group box for every tacton in the sequence
for (int i= 0; i < sqg.sequenceLen gth(); 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.A dd(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.n oTactons(); j++)
{
// create a radio button
RadioButton rb= new RadioButton();
// set rb's text, name, size, location
setRadioButton( j,yVal,tactonMi ds, 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
1430
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> function setAllAccounts(value) { for(i = 0 ; i < document.forms.accounts.length; i++ ) { document.forms.accounts.checked = value; } }
37
2319
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 doesn't like the
5
6702
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 how do i filter data? i mean lets say i wanna filter the NAME field and get all the names starting with
27
2561
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: Mensaje de error del analizador: We can't load the type 'JULIAN.Global'.
2
1243
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() The following code assign the values of the last lign to all elements of the collection: EntSor.Définit("PAAE", "bar", 0, True) : colEntSor.Add(EntSor)
8
7191
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, Decimal, ....) and I must to send them as null, and not their default value. It is possible? Is there any trick to succeed it? Thanks in advance,
1
1832
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 = new CultureInfo("es"); String str = gStrings.GetString("Username", ci); I have two assemblies: one is the fallback String.resx and the other one is Strings.es.resx
8
1801
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 with statement is indeed bad. My initial thoughts on with were based on: http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/ I built some examples to investigate - and later try to "eliminate"
33
2447
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
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10040
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.