I am working on a wpf .My requirement is to change selection of tab according to user confirmation it means every time when user changes tab a message box opens and confirm with user whether he wants to change the tab or not.
But problem with me is when I press no first time it works fine .but after that on second time it asks two times for user confirmation
can anyone help me to solve this ?
private void tabcontrol_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
try
{
if (handleSelection && e.OriginalSource == tbUserProfileMainControl)
{
//Ask user for change
if (isUserAllowedToChanged)
{
int currentIndex = (tabcontrol.SelectedIndex);
GeneralDeclaration.currentSelectedTabIndex = currentIndex;
LoadUserControl(GeneralDeclaration.currentSelectedTabIndex);
}
else
{
//e.Handled = true;
handleSelection = false;
tbUserProfileMainControl.SelectedIndex = Math.Abs(tbUserProfileMainControl.SelectedIndex - 1);
}
}
handleSelection = true;
}
catch (Exception ex)
{
//
}
}
It sounds like you're adding handlers during the click event itself. This causes your subsequent click to perform the action one more time (3rd click 3 times, 4th click 4 times, etc).
Check how you bind the event to the handler and check where you are defining the handler itself. You're doing something twice that should only be done once.
This is my estimation based on your findings, without code, I'm just taking a wild stab in the dark.
Related
I am very new to the programming world and recently dove into c#. I don't want to waste your time so I'll get right to it. I wanted to create a program just to test my knowledge, and thought I could attempt to execute specific blocks of code based on which key on the keyboard is pressed by the user. I tried doing this by creating an event handler that contained if statements, but then realized I didn't know how to have the event handler active in the program.
For example, and as you can see in the below snippet, after the WriteLine in Line 5 lets say I wanted to raise the EventKeyPress event so that it waits for user input and reads the key they have pressed and reacts accordingly, how would I do that?
Again, I'm almost a complete beginner and have searched around for explanations about event handlers for hours and still can't wrap my head around what I am supposed to do or if I am even using the event handler correctly. Thanks in advance!
static void Main();
{
if (search == "Ball")
{
Console.WriteLine("Press enter to exit or backspace to return to the search bar")
// RIGHT HERE
}
else
{
Console.WriteLine("Sorry, I don't recognize {0}", search);
}
void EventKeyPress(object sender, KeyPressEventArgs e)
{
for (int i = 0; i < 1;)
{
if (e.KeyChar == (char)Keys.Enter)
{
// exit app
}
else if (e.KeyChar == (char)Keys.Back)
{
// go back to search
}
else
{
i = 0; // error
}
}
}
}
So, you're asking for something that involves Threading which is not a beginner thing to accomplish at all. The best way to do this for a beginner is to ask for a prompt, then accept as an input. For example.
Console.WriteLine("Hello, what's your name?");
string nameStr = Console.ReadLine();
Console.WriteLine($"Hello, {nameStr}");
You can then use your variable and apply it to an if/while or whatever kind of conditional.
if (nameStr == "Matt"){
//Do This Code.
}
Once you have that code, add a sequential method that will ask the user to return to the main menu or whatever you want it to do.
Main.ReturnMenu(); //Or whatever you want to use.
I would like to have a different method called depending on the number of time I click on a button, from 1 to 5, on an UWP application.
Is it possible ? How ?
I've used something like this in my windows forms application, maybe it will help. This switch operation was inside button click event.
int clickCounter = 0;
switch (clickCounter)
{
case 1:
{
}
break;
case 2:
{
}
break;
}
Well, one or two clicks is built-in:
var b = new Button();
b.Tapped += (s, e) => { /* TODO */ };
b.DoubleTapped += (s, e) => { /* TODO */ };
But if you want three clicks, say, you need john.kernel's solution (above). Of course, you would use only Tapped to track, then some kind of timing threshold, too. Otherwise they could tap twice in 5 minutes and John's code would fire.
I don't mind interjecting that a triple-click is lame. Not discoverable. But, hey, I don't know your app or your user base, do I? So, you do what you think is right. You are the developer.
Best of luck!
I'm recording Coded UI Tests with VS 2012, which shall test the functions of a web application.
After I loaded the web page, I click on a button to start p.e. a job application.
After the next page has loaded on the same site, my problem begins.
The entry controls are at the end of the web site.
To take a look and input data into the entry controls, I must scroll down.
The recording produced the following method in the UIMap.
Designer.cs:
public void Scrollen()
{
#region Variable Declarations
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
this.UIGoogleMozillaFirefoxWindow.UIItemPropertyPage.UIBewerbungDemoFirmaDocument.WaitForControlExist();
this.UIGoogleMozillaFirefoxWindow.UIItemPropertyPage.UIBewerbungDemoFirmaDocument.WaitForControlReady();
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
WinControl uIBewerbungDemoFirmaDocument = this.UIGoogleMozillaFirefoxWindow.UIItemPropertyPage.UIBewerbungDemoFirmaDocument;
#endregion
// Click "Job application" document
Point pt = new Point(1390, 553);
int count = 0;
while (!uIBewerbungDemoFirmaDocument.TryGetClickablePoint(out pt) && count < 20)
{
count++;
System.Threading.Thread.Sleep(10);
if (count == 20)
Console.WriteLine("ClickablePoint not found");
}
Mouse.Click(uIBewerbungDemoFirmaDocument, new Point(1390, 553));
Mouse.MoveScrollWheel(10);
}
As You can see, I tried WaitForControlExist, WaitForControlReady, TryGetClickablePoint and the method MoveScrollWheel.
But neither Mouse.Click nor Mouse.MoveScrollWheel are working.
And in the next method, where I click into the first of the entry fields, I get a message at execution time, that the click event produces an error, because the control is hidden (because it's down below on the website, out of visible range).
After several tests this is making me crazy.
Any idea what has gone wrong and how can I scroll down the web site, so my entry controls are in visible range?
You can try Control.EnsureClickable(). Or you can use below mentioned function to scroll the page until the control is not clickable.
public static void ScrollAndClick(HtmlControl Control)
{
bool isClickable = false;
if (Control.TryFind())
{
while (!isClickable)
{
try
{
Control.EnsureClickable();
Mouse.Click(Control);
isClickable = true;
}
catch (FailedToPerformActionOnHiddenControlException)
{
Mouse.MoveScrollWheel(-1);
throw;
}
}
}
else
{
throw new AssertInconclusiveException("Control Not Found");
}
}
You can also add condition related to timeout to make sure it don't go to infinite loop.
Let me know if you are having issue with this at your end.
How to restore a Bound datarow (add the row back to the DataGridView) after it has been removed?
When I click the delete key on the left side of a DataGridView the UserDeletingRow event fires and I can capture the Entity object (AnswerTable) that is being removed from the list.
private void visitorHostDataGridView_UserDeletingRow (object sender, DataGridViewRowCancelEventArgs e)
{
if (e.Row.DataBoundItem is VisitorHost)
{
DatatoDelete datatoDelete = new DatatoDelete (e.Row, e.Row.DataBoundItem as VisitorHost);
visitorHostsDataRowToDelete.Add (datatoDelete);
SetModified (); // turns on save and cancel buttons
}
}
Later I do a delete when save happens
public override void OnSave ()
{
if (visitorHostsDataRowToDelete.Count > 0)
{
foreach (DatatoDelete item in visitorHostsDataRowToDelete)
{
_context.AnswerTables.DeleteObject (item.VisitorHostRecord as VisitorHost);
}
}
visitorhostBindingSource.EndEdit ();
_context.SaveChanges ();
ClearModified (); // turns off save and cancel
MessageBox.Show ("Saved");
}
Again, My question is how to I restore a removed row (cancel my delete) and add the objects back into the DataGridView - after I had clicked delete. (I don't need a conformation, I'm talking about I've done some work and realized I made a mistake and want to start over) I don't want to go back to the database and I shouldn't need to I have all the data, just not sure how to use it.
UPDATE:
I had tried List.Add(EntityObject) by itself with no luck (see below). Tried again due to comments. Updated my code with these three things, in this order:
this.visitorhostBindingSource.DataSource = null;
this.visitorhostBindingSource.DataSource = _hosts;
this.visitorHostsDataGridView.Refresh ();
Seem to solve the issues I was having, add alone didn't make it show on the screen. But not sure why I need to do the steps I am doing. Any additional thought would be useful.
public override void OnCancel () // called from on click event of cancel button
{
foreach (DatatoDelete dataGridViewRow in visitorHostsDataRowToDelete)
{
_hosts.Add (dataGridViewRow.VisitorHostRecord);
}
visitorHostsDataRowToDelete.Clear();
_hosts.Sort ();
this.visitorhostBindingSource.DataSource = null;
this.visitorhostBindingSource.DataSource = _hosts;
this.visitorHostsDataGridView.Refresh ();
ClearModified (); //disable save and cancel buttons
}
Using return in your event handler method only exits this method and not the whole event handler chain. DataGridView does some things before your handler is called and afterwards (where your row is removed from the BindingSource).
To state that you really want to cancel the whole event you have to use e.Cancel = true;.
So to cancel all the stuff done after your handler is called write:
if(answerTableDataGridView.SelectedRows.Count>1)
{
// multiselect is false so we should never hit this.
MessageBox.Show ("only one delete at a time allowed,\n Rows removed from view but NOT DELETED from the database.");
e.Cancel = true;
return;
}
CancelEventArgs.Cancel Documentation
As noted in question
I had tried List.Add(EntityObject) by itself with no luck (see below). Tried again due to comments. Updated my code with these three things, in this order:
this.visitorhostBindingSource.DataSource = null;
this.visitorhostBindingSource.DataSource = _hosts;
this.visitorHostsDataGridView.Refresh ();
Seem to solve the issues I was having, add alone didn't make it show on the screen. But not sure why I need to do the steps I am doing. Any additional thought would be useful.
public override void OnCancel () // called from on click event of cancel button
{
foreach (DatatoDelete dataGridViewRow in visitorHostsDataRowToDelete)
{
_hosts.Add (dataGridViewRow.VisitorHostRecord);
}
visitorHostsDataRowToDelete.Clear();
_hosts.Sort ();
this.visitorhostBindingSource.DataSource = null;
this.visitorhostBindingSource.DataSource = _hosts;
this.visitorHostsDataGridView.Refresh ();
ClearModified (); //disable save and cancel buttons
}
Not sure if i will be able to formulate my question quite clear but let me try:
So i've written a small piece of code which will give the user the option to select a desired status for his Office Communicator when his PC get locked ( by default it goes automatically on status "away" ) .So here it is the Windows Form wich is basically a combobox and a button .The Combo has four option "Away" , "Busy" , Do not Disturb" and "Online" respectively. All seems fine and the program compiles ok.The Menu appears , you select the status you wish , push the button and then lock your PC - so far all goes perfect.Your Status is as selected .And now comes the Problem.You unlock your PC and try to select a different status , same actions , but when you lock the PC it still shows the previously selected status here is the Button_Click method
public void Btn_Click(Object sender, EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
if (Comb.Text == "Away")
{
MessageBox.Show("Saved ! \nYour Status will be 'Away' ");
Method2();
}
else if (Comb.Text == "Busy")
{
MessageBox.Show("Saved ! \nYour Status will be 'Busy' ");
Method1();
}
else if (Comb.Text == "Do Not Disturb")
{
MessageBox.Show("Saved ! \nYour Status will be 'Do Not Disturb' ");
Method3();
}
else
{
MessageBox.Show("Saved ! \nYour Status will be 'Online' ");
Method4();
}
But.Enabled = true;
// Display the greeting label text.
}
So the 4 methods ( Method1 () , 2 ... etc ) are the one to change the status depending on the text in the combo box drop down menu ( the status you select )i have tested all methods separately from each other and they work beautiful thereforfe i exclude a problems with them , is it some logical error ?
Nikolay, give SharpDevelop's debugger a try. In your code's margin click once next to the line if (Comb.Text == "Away") and then hover over the variable names to see what they are set to each time it runs. You can use the "Step over" "Step into" and "Step out" functions to "Execute the highlighted method without looking at the internals", "Debug the Internals of a method" or "Run the current method to the end and then show the next level up" respectively.
If you do this you'll figure out why you're getting an error and it will be MUCH easier to determine where the error is coming from. (For instance, if a variable is set to an unexpected value you'll know to figure out when that changed).
static void SystemEvents_SessionSwitch1(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
System.Threading.Thread.Sleep(500);
CommunicatorAPI.MessengerClass comm = new CommunicatorAPI.MessengerClass();
if (comm.MyStatus==MISTATUS.MISTATUS_AWAY)
{
SetMyPresence1 ();
} else if (e.Reason == SessionSwitchReason.SessionUnlock)
{
ChangeStatus1 ();
}
}
}