After adding label to picturebox contols, events dont fire - C#












0















I'm having a weird behavior with a class i created inheriting from PictureBox;



its a class made to behave like a button (basiclly what i care about is the mouse enter, mouse leave, mouse down, mouse up events).



everything works perfectly until i add the TextLabel (code below) to the control of my customed class. it shows the label, centered and everything as i wanted but the events i mentioned before (and every other event in that matter) becomes disabled, for some reason it wont fire them.



would like to know what is the reason for that behavior and if there any fix for it.



public class RoundedButton : PictureBox
{
private readonly Image r_BasicImage = RoundedButtonCheck2.Properties.Resources.basicRoundedButtonIcon as Image;
private readonly Image r_HoverImage = RoundedButtonCheck2.Properties.Resources.hoverRoundedButtonIcon as Image;
private readonly Image r_ClickImage = RoundedButtonCheck2.Properties.Resources.clickRoundedButtonIcon as Image;

private string m_Text;
private Font m_Font;
public Color m_TextColor;
private Label LabelText = new Label();


public RoundedButton()
{
this.Width = 130;
this.Height = 40;
this.Image = r_BasicImage;
this.BackColor = Color.Transparent;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.MouseDown += RoundedButton_MouseDown;
this.MouseUp += RoundedButton_MouseUp;
this.MouseEnter += RoundedButton_MouseEnter;
this.MouseLeave += RoundedButton_MouseLeave;
LabelText.Font = ButtonFont;
ButtonTextColor = Color.Black;
//PROBLEMATIC CODE HERE:
***********this.Controls.Add(LabelText);***************
}

public Color ButtonTextColor
{
get
{
return m_TextColor;
}
set
{
m_TextColor = value;
LabelText.ForeColor = m_TextColor;
}
}
public Font ButtonFont
{
get
{
if (m_Font == null)
{
m_Font = new Font("Calibri Light", 12);
}
return m_Font;
}
set
{
m_Font = value;
LabelText.Font = ButtonFont;
adjustLabel();
}
}

public string ButtonText
{
get
{
return m_Text;
}
set
{

m_Text = value;
LabelText.Text = m_Text;
adjustLabel();
}
}
private void adjustLabel()
{
const int MARGIN = 10;
LabelText.AutoSize = true;//needed for autosize calculation of the label;
Size newSize = new Size(LabelText.Size.Width, LabelText.Size.Height); ;
LabelText.AutoSize = false;//after auto-calculated size of the label, set to false in order for centering label works well.
this.MinimumSize = newSize;
this.Size = new Size(newSize.Width + MARGIN, newSize.Height + MARGIN);
LabelText.TextAlign = ContentAlignment.MiddleCenter;
LabelText.Dock = DockStyle.Fill;
}

private void RoundedButton_MouseLeave(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_BasicImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseEnter(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_HoverImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseUp(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_BasicImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}

private void RoundedButton_MouseDown(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_ClickImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}
}


thanks in advanced, tomer.










share|improve this question




















  • 1





    Quick guess: LabelText is gobbling up the events before RoundedButton can see them.

    – MikeH
    Nov 12 '18 at 23:50











  • if so, any work-around or fix?

    – tom01
    Nov 12 '18 at 23:57






  • 1





    Try subscribing LabelText to those same events, see if it works then.

    – MikeH
    Nov 13 '18 at 0:00











  • yea, it does works (from within the class). your guess was correct, yet its not a fix, first because it feels a bit upside-down, second because components that will use RoundedButton wont be able to subscribe to its events since the TextLabel is private and i dont wont it to be public...

    – tom01
    Nov 13 '18 at 0:16













  • You could just call the OnMouseDown method from the event of your label which will fire the MouseDown event that is public. I'm not sure there's a way to prevent the LabelText from eating up the events (I could be wrong though).

    – MikeH
    Nov 13 '18 at 0:22


















0















I'm having a weird behavior with a class i created inheriting from PictureBox;



its a class made to behave like a button (basiclly what i care about is the mouse enter, mouse leave, mouse down, mouse up events).



everything works perfectly until i add the TextLabel (code below) to the control of my customed class. it shows the label, centered and everything as i wanted but the events i mentioned before (and every other event in that matter) becomes disabled, for some reason it wont fire them.



would like to know what is the reason for that behavior and if there any fix for it.



public class RoundedButton : PictureBox
{
private readonly Image r_BasicImage = RoundedButtonCheck2.Properties.Resources.basicRoundedButtonIcon as Image;
private readonly Image r_HoverImage = RoundedButtonCheck2.Properties.Resources.hoverRoundedButtonIcon as Image;
private readonly Image r_ClickImage = RoundedButtonCheck2.Properties.Resources.clickRoundedButtonIcon as Image;

private string m_Text;
private Font m_Font;
public Color m_TextColor;
private Label LabelText = new Label();


public RoundedButton()
{
this.Width = 130;
this.Height = 40;
this.Image = r_BasicImage;
this.BackColor = Color.Transparent;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.MouseDown += RoundedButton_MouseDown;
this.MouseUp += RoundedButton_MouseUp;
this.MouseEnter += RoundedButton_MouseEnter;
this.MouseLeave += RoundedButton_MouseLeave;
LabelText.Font = ButtonFont;
ButtonTextColor = Color.Black;
//PROBLEMATIC CODE HERE:
***********this.Controls.Add(LabelText);***************
}

public Color ButtonTextColor
{
get
{
return m_TextColor;
}
set
{
m_TextColor = value;
LabelText.ForeColor = m_TextColor;
}
}
public Font ButtonFont
{
get
{
if (m_Font == null)
{
m_Font = new Font("Calibri Light", 12);
}
return m_Font;
}
set
{
m_Font = value;
LabelText.Font = ButtonFont;
adjustLabel();
}
}

public string ButtonText
{
get
{
return m_Text;
}
set
{

m_Text = value;
LabelText.Text = m_Text;
adjustLabel();
}
}
private void adjustLabel()
{
const int MARGIN = 10;
LabelText.AutoSize = true;//needed for autosize calculation of the label;
Size newSize = new Size(LabelText.Size.Width, LabelText.Size.Height); ;
LabelText.AutoSize = false;//after auto-calculated size of the label, set to false in order for centering label works well.
this.MinimumSize = newSize;
this.Size = new Size(newSize.Width + MARGIN, newSize.Height + MARGIN);
LabelText.TextAlign = ContentAlignment.MiddleCenter;
LabelText.Dock = DockStyle.Fill;
}

private void RoundedButton_MouseLeave(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_BasicImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseEnter(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_HoverImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseUp(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_BasicImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}

private void RoundedButton_MouseDown(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_ClickImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}
}


thanks in advanced, tomer.










share|improve this question




















  • 1





    Quick guess: LabelText is gobbling up the events before RoundedButton can see them.

    – MikeH
    Nov 12 '18 at 23:50











  • if so, any work-around or fix?

    – tom01
    Nov 12 '18 at 23:57






  • 1





    Try subscribing LabelText to those same events, see if it works then.

    – MikeH
    Nov 13 '18 at 0:00











  • yea, it does works (from within the class). your guess was correct, yet its not a fix, first because it feels a bit upside-down, second because components that will use RoundedButton wont be able to subscribe to its events since the TextLabel is private and i dont wont it to be public...

    – tom01
    Nov 13 '18 at 0:16













  • You could just call the OnMouseDown method from the event of your label which will fire the MouseDown event that is public. I'm not sure there's a way to prevent the LabelText from eating up the events (I could be wrong though).

    – MikeH
    Nov 13 '18 at 0:22
















0












0








0








I'm having a weird behavior with a class i created inheriting from PictureBox;



its a class made to behave like a button (basiclly what i care about is the mouse enter, mouse leave, mouse down, mouse up events).



everything works perfectly until i add the TextLabel (code below) to the control of my customed class. it shows the label, centered and everything as i wanted but the events i mentioned before (and every other event in that matter) becomes disabled, for some reason it wont fire them.



would like to know what is the reason for that behavior and if there any fix for it.



public class RoundedButton : PictureBox
{
private readonly Image r_BasicImage = RoundedButtonCheck2.Properties.Resources.basicRoundedButtonIcon as Image;
private readonly Image r_HoverImage = RoundedButtonCheck2.Properties.Resources.hoverRoundedButtonIcon as Image;
private readonly Image r_ClickImage = RoundedButtonCheck2.Properties.Resources.clickRoundedButtonIcon as Image;

private string m_Text;
private Font m_Font;
public Color m_TextColor;
private Label LabelText = new Label();


public RoundedButton()
{
this.Width = 130;
this.Height = 40;
this.Image = r_BasicImage;
this.BackColor = Color.Transparent;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.MouseDown += RoundedButton_MouseDown;
this.MouseUp += RoundedButton_MouseUp;
this.MouseEnter += RoundedButton_MouseEnter;
this.MouseLeave += RoundedButton_MouseLeave;
LabelText.Font = ButtonFont;
ButtonTextColor = Color.Black;
//PROBLEMATIC CODE HERE:
***********this.Controls.Add(LabelText);***************
}

public Color ButtonTextColor
{
get
{
return m_TextColor;
}
set
{
m_TextColor = value;
LabelText.ForeColor = m_TextColor;
}
}
public Font ButtonFont
{
get
{
if (m_Font == null)
{
m_Font = new Font("Calibri Light", 12);
}
return m_Font;
}
set
{
m_Font = value;
LabelText.Font = ButtonFont;
adjustLabel();
}
}

public string ButtonText
{
get
{
return m_Text;
}
set
{

m_Text = value;
LabelText.Text = m_Text;
adjustLabel();
}
}
private void adjustLabel()
{
const int MARGIN = 10;
LabelText.AutoSize = true;//needed for autosize calculation of the label;
Size newSize = new Size(LabelText.Size.Width, LabelText.Size.Height); ;
LabelText.AutoSize = false;//after auto-calculated size of the label, set to false in order for centering label works well.
this.MinimumSize = newSize;
this.Size = new Size(newSize.Width + MARGIN, newSize.Height + MARGIN);
LabelText.TextAlign = ContentAlignment.MiddleCenter;
LabelText.Dock = DockStyle.Fill;
}

private void RoundedButton_MouseLeave(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_BasicImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseEnter(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_HoverImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseUp(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_BasicImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}

private void RoundedButton_MouseDown(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_ClickImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}
}


thanks in advanced, tomer.










share|improve this question
















I'm having a weird behavior with a class i created inheriting from PictureBox;



its a class made to behave like a button (basiclly what i care about is the mouse enter, mouse leave, mouse down, mouse up events).



everything works perfectly until i add the TextLabel (code below) to the control of my customed class. it shows the label, centered and everything as i wanted but the events i mentioned before (and every other event in that matter) becomes disabled, for some reason it wont fire them.



would like to know what is the reason for that behavior and if there any fix for it.



public class RoundedButton : PictureBox
{
private readonly Image r_BasicImage = RoundedButtonCheck2.Properties.Resources.basicRoundedButtonIcon as Image;
private readonly Image r_HoverImage = RoundedButtonCheck2.Properties.Resources.hoverRoundedButtonIcon as Image;
private readonly Image r_ClickImage = RoundedButtonCheck2.Properties.Resources.clickRoundedButtonIcon as Image;

private string m_Text;
private Font m_Font;
public Color m_TextColor;
private Label LabelText = new Label();


public RoundedButton()
{
this.Width = 130;
this.Height = 40;
this.Image = r_BasicImage;
this.BackColor = Color.Transparent;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.MouseDown += RoundedButton_MouseDown;
this.MouseUp += RoundedButton_MouseUp;
this.MouseEnter += RoundedButton_MouseEnter;
this.MouseLeave += RoundedButton_MouseLeave;
LabelText.Font = ButtonFont;
ButtonTextColor = Color.Black;
//PROBLEMATIC CODE HERE:
***********this.Controls.Add(LabelText);***************
}

public Color ButtonTextColor
{
get
{
return m_TextColor;
}
set
{
m_TextColor = value;
LabelText.ForeColor = m_TextColor;
}
}
public Font ButtonFont
{
get
{
if (m_Font == null)
{
m_Font = new Font("Calibri Light", 12);
}
return m_Font;
}
set
{
m_Font = value;
LabelText.Font = ButtonFont;
adjustLabel();
}
}

public string ButtonText
{
get
{
return m_Text;
}
set
{

m_Text = value;
LabelText.Text = m_Text;
adjustLabel();
}
}
private void adjustLabel()
{
const int MARGIN = 10;
LabelText.AutoSize = true;//needed for autosize calculation of the label;
Size newSize = new Size(LabelText.Size.Width, LabelText.Size.Height); ;
LabelText.AutoSize = false;//after auto-calculated size of the label, set to false in order for centering label works well.
this.MinimumSize = newSize;
this.Size = new Size(newSize.Width + MARGIN, newSize.Height + MARGIN);
LabelText.TextAlign = ContentAlignment.MiddleCenter;
LabelText.Dock = DockStyle.Fill;
}

private void RoundedButton_MouseLeave(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_BasicImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseEnter(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_HoverImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}

private void RoundedButton_MouseUp(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_BasicImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}

private void RoundedButton_MouseDown(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_ClickImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

}
}
}


thanks in advanced, tomer.







c# events label picturebox invoke






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:59







tom01

















asked Nov 12 '18 at 23:45









tom01tom01

11




11








  • 1





    Quick guess: LabelText is gobbling up the events before RoundedButton can see them.

    – MikeH
    Nov 12 '18 at 23:50











  • if so, any work-around or fix?

    – tom01
    Nov 12 '18 at 23:57






  • 1





    Try subscribing LabelText to those same events, see if it works then.

    – MikeH
    Nov 13 '18 at 0:00











  • yea, it does works (from within the class). your guess was correct, yet its not a fix, first because it feels a bit upside-down, second because components that will use RoundedButton wont be able to subscribe to its events since the TextLabel is private and i dont wont it to be public...

    – tom01
    Nov 13 '18 at 0:16













  • You could just call the OnMouseDown method from the event of your label which will fire the MouseDown event that is public. I'm not sure there's a way to prevent the LabelText from eating up the events (I could be wrong though).

    – MikeH
    Nov 13 '18 at 0:22
















  • 1





    Quick guess: LabelText is gobbling up the events before RoundedButton can see them.

    – MikeH
    Nov 12 '18 at 23:50











  • if so, any work-around or fix?

    – tom01
    Nov 12 '18 at 23:57






  • 1





    Try subscribing LabelText to those same events, see if it works then.

    – MikeH
    Nov 13 '18 at 0:00











  • yea, it does works (from within the class). your guess was correct, yet its not a fix, first because it feels a bit upside-down, second because components that will use RoundedButton wont be able to subscribe to its events since the TextLabel is private and i dont wont it to be public...

    – tom01
    Nov 13 '18 at 0:16













  • You could just call the OnMouseDown method from the event of your label which will fire the MouseDown event that is public. I'm not sure there's a way to prevent the LabelText from eating up the events (I could be wrong though).

    – MikeH
    Nov 13 '18 at 0:22










1




1





Quick guess: LabelText is gobbling up the events before RoundedButton can see them.

– MikeH
Nov 12 '18 at 23:50





Quick guess: LabelText is gobbling up the events before RoundedButton can see them.

– MikeH
Nov 12 '18 at 23:50













if so, any work-around or fix?

– tom01
Nov 12 '18 at 23:57





if so, any work-around or fix?

– tom01
Nov 12 '18 at 23:57




1




1





Try subscribing LabelText to those same events, see if it works then.

– MikeH
Nov 13 '18 at 0:00





Try subscribing LabelText to those same events, see if it works then.

– MikeH
Nov 13 '18 at 0:00













yea, it does works (from within the class). your guess was correct, yet its not a fix, first because it feels a bit upside-down, second because components that will use RoundedButton wont be able to subscribe to its events since the TextLabel is private and i dont wont it to be public...

– tom01
Nov 13 '18 at 0:16







yea, it does works (from within the class). your guess was correct, yet its not a fix, first because it feels a bit upside-down, second because components that will use RoundedButton wont be able to subscribe to its events since the TextLabel is private and i dont wont it to be public...

– tom01
Nov 13 '18 at 0:16















You could just call the OnMouseDown method from the event of your label which will fire the MouseDown event that is public. I'm not sure there's a way to prevent the LabelText from eating up the events (I could be wrong though).

– MikeH
Nov 13 '18 at 0:22







You could just call the OnMouseDown method from the event of your label which will fire the MouseDown event that is public. I'm not sure there's a way to prevent the LabelText from eating up the events (I could be wrong though).

– MikeH
Nov 13 '18 at 0:22














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271733%2fafter-adding-label-to-picturebox-contols-events-dont-fire-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271733%2fafter-adding-label-to-picturebox-contols-events-dont-fire-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma