C#: Breakpoint is never reached and Picturebox Image does not change - c#

i was trying to add some kind of advent calender to my programm.
So i created a second form with the designer.
After clicking a toolstripmenuitem following gets executed.
private void OpenAdventCalender()
{
if (fAdvent == null)
{
fAdvent = new Advent();
fAdvent.FormClosed += new FormClosedEventHandler(fAdvent_FormClosed);
AdventOptions[0] = "1";
fAdvent.Visible = true;
}
}
Now that works fine. The Form does open and i can doubleclick picktureboxes.
On a doubleclick the image of the picturebox should change.
I tried it like that:
private void ShowAdventMessage(object sender, EventArgs e)
{
if(false){} //in original code i test on 24 days and pictureboxes
else if ((DateTime.Today >= DateTime.Parse("01.12.2016")) && (sender == pictureBox1))
{
fForm1.AdventOptions[1] = "1";
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
pictureBox1.Image = Resources.p1;
pictureBox1.Update();
MessageBox.Show(fForm1.AdventList[0]); //This Message pops up correctly
}
}
So the first big Problem is that the pictureBox1 does not change the image.
It also does not get deletet if i set the picturebox to null.
In the constructor im changing it the first time, there it works. Outside the constructor it does not work.
Now the second problem. I cannot debug in there. Breakpoints in the constructor or in any other function of my second form are never reached. Since i know that that Messagebox pops up i know it does reach the code.
When i hover over the breakpoint with my mouse, it told me:
The breakpoint cannot be reached at the moment. The code is differnt to the original version.
It also told me that i can change something in options to reach the breakpoints.
So i changed "Extras - Options - Debugging - General - Sourcefiles must be exactly like the original version = deactivated".
After that the breakpoints were still unreachable. Now the message from the breakpoint was:
The breakpoint cannot be reached at the moment. There is no executable code with the targetcodetype of the debugger connected with this line.
Does someone know how to debug in there?
Or does someone know what i am doing wrong so that the image does not change?
What i use if needed:
- Windows 10 professional 64bit
- Visual Studio 2015
- Resharper Ultimate
Thanks for any help.

After testing if i also cannot debug a complete newly created form, i compiled it fully through project explorer. After that it worked.

Related

Visual studio crashes when opening forms inheriting from a specific form in the project

I know this can be a duplicate, but I already searched the web and tried literally ALL and EVERY solution I found, but to no avail...
I have a custom form in my project called ParentForm it is the parent of most of my other forms in the project, it has some standard elements on it, like panels and timers.
I have no problem with showing this form, editing its design or code.
but whenever I open a form that is inheriting from it, it opens for a moment (not responding thou) and then visual studio crashes immediately, I think it was only the designer first, but now, even when I open the form's code, the same happens!
I tweaked some of the parent form's code and now it stopped crashing.. but now, whenever I open a child form of it, I get this:
ContaierPanel and panel2 are two panels I have on the parent form, and as I see no problem with them, they are used as containers in the child forms, their access modifiers are public too.
The problems seems to be related to the parent form itself, but I already removed every single line of code in it, that didn't help at all, now I'm stuck with the error you see in the image above.
I also uploaded the complete project to Github in case someone wanted to see the code, here's the link.
I'm using the Material Skin in the project.
Your help is so appreciated, I have no much time before the deadline and there is still much to do... Thanks
Edit: After missing around the code for a few extra hours, I finally discovered the code part that is causing the problem:
As I said before I have a timer on the parent form called tmrCheckConnection, here's the code of the timer:
private void tmrCheckConnection_Tick(object sender, EventArgs e)
{
if (!Program.dbConnection.IsConnect())
{
if (!controlsAreDisabled)
{
disabledControls.Clear();
foreach (Control c in this.Controls)
{
if (c.Enabled)
{
disabledControls.Add(c);
c.Enabled = false;
}
}
controlsAreDisabled = true;
}
lblNoDBConnection.Enabled = true;
lblNoDBConnection.Visible = true;
this.Controls.SetChildIndex(lblNoDBConnection, 0);
} else {
if (controlsAreDisabled)
{
foreach (Control c in disabledControls)
c.Enabled = true;
lblNoDBConnection.Visible = false;
}
}
}
Obviously, the error happens here >> Program.dbConnection.IsConnect() << because that is not an instantiated class yet, and the IsConnect() functions uses some of its un-instatiated members.
So the solution is simply to prevent the timer code from running at design time, so I tried putting this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
return;
at the beginning of the timer's code, it didn't change anything.
Next, I tried to remove the event handler from the timer, and reassign it in the Form_Load like this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Runtime)
tmrCheckConnection.Tick += tmrCheckConnection_Tick;
And again, it didn't help.
Any ideas?
Oh, and I removed the github repository.

