|
The way Heath describes is the correct way to handle this type of situation.
The really bad practice, which you should not do (but is handy when you just want to throw something together as a quick test or throwaway util, is;
int i = 0;
while (i<2000000000)
{
if (i==5000) {
pictureBox1.Load("x.gif");
Application.DoEvents();
}
i++;
}
This forces the message pump to catch up.
But as i said already, this is bad practice, and should not be used in production code and will probably lead to you being shot by the code police
o see why you should not be using it in production code, look here; http://blog.somecreativity.com/2007/11/19/a-look-at-doevents/[^], or search Google for DoEvents and it will become obvious its baaaaaaaad.
So why tell you about then? Well, rather than you find out on your own and not know its bad and start to use it, its better to be told about it and learn why its bad and should be avoided, if that makes sense!
|
|
|
|
|
abdellah ab wrote: if (i=5000) {
I'm surprised that even compiles!
However, you may need to force a refresh of the control, not that I recommend you do that.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
|
I think he was writing pseudo code, as there are a few ommissions
|
|
|
|
|
Indeed, and we knew what he meant. I'm very happy that doesn't compile, actually, because I do it accidentally quite a lot and if I were writing C I would get my fingers burnt every time.
|
|
|
|
|
Hi wold of CodeProject!
How can i Create a dialup Connection via C# Windows Application.
and also i wanna to dialup to internet directly via the connection i created.
Thanks!
|
|
|
|
|
|
I fear this question has been asked a thousand times, but the keywords I can think of are too common and give millions of answers and I can't find the correct one.
In a Windows forms application (C# 2008) I have a base form with a datagridview on it, and I want to catch the cell click events on the descendants. If the contents of the datagridview is clicked I have to create a custom event, but if columnhead is clicked I allways want the same event.
I hoped I could achieve this by defining the common event in the base class, followed by the custom event in the descendant. I defined a break in the code of the base class when column head is clicked, the break is correctly handled, but the code resumes in the descendant's form class.
How can I stop the code execution in the descendant form if I have a break in the base class ?
Thanks
Rob
|
|
|
|
|
That's how subtyping works. When you crete an instance of the child class, all code is executing against that instance. If the base class defines handlers for events like in your case, it still executes against that instance of the child class you created.
But what is it that's not working? Both events can be defined on the base class, like so:
internal abstract class MyFormBase : Form
{
DataGridView dataGridView1;
protected MyFormBase()
{
dataGridView1 = new DataGridView();
dataGridView1.CellClick += OnDataGridViewCellClick;
}
protected virtual string Message
{
get { return "Message from the base class."; }
}
protected virtual void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
{
}
protected virtual void OnDataGridViewColumnHeaderMouseClick(object sender, DataGridViewBindingCompleteEventArgs e)
{
MessageBox.Show(Message, "Test");
}
}
internal class MyChildForm : MyFormBase
{
protected override string Message
{
get { return "Message from the child class."; }
}
protected override void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("You clicked a cell.", "Test");
base.OnDataGridViewCellClick(sender, e);
}
}
Now when you create an instance of MyChildForm and click a column header, you'll see "Message from the child class" even though the event handler is defined in the base class. It calls the virtual (overridable) property which IS the property on the MyChildForm instance. If you want to call the base class's method, use base .
You also overrided the CellClick event handler which will show "You clicked a cell." I also show an example of calling the base class's method though in this case it doesn't do anyway. If it would never do anything, define it as abstract instead of virtual and remove the body like so:
private abstract void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e);
Hopefully this shows you an example of how polymorphism is working such that you can define your handlers in your base class but have it access the data - expectedly - in the child class.
This posting is provided "AS IS" with no warranties, and confers no rights.
Program Manager II
Visual Studio Professional Deployment Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your answer, although it is not what I wanted to hear....
Perhaps I was unclear, I'm creating an application with over a dozen forms with a datagridview on it. The datagridview contains buttons, and their action is dependent on the child form.
In the cellclickevent I get the rownumber, and when it's -1 the columnheader is clicked. I want all child forms to ignore the click of the columnheader and I wanted to achieve it like:
child form:
protected override void dgvSelect_CellClick(object sender, DataGridViewCellEventArgs e)
{
base.dgvSelect(sender, e);
custom cell click events;
}
I will solve it like this:
protected override void dgvSelect_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (base.dgvSelectHeaderClicked(sender, e) == true)
{
return;
}
custom cell click events;
}
Thanks for your time.
Rob
|
|
|
|
|
Holy sh*t! A rare sighting of the elusive Heath Stewert!!
I am honored to be in the presence of the greatness of one of CP's legends.
|
|
|
|
|
Thanks!
I might have to make that first one part of my sig.
This posting is provided "AS IS" with no warranties, and confers no rights.
Program Manager II
Visual Studio Professional Deployment Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
how to draw a circle using double buffering.
please give coding
|
|
|
|
|
Please give effort and show what you have tried. Without that you will not get anything good from us.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Drawing a circle: Graphics.DrawElipse[^] - you can get the graphics context from the Paint event PaintEventArgs.Graphics[^] property.
Double Buffering: see the Control.DoubleBuffered[^] property for the simple way, or Double Buffered Graphics[^] if you need something more complex.
You could have found all this very easily by using Google...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
I'd do something that looked a little bit like this:
public abstract class Shape
{
protected Rectangle { get; private set; }
public Shape(Rectangle rectangle)
{
SetBounds(rectangle);
}
public void SetBounds(Rectangle rectangle)
{
this.Rectangle = rectangle;
}
public abstract void Draw();
}
public class Circle : Shape
{
public void Draw()
{
}
} That's the hard bit done - I've left the easy bit for you to fill in.
|
|
|
|
|
Hi,
How can I define a class that is enable to add properties dynamically to it?
Best wishes
|
|
|
|
|
There could be someways to do this.
Using an indexed property is an easy way to do this.
Generics provides a way to do this as well[^].
Reflection could also be used for this too.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
The easy way to do this is to add a class that inherits from DynamicObject[^].
|
|
|
|
|
I am trying to develop an application with list view having CheckBoxes and Multiple Selection property as TRUE.
Single Selection works perfectly. But when I try Multiple selection, check box of all the currently selected rows get changed on every selection (and sets to not of the check value of the new selection)
Am I missing any event that I need to handle???
Please help me guys!!!
Thanks in advance...
Although I haven't handle any event, I am still giving the code (I think it'll be irrelevant)
<br />
<br />
this.listView.CheckBoxes = true;<br />
this.listView.FullRowSelect = true;<br />
this.listView.HideSelection = false;<br />
this.listView.UseCompatibleStateImageBehavior = false;<br />
this.listView.View = System.Windows.Forms.View.Details;<br />
<br />
<br />
public partial class Form1 : Form<br />
{<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
<br />
listView.Columns.Add("One", 100, HorizontalAlignment.Left);<br />
listView.Columns.Add("Two", 100, HorizontalAlignment.Left);<br />
listView.Columns.Add("Three", 100, HorizontalAlignment.Left);<br />
listView.Columns.Add("Four", 100, HorizontalAlignment.Left);<br />
<br />
for(int i = 0; i < 50; i++)<br />
{<br />
ListViewItem item = new ListViewItem();<br />
item.Checked = true;<br />
item.SubItems.Add("Garbage1");<br />
item.SubItems.Add("Garbage2");<br />
item.SubItems.Add("Garbage3");<br />
listView.Items.Add(item);<br />
}<br />
}<br />
}<br />
IMP:- This is happening only when I am clicking column other than the first one for selection...
modified on Saturday, July 9, 2011 5:39 AM
|
|
|
|
|
|
Thanx for the reply...
No Multiple Selection property is TRUE...
It hardly takes 5 minutes to make this sample...
Just Try it... I think this is the default behavior...
Am I missing any event that I need to handle???
Please help me... Getting stuck on such a puny issue :-|
Full points for anybody who helps... (Silly Bargain )
|
|
|
|
|
This is the correct behaviour. Selecting an item should not change it's checked state. If it did then the check box would be useless as it would only be a reflection of the selected state which is already indicated by the focus rectangle.
To get the behaviour you desire, handle the ListView 's Click event:
private void listView_MouseClick(object sender, MouseEventArgs e)
{
foreach (ListViewItem item in listView.Items)
item.Checked = item.Selected;
}
By the way, the inline code widget is for code written inline like this . The code block widget is for
blocks of code
like this
|
|
|
|
|
Thanks for replying. I know that it is the default behavior of list view with Check box but I need to change that.
The solution you gave fails if I select some unchecked Item...
|
|
|
|
|
I guess Multiple Selection Property and Check Box property are the for same purpose. To eliminate multiple selection by CTRL Key, we can also use CheckBoxes property. Using both the properties simultaneously conflicts behaviorally. It was my clients requirement to ave Enable /Disable feature in the list itself. Indeed, it wasn't the best way to design by using checked property of ListView for any other purpose. After a long discussion with my team members, we have come to a conclusion that it is better to eliminate Multiple Selection feature from the product. :-|
Although, I have a possible solution- Store the state of all the items just before every click and re-apply it after the click.
But this might create trouble only if no. of items exceed a few thousand. Anyways, I am still open for better (optimized) solution.
Thanks everybody...
|
|
|
|