There are similar questions around here but none that fit into my my particular case scenario.
I have a Windows Form with a Button. The button is attached to the event handler as follows:
private void mybutton_Click(object sender, EventArgs e)
{
// do some processing here
}
In addition there is a combobox where a change in selection in supposed to trigger the button event handler as defined above.
private void mycombobox_SelectedIndexChanged(object sender, EventArgs e)
{
mybutton_Click(sender, e); // this is the line which pops up the dialog
}
The code works exactly as intended at runtime but i get a dialog prompt at compile time which reads:
object reference not set to an instance of an object
There are no other errors or warning.
A google search tells me that this message is an error caused if the program is trying to access a member of a reference type variable which is set to null.
However when i run this code in debug mode, both the sender and event(e) variables are not null.
So why is this dialog popping up ?
And if this had been an error or warning - it should have shown as an error or warning but nothing of that sort happens.
Here's the screenshot:
Edits: Answering Questions Raised in Comments
There are no errors as you can see in the screenshot.
The program works great - just this pop up
The popup is caused by the line:mybutton_Click(sender, e); in the combobox selectedIndexChanged function.
The mybutton_Click(sender, e) does not use any of the arguments sender or e in the processing.
I have not installed any VS extensions either.
It is not a good design to call the Click-event of the button in the SelectedIndexChanged-event of the ComboBox and this might also be the reason for the error.
Better put your logic in a seperate method and call it in the Click- and the SelectedIndexChanged-event like this:
private void UpdateSomething()
{
// Do whatever you want
}
private void mybutton_Click(object sender, EventArgs e)
{
UpdateSomething();
}
private void mycombobox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateSomething();
}
I think what is happening is that the events are firing in Design mode (which I seem to recall happening to me a few times when using WinForms back in the day).
To get around it what I did was handled the form load event, then in that I attached a listener to the SelectedIndexChanged. Then in Design mode the event isnt bound and wont fire, but at run time it is bound.
Something like:
public void form_OnLoaded(object sender, EventArgs e)
{
myComboBox.SelectedIndexChanged += mycombobox_SelectedIndexChanged;
}
Related
I would want to launch a KeyDown Event on Form_Load however its taking me somewhere else in the Form_Load event.
Form_Load:
int static_int = 0;
private void Form1_Load(object sender, EventArgs e)
{
if(condition == true)
{
txtInput.Text = "something";
txtInput.Focus();
SendKeys.Send("{Enter}");
int somegeneratednubmer = 20;
static_int = static_int + somegeneratednumber;
//somemore code here
}
}
KeyDown:
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
static_int = 10;
//somemore codes here too
}
I would like to get the SUM of static_int and somegeneratednumber which is 30. However, after Debugging, I'm getting its initialized value of 0. From what I understood, after SendKeys.Send("{Enter}") the KeyDown event should proceed.
Why is it not??
How would I get the correct result? I really should do the KeyDown event on Form_Load, a conditional event...
or What am I doing wrong here?
Note: originally static_int is initialized on a Class
No, the KeyDown even will proceed at the earliest possible moment, which is when the appropriate message is executed from the form's message queue. That cannot happen before the Load event finishes, because that also on the message queue. Even if that weren't the case, SendKeys doesn't wait for the action to be processed. It just sends the message and returns immediately.
Another problem is that SendKeys sends the virtual keys to the currently active window. That can never be your window, since your window isn't even shown yet! When something behaves weird, a good first step is to read the documentation.
So, why is the value of static_int zero, instead of 20 or 30? Well, the likeliest case is an unhandled exception, and I'm pretty sure that's exactly what happens when you do tbxInput.Focus. The control doesn't quite exist yet, and it can't be made the input focus. If you have trouble understanding all this, you might want to find some book on the basics of how Windows windows work - there's nothing .NET can do about it, and it's places like this where the (very pretty) .NET abstraction leaks a lot. If you're planning to do any Windows UI development, you really need to know at least the basics.
However, that's completely unnecessary anyway. You don't have to execute a KeyDown event. Just make a method that's called from both the Load event handler and the KeyDown event handler.
try adding this event instead
Form1 isn't loaded yet so no events yet.
private void Form1_Shown(Object sender, EventArgs e)
{
SendKeys.Send("{Enter}");
}
But truly this design is wrong
So I was make the display name (content) of a Lable from another page of the GUI. After realising a public static void function can't change the Displayname because it's not static, I messed around with Events and got a Handler set up to run when there other page publishes the event.
It looks like this
public void UpdateEventHandler(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Event Received");
tab1.DisplayName = Globals.tab1Name;
}
When the event goes off the message box pops up but the DisplayName does not change.
There was no errors, nothing.
The problems is not Globals.tab1Name because I ran it through the MessageBox and it was fine.
So I made another button, on the same page as the Lable:
private void Button_Click(object sender, RoutedEventArgs e)
{
tab1.DisplayName = Globals.tab1Name;
System.Windows.Forms.MessageBox.Show("clicked");
}
This time all the code worked, lable changed and msgbox popped up.
I made another function with the same two lines of code in it, called it with the event handler, again only the msgbox works. But when I call the same function with the button it all works.
Any help would be great, thanks in advance.
MessageBoxes pause the execution of the current thread. Therefore, when you show the MessageBox first the second line isn't hit thus the string isn't updated
Try adding the display name as a parameter for the form load. By default the form has sender and e as parameters, but you can always add your own too.
I have the following method defined to be called when a form closes
private void TimeKeeper_Closing(object sender, EventArgs e)
However, it doesn't show up in the list when I try to assign it in the Events section of the Form properties. Is there something I need to do to get it to show up there?
EDIT: Sorry, originally posted the wrong method...
The Closing event is obsolete, it dates from .NET 1.x. Microsoft goofed that one pretty badly and it was replaced in .NET 2.0 with the FormClosing event. Which tells you a lot more about why the form is getting closed. The e.CloseReason is very important, you don't want to prevent Windows from shutting down.
Which is why you can't find it, you are not supposed to use it anymore. Note how the answer you accepted just stops your program from compiling. You probably discovered the FormClosing event by yourself.
Might as well go whole-hog and point out how silly it is for a class to listen to its own events. Events are meant for other code. They work pretty well in the designer, that's why you end up writing code like this. But the sane thing to do is to just override the method:
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
// Some code that might set e.Cancel = true
//...
}
base.OnFormClosing(e);
}
Which has many advantages, beyond the typical lossage of forgetting to subscribe the event with the designer, a derived class can simple alter the decision being made here by setting e.Cancel back to true.
You have to change EventArgs to FormClosingEventArgs
private void TimeKeeper_Closing(object sender, FormClosingEventArgs e)
I am busy having a problem with allocating a new event to an existing button that was created in the designer.
Now here is the button that was created prior to runtime which is inside the modalpopupexteder5 -
<asp:Button runat="server" ID="btnClose" Text="Close" OnClick="btnClose_Click" />
Here is the codebehind -
protected void btnAddAccount_Click(object sender, EventArgs e)
{
btnClose.Click -= new EventHandler(btnClose_Click);
btnClose.Click += new EventHandler(btnCancel_Click);
ModalPopupExtender5.Show();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
ModalPopupExtender11.Hide();
}
protected void btnClose_Click(object sender, EventArgs e)
{
ModalPopupExtender5.Hide();
}
So the button in the beginning has the event btnClose_Click hooked up to it. But I want to change it to the btnCancel_Click
But it wont execute the btnCancel_Click. It executes the original btnClose_Click
Any Ideas what could of caused this?
Does this relate to the page life cycle?
--EDIT--
I should let you know that the btnAddAccount_Click does get executed.
Basically I don't to create the same modalpopupextender, I want to use the existing one but depending on user selection will determine which eventhandler should be called and in this case the btnAccount_Click has selected the btnCancel_Click to be assigned to the button.
ASP.NET is not assigning the Click events anywhere for the server side buttons. Those buttons are rendered as plain submit buttons, and the internal code behind is checking the submitted value then based on that it finds the "clicked" button and calls the proper handler.
This means your current logic is leading to a dead end.
Instead of messing with the events, have btnClose_Click as the handler, and in there check the currently active/visible popup and hide it.
I am not quite sure what exactly you are asking, but you can add a Handles to your event and so control the events on your buttons.
Something like:
protected void btnCancel_Click(object sender, EventArgs e) Handles btnCancel.Click, btnClose.Click
{
//do stuff
}
The form I am using requires a copy pasted URL. I am trying to have a textChanged event that will check the url as soon as it is pasted, telling the user whether it is valid or invalid. I also want to be able to lock out the textbox when this happens, with a message saying something like "Processing...".
The problem is with the code below, the textbox is never disabled, the program will do the checkUrl() method and the textbox is never disabled even though it is first to execute (I assume it is but the fact there is a function call right underneath it is messing around with something or getting higher priority).
How do I go about making the control visually disabled while the method runs?
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
checkUrl();
urlTxtBx.Enabled = true;
}
I think this is happening because the Application needs to complete all the active threads before disabling the TextBox. Please try the following code:
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
Application.DoEvents();
checkUrl();
urlTxtBx.Enabled = true;
}
This will let the UI to be updated. For more details check here.