can't get form to remain on top, in .net

I can't get a form to remain on top, in .net
I have checked How to make form always on top in Application and the answer there mentions form1.TopLevel = true; and I've checked How to make a window always stay on top in .Net? and it says Form.ActiveForm.TopMost so i've tried Form.ActiveForm.TopMost = true; and this.TopMost = true;
private void Form1_Load(object sender, EventArgs e)
{
this.TopLevel = true; //default anyway
Form.ActiveForm.TopMost = true;
this.TopMost = true;
}
But as you can see, a notepad window or any window can cover it.
Added
I have tried every suggestion made so far.
In response to Han's advice, "Of all the possible places to set TopMost property, the Load event is the worst. It should be set in the constructor, so the window gets created top-most right away. Or it should be set after it is visible, so after the Load event. Use the constructor. ". I tried putting those lines in the constructor.
public Form1()
{
InitializeComponent();
this.TopLevel = true; //default anyway
//Form.ActiveForm.TopMost = true; (commented to prevent a System.InvalidOperationException, presumably the form isn't yet active at this stage)
this.TopMost = true;
}
And the the other two suggestions, from others-
I tried setting the Form's TopMost field in the designer to True.
And running the EXE directly rather than just clicking play in visual studio.
Same issue.
And if you find that hard to believe, i've taken a 1min video here showing just that. https://screencast-o-matic.com/watch/cbXXrw2oTN
A potentially useful comment was mentioned..
Steve comments - "OK something definitively odd is happening here. I have tried to create a simple topmost form using linqpad and at the first run I get your same behavior. At the second run though everything works as expected."
A workaround, is this, following on from Han's point to put not put it in Load. Load is indeed too early. I've been finding that (at least on the system with the issue), The Constructor is also too early. I find that putting it in the Shown event, works.
One possible solution to this, is to run this patch, https://support.microsoft.com/en-us/help/2587473/topmost-windows-are-not-always-in-the-topmost-position-in-windows-7-or But be warned, if you want to uninstall it, I have some doubts whether it uninstalls correctly. And it's also not clear to me whether the patch is working or working sporadically. It likely works but it's hard for me to tell.
One commenter thought it's just my system, though that's not the case, as Steve ran into the same issue the first time he ran it. I find it may be most prone to happen after windows restarts, So, the program is running very fresh. But I find that putting that code in the Shown event, is fine and the form stays on top.
I tried instead of TopMost=true, using SetWindowPos, to set the window on top, I tried it fairly early like in the constructor, and late, like in the Shown event or on a button click, and I found it was fine in the Shown event or on a button click. So the issue was related to the TopMost=true line, or SetWindowPos line, firing too early, prior to the window it is setting appearing.
When calling SetWindowPos later one can use either this.Handle or GetForegroundWindow(), the former is easier as it's native. When calling it earlier one must use this.Handle. And using this.TopMost=true avoids all winAPI calls completely.
So in short, you could try the patch, though note that it might not uninstall right.. Or you can try the workaround of putting this.TopMost=true in the Shown event of the form.

Picturebox tick event behaving inconsistently

So recently i have been trying to get into C# programming after spending alot of time in C.
Naturally i jumped right into learning a few new things, in this case i wanted to try some forms, classes and events - simple right?
Well apparently not, i am rather dumbfounded, i have been running the program, analyzing step by step, the tick event triggers as its supposed to, but the picture in the picturebox is not updated.
And here is the kicker, i made a button that runs almost identical code, and that works, what gives? i can barely get my head around it as it is.
http://pastebin.com/psYzQSLE - here is the code i am running currently.
And here is the specific segment of code i cant get to behave.
private void timer1_Tick(object sender, EventArgs e)
{
if(swapper)
{
swapper = false;
pictureBox1.Image = ima1;
pictureBox1.Refresh();
}
else
{
swapper = true;
pictureBox1.Image = ima2;
pictureBox1.Refresh();
}
}
Your code looks fine, and is working for me. I suspect the Form1_Load method is not hooked up to the form's Loaded event; you can check that in the designer.
Okay, i figured it out!
Being unfamilar with the design document, the timer was added to the tick in there and i also added it myself in my code, the result being a timer that triggers twice immediately, and thus i saw no result.
Problem is solved now, thanks for the warm welcome :)

Buttons Enabled Property not Working Properly

