referencing a control from a string
-
I have a number of PictureBox controls that show a Street Stop Light. Each Picturebox control is controled by two radio buttons for a total of 8 picturebox control and 16 radio buttons. One for Run (turns the Stop Light Green) One for Stop (turns the light red). I am storing the name of the PictureBox control in the tag of it respective radio buttons. Can I take this string and use it to reference the Picturebox control and change the image? Here is me code:
private void OnCheckedChange(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } } private void OnLoadPictureImage(String picBox, Boolean ObjectStatus) { try { PictureBox pb = new PictureBox(); pb = (PictureBox)this.Controls\[picBox\]; if (ObjectStatus) { Stream s = File.Open("../images/traffic-light-green.jpg", FileMode.Open); Image temp = Image.FromStream(s); s.Close(); pb.Image = temp; } else { Stream s = File.Open("../images/traffic-light-red.jpg", FileMode.Open); Image temp = Image.FromStream(s); s.Close(); pb.Image = temp; } } catch (Exception err) { MessageBox.Show("Error finding image: " + err.Message); } }
Is this not possible? Thanks Tom
Tom Wright tawright915@gmail.com
-
I have a number of PictureBox controls that show a Street Stop Light. Each Picturebox control is controled by two radio buttons for a total of 8 picturebox control and 16 radio buttons. One for Run (turns the Stop Light Green) One for Stop (turns the light red). I am storing the name of the PictureBox control in the tag of it respective radio buttons. Can I take this string and use it to reference the Picturebox control and change the image? Here is me code:
private void OnCheckedChange(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } } private void OnLoadPictureImage(String picBox, Boolean ObjectStatus) { try { PictureBox pb = new PictureBox(); pb = (PictureBox)this.Controls\[picBox\]; if (ObjectStatus) { Stream s = File.Open("../images/traffic-light-green.jpg", FileMode.Open); Image temp = Image.FromStream(s); s.Close(); pb.Image = temp; } else { Stream s = File.Open("../images/traffic-light-red.jpg", FileMode.Open); Image temp = Image.FromStream(s); s.Close(); pb.Image = temp; } } catch (Exception err) { MessageBox.Show("Error finding image: " + err.Message); } }
Is this not possible? Thanks Tom
Tom Wright tawright915@gmail.com
why not just store a reference to the
PictureBox
in theTag
? [edit] I may be making assumptions based on the limited scope of the code snippet, but shouldn't thisOnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
instead read
OnLoadPictureImage(rb.Tag.ToString(), rb.Checked);
Why create a new
PictureBox
inside ofOnLoadPictureImage
here:PictureBox pb = new PictureBox();
pb
is immediately overwritten to anotherPictureBox
so wouldn't it be better to set it tonull
and not waste the time or resources creating the throw-away object?
Last modified: 16mins after originally posted --
:Badger:
-
why not just store a reference to the
PictureBox
in theTag
? [edit] I may be making assumptions based on the limited scope of the code snippet, but shouldn't thisOnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
instead read
OnLoadPictureImage(rb.Tag.ToString(), rb.Checked);
Why create a new
PictureBox
inside ofOnLoadPictureImage
here:PictureBox pb = new PictureBox();
pb
is immediately overwritten to anotherPictureBox
so wouldn't it be better to set it tonull
and not waste the time or resources creating the throw-away object?
Last modified: 16mins after originally posted --
:Badger:
I did it that way because when I reference the PictureBox.image property I received and error that the object had not been instantiated. Either way it's still null...almost like it cannot find it. Oh...and I did have it like this: OnLoadPictureImage(rb.Tag.ToString(), rb.Checked); Just doing some debugging when I thought I'd post this to the forum and I forgot to set it back like I had it. Thanks for the help. Tom
Tom Wright tawright915@gmail.com
-
I did it that way because when I reference the PictureBox.image property I received and error that the object had not been instantiated. Either way it's still null...almost like it cannot find it. Oh...and I did have it like this: OnLoadPictureImage(rb.Tag.ToString(), rb.Checked); Just doing some debugging when I thought I'd post this to the forum and I forgot to set it back like I had it. Thanks for the help. Tom
Tom Wright tawright915@gmail.com
The exact error was: Object reference not set to an instance of an object. T
Tom Wright tawright915@gmail.com
-
The exact error was: Object reference not set to an instance of an object. T
Tom Wright tawright915@gmail.com
where? and in response to your original question,
pb = (PictureBox)this.Controls[picBox];
should work fine just fine as long as
this.Controls[picBox]
actually is aPictureBox
. Doing a safe cast (this.Controls[picBox] as PictureBox
) and then checking fornull
would be a safer way to do it.:Badger:
-
where? and in response to your original question,
pb = (PictureBox)this.Controls[picBox];
should work fine just fine as long as
this.Controls[picBox]
actually is aPictureBox
. Doing a safe cast (this.Controls[picBox] as PictureBox
) and then checking fornull
would be a safer way to do it.:Badger:
pb.Image = temp; <----- Here....when I try and set the image.
Tom Wright tawright915@gmail.com
-
pb.Image = temp; <----- Here....when I try and set the image.
Tom Wright tawright915@gmail.com
-
I have a number of PictureBox controls that show a Street Stop Light. Each Picturebox control is controled by two radio buttons for a total of 8 picturebox control and 16 radio buttons. One for Run (turns the Stop Light Green) One for Stop (turns the light red). I am storing the name of the PictureBox control in the tag of it respective radio buttons. Can I take this string and use it to reference the Picturebox control and change the image? Here is me code:
private void OnCheckedChange(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } } private void OnLoadPictureImage(String picBox, Boolean ObjectStatus) { try { PictureBox pb = new PictureBox(); pb = (PictureBox)this.Controls\[picBox\]; if (ObjectStatus) { Stream s = File.Open("../images/traffic-light-green.jpg", FileMode.Open); Image temp = Image.FromStream(s); s.Close(); pb.Image = temp; } else { Stream s = File.Open("../images/traffic-light-red.jpg", FileMode.Open); Image temp = Image.FromStream(s); s.Close(); pb.Image = temp; } } catch (Exception err) { MessageBox.Show("Error finding image: " + err.Message); } }
Is this not possible? Thanks Tom
Tom Wright tawright915@gmail.com
private void Form1_Load(object sender, EventArgs e)
{
for (int x = 0; x < this.Controls.Count; ++x)
{
for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
{
if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
{
RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];
_rdbtn.CheckedChanged += new EventHandler(_rdbtn_CheckedChanged);
}
}
}
}void _rdbtn_CheckedChanged(object sender, EventArgs e)
{
for (int x = 0; x < this.Controls.Count; ++x)
{
for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
{
if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
{
// Founded the radio button
RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];if (\_rdbtn.Checked == true) { for (int z = 0; z < this.Controls\[x\].Controls.Count; ++z) { if (this.Controls\[x\].Controls\[z\].GetType() == typeof(PictureBox)) { // Change to green PictureBox \_pic = (PictureBox)this.Controls\[x\].Controls\[z\]; if (\_pic.Tag == "Hi") { // this is the one you want to work with... } } } } } } }
}
Take note... if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } will always execute irrelevant true or false. i assume one is 'Unchecked' This code should help only if you have the radio / image / etc. on panels. remove the 2nd layer of loops if you not using panels... Regards, X
Yeee :cool:
modified on Tuesday, May 19, 2009 5:17 PM
-
pb.Image = temp; <----- Here....when I try and set the image.
Tom Wright tawright915@gmail.com
modification to my previous statement: in order for this to work:
pb = (PictureBox)this.Controls[picBox];
the picture box needs to be parented by the main form. If it exists on a child panel or a tab control or something like that then it won't exist in the main forms control collection. You'll have to recursively search the
this.Control
objects to find it. Like I said before, why not just store the picture box itself in the tag of the radio button? Then you'd havePictureBox pb = rb.Tag as PictureBox;
and you'd be done.
:Badger:
-
private void Form1_Load(object sender, EventArgs e)
{
for (int x = 0; x < this.Controls.Count; ++x)
{
for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
{
if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
{
RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];
_rdbtn.CheckedChanged += new EventHandler(_rdbtn_CheckedChanged);
}
}
}
}void _rdbtn_CheckedChanged(object sender, EventArgs e)
{
for (int x = 0; x < this.Controls.Count; ++x)
{
for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
{
if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
{
// Founded the radio button
RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];if (\_rdbtn.Checked == true) { for (int z = 0; z < this.Controls\[x\].Controls.Count; ++z) { if (this.Controls\[x\].Controls\[z\].GetType() == typeof(PictureBox)) { // Change to green PictureBox \_pic = (PictureBox)this.Controls\[x\].Controls\[z\]; if (\_pic.Tag == "Hi") { // this is the one you want to work with... } } } } } } }
}
Take note... if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } will always execute irrelevant true or false. i assume one is 'Unchecked' This code should help only if you have the radio / image / etc. on panels. remove the 2nd layer of loops if you not using panels... Regards, X
Yeee :cool:
modified on Tuesday, May 19, 2009 5:17 PM
I see. I did not know that I had to iterate through all of the controls. Currently the Radio buttons are in group fields. I have 6 sets of Run/Stop radiobuttons on my form in 6 group fields. I ran into a problem where they were not in group fields and then only one radiobutton was allowed to be selected from the rest. Thanks for the help. Tom
Tom Wright tawright915@gmail.com