I'm having a Windows Mobile CE application written in C# with .Net CF.
Consider i have 2 forms in my application:
List of objects (has a listview)
Details page (should appear when something is selected in previous listview)
Currently i'm attaching callback to listview's SelectedIndexChanged event, and open a new form there.
New form opens okay (in the midde of the event callback), but when i close the form(this.Close()), then the list page isn't clickable first time, after the first click UI is interactable again.
Also the the ListViewItem clicked at first step doesn't get selected(blue background).
Here's a short (12s) video showing this problem: http://take.ms/urkme
As you see from the video, after coming back from details screen, refresh button doesn't click on the first click..
I'm showing the details form like so:
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
(new FormDetails()).ShowDialog();
}
Is there any way to show the details form after event finishes, or am i doing it completely wrong?
PS! Tried the same with a Button and it's click event, then all worked nicely..
As I know ListView's SelectedIndexChanged event fired twice on almost case not like Button's Click event which fired once, this maybe what cause that weird interaction. Maybe try changing to ItemSelectionChanged event as suggested in here.
Your problem is caused by using the SelectedIndexChanged event. When you select an item in your list you'll set the SelectedIndex, if you select the same item again the index won't be changed so you'll never call the event.
You could use the Click event to trigger the wanted response.
Here is an example:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0]; //This is your selected item.
//do stuff
}
When you use ShowDialog() you open a form in modal mode. All further processing of following code will not take place until the modal form is closed or returns a DialogResult.
Better use a modeless form using .Show().
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
(new FormDetails()).Show(); //will not stop processing of events in mainForm
}
If the new FormDetails is finished, it can use a simple Hide or Close to bring up the main form to foreground.
Remember that the main form is still there and will not wait for the FormDetails being closed (as it is a modeless dialog).
Related
As the title suggests, I cannot get a single event to fire from an MDIChild application. No mouse event, no load, keypress, nothing at all.
private void btnSave_Click(object sender, EventArgs e)
{Console.WriteLine("Clicked");}
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
Selected the event from the properties window, manually subscribed to the event and yet nothing at all.
As the comments suggest you could insert a break point to see if these events are being fired.
From the information in your question it looks like the issue is that you're calling
Console.WriteLine
From a Winforms application.If you want to see somethng appear you could try
MessageBox.Show("Clicked")
Console applications would show Console.WriteLine, but the fact that you have buttons suggests a WinForms app.
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.
I added a System.Windows.Forms.ToolStripDropDownButton in my Windows forms application and now I'm trying to add a keyboard shortcut for clicking this button
However, when I invoke button.PerformClick() it simply doesn't open:
void _Cnc_KeyPress(object sender, KeyPressEventArgs ){
btnFiltros.PerformClick();
}
Is this by design? Is there another way to simulate the click or properly open the dropdown?
Edit
The reason I'm doing this is I have a working application that is going to be used in a mouseless device, so I have to make the whole navigation possible from the keyboard
Does your form have the KeyPreview property set ? You need it to receive all key events.
When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus.
Form http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx
For the toolstripdropdownbutton you have to select the drop down item first. The following code snippet shows how to do that.
// This method shows the drop-down for the first item
// in the form's ToolStrip.
private void showButton_Click(object sender, EventArgs e)
{
ToolStripDropDownItem item = this.toolStrip1.Items[0] as ToolStripDropDownItem;
if (item.HasDropDownItems)
{
item.ShowDropDown();
}
}
I have another issue in converting my Winforms program to a WPF program. In my first program, I had a smaller window open to allow the user to adjust some data, and then when it closed, the other form was activated again with the new data.
I used form2.ShowDialog(); to open the form, which automatically makes the parent form deactivated in Winforms. This way when I closed form2, the parent form was activated, and I was able to use an event handler form1_Activated to reload and re-initialize some of the settings successfully.
However, now when I attempt to do the same thing with WPF, I am still able to open form2 using form2.ShowDialog();, but then when I close the form, it does not register the form1_Activated event handler. Instead, in order to reload the settings, I must click on another window, and then come back into my program to register the form1_Activated event handler.
Am I just doing something wrong, or is there another event handler that I should be using in WPF to achieve the same thing I was able to do in Winforms?
Calling ShowDialog() causes the dialog box top appear in modal mode so I don't understand why you would need an event handler to process the results after the dialog box is closed. Keep in mind that you can access public variables in the DialogBox, as well. If I understand your question, this should do what you are asking:
MainWindow:
My_DialogBox dlg = new My_DialogBox();
dlg.Owner = this;
dlg.MyPublicVariable = ''; //some value that you might need to pass to the dialog
dlg.ShowDialog(); //exection of MainWindow is suspended until dialog box is closed
if (dlg.DialogResult == true)
{
//dlg.MyPublicVariable is still accessible
//call whatever routines you need in order to refresh the main form's data
}
DialogBox:
private void OK_Button_Click(object sender, RoutedEventArgs e)
{
MyPublic variable = something; //accessible after the dialog has closed.
this.DialogResult = true;
}
private void Cancel_Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
The MSDN write-up on dialog boxes is pretty good. There may be some tips that might help you even more:
http://msdn.microsoft.com/en-us/library/aa969773.aspx
Good luck!
I currently have a tabcontrol on my windows form in visual studio, i would like it so that when the user clicks on a different tab that i can execute some code (for example populate a listbox).
when i double click on the tab it only brings up an onclick event for the body of the tabcontrol.
i was thinking that i may have to create a thread in the form load that will constantly check whether the tab index changes and if it does then execute some code. but surely there must be an easier way?
You can use TabControl.Selecting or TabControl.SelectedIndexChanged event
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
//Your code goes here.
}
You should look at TabControl events
http://msdn.microsoft.com/en-ie/library/system.windows.forms.tabcontrol.selecting.aspx