Intercept ListView Column disposing - c#

Environment: C# WinForm Application (Framework 4.0)
Objective: read some data (Ex. column width) from a ListView just before the containing form is closed.
I don't wanna read the data EveryTime the column size is changed....
If i check Listview.Columns.Count inside the FormClosing event, i always find that the list is ALREADY empty.
That means Columns List is getting cleared just before FormClosing event.
How can i get column width (Or other columns-related info) just before the form is closed?
What is the event i am looking for? I use C#
Thanks in advance guys...you're the best!

You can use
protected override void OnClosing(CancelEventArgs e)
{
}
Event, this will be called before form_closing event.
I have checked your scenario both in form_closing and onclosing column has the count and you can get the details in both cases . If this does not worked out for you Please post some code so that anyone could help.

Related

How do I code a DataTable.RowChanged event to be attached at compile time?

I have the following setup in a Windows Forms project:
myDataGridView, whose data source is myBindingSource, whose data source is myProjectDataSet and whose data member is myDataTable, whose data is populated from a database at runtime per user request.
When the user makes a data change on myDataGridView I have the changes filtering down to update myDataTable as well. I then want to handle myDataTable's RowChanged event, but I'm not quite sure in which project file I should put the code to attach the event handler and then handle the event so that they are hooked up when the data table is "initialized". I know I can hook up the event in my Form.cs file after the form loads, but I'd prefer to hook it up in myProjectDataSet.cs if I can. But, since myProjectDataSet.cs is auto-generated, I'm not sure if this is the best place to do it.
I hope that made sense. Thanks for your input.
Thanks, realized I had to add to hook it up in my Form.Designer.cs file after myProjectDataSet instantiated.
this.myProjectDataSet.myDataTable.RowChanged += new System.Data.DataRowChangeEventHandler(myDataTable_RowChanged);
Then, of course, I handled the event in Form.cs
private void myDataTable_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
{
//Do stuff.
}
But now the hook is getting wiped out when Form.Designer.cs auto-generates. Oh well I'll figure that one out too.

In C#, How to cancel the default mouse click event when using bindingNavigator

I am a new C# user and now I have encountered a problem in using BindingNavigator.
I am using bindingNavigator to update records in a table of database. Before I leave current updated record and enter the next record by clicking Next button, I will perform some validation, if there is any thing incorrect, I hope it could raise a warning to give me chance to correct the wrong fields instead of moving to the next record.
I added some lines in bindingNavigatorMoveNextItem_MouseDown event, but it still move to next item even there are some thing wrong with current record(fields have some logical connection). Can any expert here help me out about that? Many Thanks!
You have two approaches: either overriding WndProc and prevent mouse click window message from calling the base's WndProc, or simply overriding OnMouseClick:
class Hello : BindingNavigator
{
private bool canFire = false;
protected override void OnMouseClick(MouseEventArgs e) // second approach
{
// don't call base method so that event doesn't fire up
if (this.canFire)
base.OnMouseClick(e);
}
}
I know this is old.. but for anyone else...
You should use the normal buttons, and just use the validating event, canceling if anything fails your validation.
The control does not display the property in the designer, but you can still set it : bindingnavigator.CausesValidation = true;
I do this in the form load.
this alone still won't do it.
you also need to set the focus.
bindingnavigator.focus();
I do this in the bindingnavigator_ItemClicked event so it happens no matter what button is clicked.

Handling OnSelectedIndexChanged before SelectionChangeCommitted?

I'm working on an async binding control that inherits a ComboBox (and a Textbox in other scenarios, but that's not really relevant..). I've replaced a bunch of existing ComboBoxes on the forms in this application, which for most of these controls has worked perfectly. Except for one...
My box overrides the OnSelectedIndexChanged method with the following code:
protected override void OnSelectedIndexChanged(EventArgs e)
{
if (!Helper.ItemsApplied)
return; //don't worry! the index will be selected when the items are applied!
_selectedValue = Helper.GetValue(Text);
base.OnSelectedIndexChanged(e);
}
This works fine, until someone tries to handle the value change using the SelectionChangeCommitted event. Perfectly within reason though; I shouldn't be changing their code to use the SelectedIndexChanged, because this functionality shouldn't execute when the value is programmatically invoked (see the MSDN Article).
However, my functionality does need to be executed when the index is programmatically changed, but it needs to execute before the SelectionChangeCommitted event does! The SelectionChangeCommitted event is fired before the SelectedIndexChanged event.
Any ideas on how I could get round this? Preferably only by changing the code in my control and not in their form?

