VisualC# Cannot Change DisplayName of Control - c#

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.

Related

Button clicked event not triggering after lost focus event

I have a Winforms c# form with some comboBoxes , cancel and save buttons that work fine.
I now need to capture when the user has finished entering text into a comboBox.
I add an empty ( for now) lostFocus (or Leave) event to the combbox , which triggers fine. However if the cause of that event was a cancel or save button press , the corresponding event is no longer triggered. These buttons still work fine if pressed at other times.
Should these two event be firing in sequence or is there some better way to capture completed text entry?
The Leave and/or LoseFocus events do not get triggered because you do not leave the combobox and because it doesn't lose focus when you press Enter or Escape.
Therefore the best way is to add the function you are triggering in the LoseFocus event, also to the Button click events of the Cancel- and the Accept-Buttons.
Adding a call to the leave event itself: comboBox1.Leave(null, null); would be the simplest way.
To make sure that the function is called only once, I check who has focus in the ButtonClick events:
private void acceptButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(acceptButton, null);
// do accept stuff here..
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(cancelButton, null);
// do cancel stuff here..
}
private void comboBox1_Leave(object sender, EventArgs e)
{
// do leave stuff here..
Console.WriteLine(sender);
}
I also pass in the Button so you could check the sender to see how the Leave was triggered..
I'm answering my own question here as I feel it might be useful to other newbies.
The breakpoint I had set in my empty lostFocus event was stopping button click event from occurring. When I removed the breakpoint the problem went away.
However when I added code to my lostFocus event, a form redraw was sometimes moving the buttons and preventing their events from firing. To solve this problem I adapted TaWs very useful answer and fired the button event from within the lostFocus event.
private void comboBox1_LostFocus(object sender, EventArgs e)
{
bool saving = btnSave.ContainsFocus;
// form redraw stuff here..
if (saving)
btnSave_Click(btnSave, null);
}

C# Form Background Parameter

I have created a simple C# application to automatically login into a hotspot. I have a notify icon with a contextmenustrip with some functions like Connect, Disconnect, etc. I want to be able to run the form in the background showing only the notify icon to automatically login into the hotspot if the form is hidden.
I followed the instructions from VBNight in this post:
Hide form at launch
The application is running in the background, the notify icon showing but the Form_Load function is not working until I press on the notify icon.
I guess that's because Form_Load Occurs before a form is DISPLAYED for the first time.
Try moving your code from Form_Load to the constructor after InitializeComponent(); or so.
EDIT:
To answer your question, I suggest you extract code #1 from...
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// move this code #1 to...
}
and move the code to a brandnew method.
private void NewButtonClicked() {
// move code #1 here (in case)
}
Then, go back and call the method you just created.
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// You can leave code #1 but to remove duplicate,
NewButtonClicked();
}
Finally, replace YOUR_BUTTON.PerformClick(); with NewButtonClicked(); wherever you need. I assume you don't need any interaction with form Controls since the form is hidden.
I fixed it changing the form opacity to 0 and the ShowInTaskbar property to false. The form is hidden and the code is working.

Object Reference Not Set

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;
}

Disabling a Textbox Using TextChanged Event

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.

How can I give focus to a textBox after a Tab has been selected?

I'd like give focus to a textBox after a Tab has been selected but no matter what I try it doesn't work. I've looked at similar questions here but they don't get me the results I need. Here is what Ive tried.
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
and
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
Can someone please tell me what I'm doing wrong?
Thanks
First thing the Click event of the TabPage control fires when the user clicks inside the TabPage not on the header so your SelectedIndexChanged event is the one you want to use.
I just tested code very similiar to yours:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage2)
{
textBox4.Focus();
}
}
And it worked fine.
Is the password textbox not enabled or something like that?
If you try to call Focus() on a different control does that also not work?
If you set a breakpoint inside the SelectedIndexChanged code does it get hit?
Update: Interesting. If the breakpoint isn't getting hit (before the if) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.
Update: The easier way to attach an event handler to a control on your form:
1: Select the Control to want to attach an event handler to and then click the Events icon (lightning bolt) in the Properties window.
alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png
2: Find the event you want to attach to and double click to the right.
alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png
3: A code stub will be automatically generated for you and the event will be attached in the designer.
alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png
If you look at the properties window again you'll now see the name of the method that was generated.

Categories

Resources