TabControl avoid changing to another tab - tabcontrol

I have a tab control with 2 tabs.. When the user clicks on the second tab, it does some validation and then if that validation returns false, the user gets a message indicating to go back. Now, here's my problem, it changes tabs anyways with the code below:
Although the user doesn't see the tab 2, it is showing as changed.
private void tabprincipal_SelectedIndexChanged(object sender, EventArgs e)
{
if (!saved_plan)
{
MessageBox.Show("You need to save a plan first.");
return;
}
How can I avoid this behavior? I want to display the message and the user to remain in the first tab
I think I'm looking for an event prior to the selectedindexchanged to detect that the user clicked tab2 and then don't let him move..

I actually found a way using the Deselecting method of the TabControl
private void tabprincipal_Deselecting(object sender, TabControlCancelEventArgs e)
{
if (!saved_plan)
{
MessageBox.Show("You need to save a plan first");
e.Cancel = true;
}
}

Related

Devexpress pivotgirid customization form for fields

I have a button on ribbon control.When I click it I open a customization form which shows pivotgrid's fields and I drag items from customization form to the pivot grid but when I close and reclick the button to open the customization form the draged fields are not shown in the pivot.I have to reselect the fields to show on the pivot.
How can I avoid this?
Below code is for button click event.
private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
{
pivotGridControl1.RetrieveFields(PivotArea.FilterArea, false);
pivotGridControl1.FieldsCustomization();
}
The PivotGridControl.RetrieveFields method with your parameters are hiding all fields. If you remove this method from your code, then you will avoid your situation.
private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
{
pivotGridControl1.FieldsCustomization();
}

Enable/disable an ordinary button on Form2 from Form1

I need some help. I've been reading this site for days, and have read a lot of tips about controlling for example a button's property from another form. There's even a video on Youtube, which works to me as stand alone, but when I implenet it in my application it throws a NullReferenceException.
Let's say that I have a toolstrip menu on Form1. A click on the Kalibracio option opens the second form (also called Kalibracio - not Form2). Then, click on Proba in the menu should disable an ordinary button on the Kalibracio form which propery is set to public. The code on Form1 is as follows:
private void kalibracioToolStripMenuItem_Click(object sender, EventArgs e)
{
Kalibracio Kalibr = new Kalibracio(this);
Kalibr.Owner = this;
Kalibr.Show();
}
private void probaToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Application.OpenForms.OfType<Kalibracio>().Any())
(this.Owner as Kalibracio).button1.Enabled = false;
// the above line throws a NullReferenceExcteption if Kalibracio form is open (Kalibracio is null)
}
What am I missing?
LoL just had to declare globaly an instance of Kalibracio, open it and then access it's properties from all other methods.
I tried this approach at first, but my problem was that I was creating and instance locally, then I had to create another one in some other method because I couldn't address the former one created locally, and ofc it didn't work...

Postback parent page when using an ASP.NET ModalPopup control

I have a custom UserControl that displays a modal popup (from the Ajax Toolkit). The control allows the user to add a note to a customer record which the parent page displays in a GridView.
I'm unable to force the parent page to reload the grid after the user clicks the "Add Note" button on the modal popup and closes it. The note is added to the database correctly, but I have to manually refresh the page to get it to display instead of it automatically refreshing when I save+close the popup.
You can use a delegate to fire an event in parent page after note is added to the database.
// Declared in Custom Control.
// CustomerCreatedEventArgs is custom event args.
public delegate void EventHandler(object sender, CustomerCreatedEventArgs e);
public event EventHandler CustomerCreated;
After note is added, fire parent page event.
// Raises an event to the parent page and passing recently created object.
if (CustomerCreated != null)
{
CustomerCreatedEventArgs args = new CustomerCreatedEventArgs(objCustomerMaster.CustomerCode, objCustomerMaster.CustomerAddress1, objCustomerMaster.CustomerAddress2);
CustomerCreated(this, args);
}
In parent page, implement required event to re-fill grdiview.
protected void CustomerCreated(object sender, CustomerCreatedEventArgs e)
{
try
{
BindGridView();
}
catch (Exception ex)
{
throw ex;
}
}
In your case, you can not use any custom event args, and use EventArgs class itself.