I am creating an Windows Application. I have two buttons.
I have written the following code snippet.
frmRb obj = new frmrb();
private void btnPd_Click(object sender, EventArgs e)
{
btnCancel.Enabled = true;
obj.btnRtn.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
obj.BringToFront();
obj.Focus();
}
The above coding does not generate any error.
All the statements are working properly but the following statement is not working properly:
obj.btnRtn.Enabled = true;
is not executed.
The frmrb forms is bring to front and it is focussed but btnRtn is not Enabled that is the statement obj.btnRtn.Enabled = true; is not working.
By default I have set the property of btnRtn Enabled to false.
And Please note the Modifier property of btnRtn button is set to PUBLIC.
Now how should I change the coding so that I can get this statement executed.
obj.btnRtn.Enabled = true;
Can anybody help me out?
Thanks in Advance!!
SOLUTION
You should never disable a button, or change it´s visibility before it is initialized, otherwise you won't be able to enable it again, or turn it visible again.
Instead, you should disable it on it's own "Initialized" event, and then it will work properly!
I had the same problem.
Is the button placed inside a panel or any container. If yes then please check the enabled status of the container also.
You not mention that where you obj(which is the instance of frmRb) show. because it is very important point.
from your coding it seem that frmRb is already visible. so u never called the
obj.Show() ;
instead you call the
obj.BringToFront();
so the problem is that you never show the frmRb object. which is you create u in 1st line. each time u write the line
frmRb obj = new frmrb();
new instance of frmrb is created. So u must again show it, with the line obj.Show() ;
Now u rewrite ur code as ::
frmRb obj = new frmrb();
private void btnPd_Click(object sender, EventArgs e)
{
btnCancel.Enabled = true;
obj.btnRtn.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
obj.Show();
obj.BringToFront();
obj.Focus();
}
I hope it is helpful for u and solve ur problem.
I've been using VB rather than C#, but the languages are very similiar. in VB, you have to add "handles SomeButton.Click" to make it handle the Click events.
According to google, the equivalent in C# is to go into the Designer.cs file, find where the controls are, and manually change the click event hookup to point to your new event handler.
As mentioned by the previous poster, use a breakpoint (F9) and the debugger to see if that method is ever called when you execute the event. If it is not called, then the problem is probably not with the enabled property, but the wiring of the method so that it is invoked when the event occurs.
Here's a reference:
http://www.tek-tips.com/viewthread.cfm?qid=1442702&page=5
I strongly suspect that either the click handler isn't being called or you're not looking at the form you think you are.
Where are you setting up the click handler for btnPd_Click? Check that's still wired up correctly.
Then put a breakpoint on the first line of the handler and run it in the debugger - if you don't hit the breakpoint when you click the button, that's the problem.
EDIT: Okay, next steps:
Check that you're looking at the right button. Change its text as well as enabling it.
Check that its container is enabled, as suggested by phoenix. Not just its direct parent, but all the way up.
Check what else your UI thread is doing afterwards - are you blocking it for some reason? Does the rest of the UI work at that point?
I would try just switching the sequence of statements to:
private void btnPd_Click(object sender, EventArgs e)
{ obj.btnRtn.Enabled = true;
btnCancel.Enabled = true; }
and see if that helps you debug
Your code really should produce an error... C# is case sensitive, meaning frmRb is not the same as frmrb. Anyway, I copied it, created 2 forms and 3 buttons, and set up the handler, and it worked fine.
private void InitializeComponent()
{
this.btnPd = new System.Windows.Forms.Button();
this.btnPd.Location = new System.Drawing.Point(90, 116);
this.btnPd.Name = "btnPd";
this.btnPd.Size = new System.Drawing.Size(75, 23);
this.btnPd.TabIndex = 1;
this.btnPd.Text = "button1";
this.btnPd.UseVisualStyleBackColor = true;
this.btnPd.Click += new System.EventHandler(this.btnPd_Click);
}
public System.Windows.Forms.Button btnRtn;
Are you sure you were handling btnPd? Perhaps you may have locked your enabling code inside a disabled button? Hopefully this small working sample helps you find the problem. As for the rest of the code, All I changed was the frmRb to frmrb so they match.
If the form that your are trying to show on the screen is properly shown, may be you can try this:
Create a public method in the form that will set the button property enabled = true;
After creating the form, and showing, you can call that public method;
frmRb obj = new frmrb();
obj.EnableButton
It's been a while since anyone commented or answered I thought I would pose this answer/comment as it may be be helpful to others who stumble on it.
I recently had some CheckBoxes and NumericUpDowns not changing enabled state but it was simply due to the fact they were in a GroupBox that had not been enabled. A forehead slapping moment for me, but took me 20 min to figure out why those controls weren't responding!
Just a hunch. May be the 'Locked' property of button is 'true'
Pl check btnRtn situation if btnRtn is in panel or open ,if it in the panel pl check the panel enabled property and your coding is ok

Windows Forms ToolTip will not re-appear after first use

