|
You can use next template in your structure to recover your data when user code throws exception:
public void SomeOperation()
{
try
{
SomeOperationBase();
}
catch(Exception)
{
throw;
}
}
protected virtual void SomeOperationBase()
{
}
Notice!
1. SomeOperation() is public and nonvirtual.
2. SomeOperationBase() is protected and virtual.
You can also use this template to solve this problem:
The unfortunate side effect is: it seems there are no simple way to have all user code run at the begining or at the end of the code.
-- modified at 4:51 Sunday 9th July, 2006
|
|
|
|
|
Thanks for answering!
Doesn't work though!
My Code is more like that:
void ActionImpl() { }
protected void Action(TextElement aParent)
{
foreach(TextElement child in this)
child.Action();
ActionImpl();
NotifyAction(this);
}
protected virtual void NotifyAction(TextElement aParent)
{
}
One problem is the recovering action has user notification too!
(in fact it just use the reverse operation).
So the recovering could fail to. (and then I have 2 exception which I should report to the user... which might be confusing).
So I decided I won't recover, I will just manage to have the data in a valid state, though neither the initial nor the expected final one.
An other case is:
protected void BigAction()
{
Action_1();
Action_2();
}
after Action_2() thrown an error I should undo Action_1(). But what if the undo throw an exception?
It's all clear to me now.
I should NOT recover.
That was my mistake.
I should report the exception straight away with as little change as possible.
However I should, at least, ensure valid (even though unexpected) internal data.
|
|
|
|
|
BTW my case is exactly the opposite. core work is NOT virtual.
Unfortunately I have virtual "notification" method (or sometimes event, but that lead to the same logical problem).
Which "might" throw an exception....
|
|
|
|
|
If there's a possibility that the user's code might corrupt the data (which is likely to be true if the data is not a read-only and user has sufficient rights to change it) - provide some way to the [b]user[/b] to handle it.
Don't try to recover data by your own means because obviously your code have no prior knowledge that might go wrong therefore there's no guaranty that you would be able to fix all the possible mess.
If there's no guaranty - don't claim it. Let the user's code do
all cleaning up/fixing job in a way he/she wants.
One thing that might be usefull is ability of making a copy of data so the user could try again if bad things happened with the original data. If it pays to make a copy of the data before potentially dangerous operation - let user do the decision.
The bottom line is - let the user decide is data was corrupted and if it was - let user's code recover from corruption.
Best regards,
-----------
Igor Sukhovhttp://sukhov.net
|
|
|
|
|
Exactly!
That was one of my mistake!
|
|
|
|
|
If i want to read a file from a ftp site, how can i do it.
|
|
|
|
|
|
Hello,
I have a treeview already populated with data. What I want to do now it to populate a listBox control with extactly the same data.
Let's say my treeView control contains the following items:
Parent
+ Son
- - Grandson 1
- - Grandson 2
+ Daughter
- - Grandson X
- - and so on...
I want to make my listBox items as follow:
- Son
- > Grandson 1
- > Grandson 2
- Daughter
- > Grandson X
- > and so on...
I don't know how many level I'll have at design-time.
Please help. Thanks.
|
|
|
|
|
Use recursion. Something like this:
private void FillListBox()
{
foreach (TreeNode rootNode in treeView.Nodes)
{
AddSubtree(rootNode, string.Empty);
}
}
<p/>
private void AddSubtree(TreeNode node, string prefix)
{
listBox.Items.Add(prefix + node.Text);
prefix += '>';
foreach (TreeNode childNode in node.Nodes)
{
AddSubtree(childNode, prefix);
}
}
-- modified at 2:24 Sunday 9th July, 2006
|
|
|
|
|
Worked like a charm! Many thanks.
Can I ask you another question?
I have a self joined table named: Category. It contains 3 columns only: CAT_ID, PARENT_CAT_ID and Title. I'm using a data reader to get the categories from the database as follow:
while (dbReader.Read())
{
Title = (string)dbReader["Title"];
CAT_ID = (int)dbReader["CAT_ID"];
PARENT_CAT_ID = (int)dbReader["PARENT_CAT_ID"];
// MessageBox.Show(Title); <- Working!!!
}
Can you please tell me how can I populate a treeview recursivley with the data I pulled from the database?
I would appreciate any help.
-- modified at 3:08 Sunday 9th July, 2006
|
|
|
|
|
1. How do you define root records? CAT_ID = PARENT_CAT_ID or CAT_ID is null or CAT_ID = 0 or something else?
2. Do child records always follow parent record in your database query result or they can be mixed?
|
|
|
|
|
1. Root PARENT_CAT_ID = 0
2. They can be mixed
Thanks.
|
|
|
|
|
Fill TreeView.Nodes without hierarchy at first:
while (dbReader.Read())
{
TreeNode newNode = new TreeNode((string)dbReader["Title"]);
newNode.Tag = (string)dbReader["PARENT_CAT_ID"];
newNode.Name = (string)dbReader["CAT_ID"];
treeView.Nodes.Add(newNode);
}
And then build hierarchy:
for (int j = treeView.Nodes.Count - 1; j >= 0; j--)
{
TreeNode node = treeView.Nodes[j];
string parentId = (string)node.Tag;
if (parentId != "0")
{
TreeNode[] parent = treeView1.Nodes.Find(parentId, true);
if (parent.Length == 1)
{
treeView.Nodes.Remove(node);
parent[0].Nodes.Add(node);
}
}
}
Just don't forget to call treeView.BeginUpdate() before filling and treeView.EndUpdate() after it.
|
|
|
|
|
Thanks a lot. It worked
|
|
|
|
|
I am trying to detect KeyDown to detect "CTL+A" with following code:
Form1.KeyDown += new KeyEventHandler(OnKeyPress);
public void OnKeyPress(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.A && e.Control)
MessageBox.Show("CTL+A was pressed");
else
{
MessageBox.Show("Wrong Key pressed");
}
}
Still does not work. it does not like this line
Form1.KeyDown += new KeyEventHandler(OnKeyPress);
ANy help is greatly appreciated.
|
|
|
|
|
|
I made the changes
*************
public void OnKeyPress(object sender, KeyEventArgs e)
{
if(e.KeyCode == (Keys.A | Keys.Control))
MessageBox.Show("CTL+A was pressed");
else
{
MessageBox.Show("Wrong Key pressed");
}
}
*****************************
but still does not work, I think is because I am not trapping the KeyDown handler correctly
|
|
|
|
|
Does some other control have the focus when you press it?
It seems to me the best way to go about doing this is to override the ProcessCmdKey method and listen from there. That way you'll catch the key press no matter which control has focus.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Goof around music jam with my brothers (with video)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
When I run the application, one of the textBox has the focus. Is there a way to give the current Form the focus? or do you have a sample code to override using ProcessCmdKey?
Thanx in advanced
|
|
|
|
|
picasso2 wrote: When I run the application, one of the textBox has the focus.
That explains why the form's key press handler isn't catching the key; the key was pressed for the text box, not the form.
picasso2 wrote: do you have a sample code to override using ProcessCmdKey?
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.A))
{
MessageBox.Show("You pressed CTRL+A!");
}
return base.ProcessCmdKey(ref msg, keyData);
}
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Goof around music jam with my brothers (with video)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
You can use Form.KeyPreview and Form.KeyDown:
MyForm.KeyPreview = true;
MyForm.KeyDown += new KeyEventHandler(MyForm_KeyDown);
.
.
.
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.A) && e.Control && !e.Alt)
{
Debug.WriteLine("Ctrl + A");
}
}
-- modified at 2:44 Sunday 9th July, 2006
|
|
|
|
|
Keyboard handling is one of the most confusing thing in the Control class.
To know it all in great detail read that:
.. uh.. can't find the article again.
to make it simple override ProcessDialogKey
|
|
|
|
|
Hey all. I am currently using a low level mousehook to retrieve the mouse position. I would like to know if there is a function in .NET 1.1 that will allow me to retrieve the mouse coordinates when a Form is created or the program starts. I don't think I can use mouse events unless I can programatically call an event (such as mouse move). I am new to C# so any help would be greatly appreciated.
Thanks!
Josh
|
|
|
|
|
Josh, you can use Cursor.Position to determine the position of the mouse at any time. You don't need to handle an event in order to query this property.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
ahh right, the static instance of Cursor. I also found that Control.MousePosition works as well. Thanks for the help.
|
|
|
|