.Net CF Prevent Overzealous, Impatient Clicking (while screen is redrawing)

.Net Compact Framework
Scenario: User is on a screen. Device can't finds a printer and asks the user if they want to try again. If they click "No", the current screen is closed and they are returned to the parent menu screen. If they click the "No" button multiple times, the first click will be used by the No button and the next click will take effect once the screen has completed redrawing. (In effect clicking a menu item which then takes the user to another screen.)
I don't see a good place to put a wait cursor...there isn't much happening when the user clicks "No" except a form closing. But the CF framework is slow to redraw the screen.
Any ideas?
you can skip pending clicks by clearing the windows message queue with
Application.DoEvents();
We use the following custom Event class to solve your problem (preventing multiple clicks and showing a wait cursor if necessary):
using System;
using System.Windows.Forms;
public sealed class Event {
bool forwarding;
public event EventHandler Action;
void Forward (object o, EventArgs a) {
if ((Action != null) && (!forwarding)) {
forwarding = true;
Cursor cursor = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
Action(o, a);
} finally {
Cursor.Current = cursor;
Application.DoEvents();
forwarding = false;
}
}
}
public EventHandler Handler {
get {
return new EventHandler(Forward);
}
}
}
You can verify that it works with the following example (Console outputs click only if HandleClick has terminated):
using System;
using System.Threading;
using System.Windows.Forms;
class Program {
static void HandleClick (object o, EventArgs a) {
Console.WriteLine("Click");
Thread.Sleep(1000);
}
static void Main () {
Form f = new Form();
Button b = new Button();
//b.Click += new EventHandler(HandleClick);
Event e = new Event();
e.Action += new EventHandler(HandleClick);
b.Click += e.Handler;
f.Controls.Add(b);
Application.Run(f);
}
}
To reproduce your problem change the above code as follows (Console outputs all clicks, with a delay):
b.Click += new EventHandler(HandleClick);
//Event e = new Event();
//e.Action += new EventHandler(HandleClick);
//b.Click += e.Handler;
The Event class can be used for every control exposing EventHandler events (Button, MenuItem, ListView, ...).
Regards,
tamberg
Random thoughts:
Disable the some of the controls on the parent dialog while a modal dialog is up. I do not believe that you can disable the entire form since it is the parent of the modal dialog.
Alternatively I would suggest using a Transparent control to catch the clicks but transparency is not supported on CF.
How many controls are on the parent dialog? I have not found CF.Net that slow in updating. Is there any chance that the dialog is overloaded and could be custom drawn faster that with sub controls?
override the DialogResult property and the Dispose method of the class to handle adding/remvoing a wait cursor.

Ajax Modal Popup Display Issue

On the web page, there is gridview control contains product ID's. bind to a link button.
On ItemCommand event of gridview, I fetch the product information and display in the ajax modal popup extender control. The popup is programatically show on ItemCommand of gridview and also hide programatically.
Now the problem is that, when i close popup after showing first product details and try to see next 1 by clicking on other product ID.., sometimes details are displaye dand sometimes not.
The data comes from database is fetched as well for each product.
Plz help.
I do the same but i have no such problem. So i am pasting code here that may b help full to u.
CODE:
protected void GVallusers_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
GridViewRow gvRow = ((GridView)sender).Rows[e.NewEditIndex];
populatepanel(gvRow);
ModalPopupExtender1.Show();
}
catch (Exception exc)
{
lblinfo.Text = exc.Message;
}
}
public void populatepanel(GridViewRow gvrow)
{
string userid = gvrow.Cells[0].Text;
lblSite.Text=gvrow.Cells[1].Text;
lblemail.Text = gvrow.Cells[3].Text;
}