I have a Windows Forms C# application where I would like to use a tooltip on one of the text boxes. I initialize the tool-tip in the constructor of the Form class, and it works the first time. So when I hover over the text box with my mouse it works, but once the toolTip times out and it goes away, it does not re-appear when I move my mouse away and back onto the control. I would expect it to come back. What am I doing wrong?
Here is how I initialize the tooltip:
myTip = new ToolTip();
myTip.ToolTipIcon = ToolTipIcon.Info;
myTip.IsBalloon = true;
myTip.ShowAlways = true;
myTip.SetToolTip(txtMyTextBox,"My Tooltip Text");
I had a similar problem today. Sometimes, the tooltip would not show. I had one ToolTip control for all the controls in my form.
I also had a MouseEnter event on all the controls added automatically, so I modified the MouseEnter event to do:
_tooltip.Active = false;
_tooltip.Active = true;
It fixed the bug, but I don't know why.
Also, the bug always happened on Windows XP machines, but not on Windows Vista.
I guess you'll be happy to know that Microsoft knows about it...since about 5 years...
2/21/2005 Bug acknowledged as reproducable
3/29/2005 Hum we might fix it, but later...
11/15/2005 Well actually it's not a big bug, and it doesn't happen much, so we won't fix it.
Damn I love it when I stumble on bugs Microsoft doesn't want to solve! This time it's called a corner case, last time it was simply too difficult to resolve...
http://connect.microsoft.com/VisualStudio/feedback/details/115385/tooltip-stop-showing-after-autopopdelay
I'm off to tell my client that the bugs in my program are just corner cases and too difficult to resolve...
I had a similar problem today. VS 2010 SP1 .Net 3.5
After AutoPopDelay-Time the ToolTip do not show the Controls ToolTipText.
Kevins solution is the only way to solve the problem.
I encapsulate this in my own ToolTip class:
public class ToolTip : System.Windows.Forms.ToolTip
{
public ToolTip() : base() { }
public ToolTip(System.ComponentModel.IContainer components) : base(components) { }
public new void SetToolTip(System.Windows.Forms.Control ctl, string caption)
{
ctl.MouseEnter -= new System.EventHandler(toolTip_MouseEnter);
base.SetToolTip(ctl, caption);
if(caption != string.Empty)
ctl.MouseEnter += new System.EventHandler(toolTip_MouseEnter);
}
private void toolTip_MouseEnter(object sender, EventArgs e)
{
this.Active = false;
this.Active = true;
}
}
I had this issue in VB.NET. What I did was drop a TooTip control on the form, and then on the target control's MouseHover event, I set the properties of the ToolTip. I did this because I used one ToolTip control for five different Label controls. It worked great. (Really, I wanted the ToolTip to show immediately, so I used the MouseEnter event instead.) I can post my exact code tomorrow when I get to work.
I solved this problem by this
if (t == null)
{
t = new ToolTip();
}
t.IsBalloon = true;
t.ToolTipTitle = "Stop";
t.ToolTipIcon = ToolTipIcon.Error;
t.Show("", yourControlonWhichToApplyToolTip, 0);
t.Show("PDescription", yourControlonWhichToApplyToolTip, 1000);
Note i have added an empty tooltip.
For what it's worth, I was having this problem on my Windows XP system until I noticed that if I placed at least one tooltip control on my form manually (from the toolbox) I could create as many tooltips as needed within my code, and they would all work.
If, however, I tried to create all tooltips in code (say for instance in the formload event) the tips would only show once and never to be seen again. I can't give you the exact "why this happens" story, but I have duplicated this issue several times always with the same effect. It might have something to do with the object scope, but I'm not sure.
So now just as a habit, I always include at least one Visual Studio tooltip control and then the rest within my code.
I just had the the problem on Windows 7 so I found this thread.
In my case this did not work in tooltip_MouseEnter:
tooltip.Active = false;
tooltip.Active = true;
So I tried the following:
this.toolTip.SetToolTip(this.txtbx1, "tooltip-text");
This worked fine for me.
In my case after setting the tooltip text with the SetToolTip method, I used the Show overload with duration parameter, i.e.
toolTip.Show(text, textEdit, 1000);
After that tooltip did not reappear on mouse hover, and resetting tooltip.Active didn't work..
A workaround that worked for me was to use Show overload without the duration, and hide it manually afterwards:
toolTip.Show(text, textEdit);
new Task(() =>
{
Thread.Sleep(750);
textEdit.Invoke(new Action(() => toolTip.Hide(textEdit)));
}).Start();
With this code I have the desired behaviour, i.e.
The tooltip is shown at once for 750 millisec. after the tooltip text has changed
The tooltip does appear for the specified time when the mouse is over the control
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
private void textBox_MouseHover(object sender, EventArgs e)
{
ToolTip1.Show("YOUR TEXT", textBox);
}
private void textBox_MouseLeave(object sender, EventArgs e)
{
ToolTip1.Active = false;
ToolTip1.Active = true;
}

Categories

Resources