how to load form with many controls Smooth on Winform? [duplicate]

This question already has an answer here:
Winforms Double Buffering
(1 answer)
Closed 5 years ago.
I have a C# program with a DataGridView on the form.
When the user selects a row and presses a button, I present control panel with some textboxes that fill from the DataGridView.
When the control panel is shown, I see the controls building on the screen and it's not pleasing to the eye.
How do I show this control panel instantly?
Thanks in advance
Control.SuspendLayout can be used to stop the control painting while you modify it and ResumeLayout when you are done
If the drawing is too slow you can finesse the control double buffering. Thanks to Sergio for the link.
I had the same problem. I use BackgroundWorker on the the Load event handler of the form. That solved my problem.
private void MainForm_Load(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
//Write here all the initial code that should be in the Load event handler.
}
One of two situations are occurring:
Either: you have a very large number of controls on the panel.
Or: you are doing some other data loading in the middle of populating the panel.
For the first one, the easiest thing I know of to get the control handles going is to draw it off screen or behind another control and then reposition it when it's all ready.
For the second one, I'd create the panel first then connect the bindings / populate afterwards.
Without some sample code about how you create the panel and how you supply the data, I can only speculate.
I have faced the same issue, thanks to "Adam Houldsworth" answer [SOLVED] :
1- position all controls that contain all other controls to some position out of screen.
Say, to (2000, 2000)
2- make them inVisible (try, someControl.Visible = false)
3- on Form_Shown event :
return the controls you move to their actual locations
make them Visible (try, someControl.Visible = true)
it was satisfied and much better.

combobox in C# not getting populated

I have a windows forms app in C#. Platform is vS 2005.
Following is the piece of code:
namespace HostApp
{
public partial class Form1 : Form
{
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Add("Apples");
comboBox2.Items.Add("Oranges");
comboBox2.Items.Add("Grapefruits");
}
}
}
I run the app but the fruit names do not show up in the drop down of comboBox2. I am sure I am missing some line of code to "populate" the drop down with the entered values.
Any help would be much appreciated.
Thanks,
Viren
You add the items in the handler for the SelectedIndexChanged event. You need to move the code to InitializeComponent or another appropriate place.
Please check the following things:
You have added AutoPostBack="true" in the combo-box so that the selectedChange event is fired and post back happens.
Make sure you have nothung in ur Page load which refreshed the combo box. You can use IsPostBack to acheive loading of the values.
Your items are being added when the selected item is changed, but as there are no existing items this will never happen. Move those lines to the constructor for Form1 and it'll work.
The code you provided will only add items to comboBox2 when the selection changes in the control that is hooked up to comboBox2_SelectedIndexChanged.
There are two concepts at play here: Control Initialization/Databinding, and event handling.
The code you have written essentially says "If somebody selects something new in the combo box, add these 3 options to the combo box". That would happen every time the selected index changes in the combo box. This, of course, assumes you have even hooked up this event handler to the combo box to begin with. This is event handling.
What you are probably trying to do is initialize the control. This happens when you load the page and want to setup the initial options available in your page controls. Using the Init or Load event is probably where you want to setup the choices in your control. This is also when you would initialize your event handlers to say "When something happens, do this".
Move the code to the Page_Load event ...
The SelectedIndexChanged only fires when the ComboBox index has changed AND AutoPostBack = True.
EDIT: Sorry, it's a Form, I was thinking web ... move to Form_Load
For people having difficulties with autopostback and viewstate, beware of the page_load event.
If have been getting on this page alot when trying to google, so that's the reason i'll post it here.
If you fill your dropdownlist (or any other control) in the page_load method, be sure to write an extra control is there is a postback (triggered when changing value of a dropdownlist).
If you don't make that control, your controls will be refilled.
That mistake took me a while to figure out.
So what i'm saying is
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//fill your controls here
}
}

Categories