|
Can anyone help me out with this and tell me what I'm doing wrong?
1. I have a form with a ListView control which will hold items for me...
2. I have declared a private variable of type ListViewItem ie.
private ListViewItem lvi = null;
3. In the ListView's MouseDown Event Handler, I check if the right mouse button is pressed, and to popup a context menu to delete the selected item.
private void ListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
try{
if (e.Button == MouseButtons.Right && this.ListView.Items.Count >= 1){
this.ListView.Focus();
this.lvi = this.ListView.GetItemAt(e.X, e.Y);
if (this.lvi != null) this.lvi.Selected = true;
this.cntxtMenu.Show(this.ListView, new System.Drawing.Point(e.X, e.Y);
} /* if (e.Button == .....) */
}/*end try */
catch{}
}
4. cntxtMenu contains a Caption 'Delete' and the menu-item is called cntxtMenuDelete.
5. In the cntxtMenuDelete Click Event I have the following
private void cntxtMenuDelete_Click(object sender, System.EventArgs e)
{
try{
this.ListView.Items.Remove(this.lvi); /* THIS LINE CRASHES! */
}/* end try */
catch{}
}
I have omitted the Exception code for brevity here, in the comment above where I've highlighted the line that causes a crash...the message I get from the Exception handler is, mind you, it doesn't even tell me what kind of exception is it - "Specified argument was out of the range of valid values. Parameter name '0' is not valid for 'displayIndex'." It even cut across the exception handler and therefore it did not catch the problem. I loaded this code into the CLR debugger and got it to trap all Exceptions and got a ArgumentOutOfRangeException - guess where, not in my code but in system.windows.dll....weird! the CLR debugger says it crashed when the Listview was making a call to get_item(int displayIndex) based on the stack dump from the debugger. ie a LVM_GETITEM message, something went funny there....I even derived the Listview and added a custom WndProc override to trap the LVM_GETITEM message and to ignore it....thinking that would solve it but no...Any ideas? Is this a bug?
Thanks in Advance,
Tom.
|
|
|
|
|
Hello Tom.
I hope write well!! because i'm from Argentina and a have 2 works to do, think the answer and translate it! .
I copy your code in a project that i use for test.
In the only case that happends, is when the listview remove the last lisviewitem, because for dafault, take focus the next listviewitem.
if after de "line crashes" write something like that
MessageBox.Show(" there are " + this.listView1.Items.Count.ToString());
the message take focus, and the error don't happends.
I dont know if this is a bug, I never have it because i ever confirm the delete.
I use a CommandButton near the listview (with a "-" icon)
and the code is
if (this.listView1.SelectedItems.Count >0)
{
if (MessageBox.Show (this, "¿Are you agree, for delete the item
selected?"," ",System.Windows.Forms.MessageBoxButtons.OKCancel)==
DialogResult.OK)
{
this.listView1.Items[this.listView1.FocusedItem.Index].Remove();
}
}
Bye
|
|
|
|
|
Many Thanks,
On Closer inspection after debugging, the call stack shows that with the code at hand, it somehow thinks it was initiating a drag and drop operation, Bizarre!! There's a few lines in the call trace that was calling WM_NOTIFY message, then calls LVN_BEGINDRAG, which in turn calls the ListView_getItem(displayIndex) and since the listitem was the only one, removing it, and boom, system.windows.forms.dll crash! I cured it by changing the event to a MouseUp Event which begs the following questions,
1) What is the real difference between MouseUp/MouseDown event handler apart from the fact that the button is released and the button is down respectively?
2) What situations would I use the MouseDown/MouseUp event handler and what would be a good reason to do so?
2) Why did it work with the MouseUp event handler? I used my hunch on this and changed the event handler, for some inexplicable reason, the listview control wasn't expecting a drag and drop and so it happily removed it!
Many thanks again!
Tom
|
|
|
|
|
1) What is the real difference between MouseUp/MouseDown event handler apart from the fact that the button is released and the button is down respectively?
Vanesa: if you click with the mouse and you do it on the wron place... using the MouseUp event you cant move the mouse (without relese the button) to another place and do nothing, while if you use the MouseDown, the event run when you click the mouse. This is the diference that I note.
2) What situations would I use the MouseDown/MouseUp event handler and what would be a good reason to do so?
Vanesa: I use MouseUp for example when I search address for a customer name, if datatable (returned for the sql query) contains more than 1 row, I send it to a especial form with a datagrid, and the user can select the customer to view his address,
private void dataGrid1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
// this is a control that "public" the selected id to the others forms
this.textBox1.Text = dataGrid1.CurrentRowIndex.ToString();
this.Close();
}
}
and the program return to the form that call the grid
Bye
Vanesa Addicted To DJ Tiësto's Trance
|
|
|
|
|
Many thanks for that insight! Certainly clarified everything! ![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
Remove this line
this.cntxtMenu.Show(this.ListView, new System.Drawing.Point(e.X, e.Y);
attache your contect menu like below in your in your InitializeComponent() method
this.ListView.ContextMenu = this.cntxtMenu;
Sandeep Naik
|
|
|
|
|
Great thanks for your solution, even I changed the event to MouseUp it worked! But nonetheless I'll certainly remember this! ;)
|
|
|
|
|
Hi all, I'm again,
In my project I want to draw some points with "drawEllipse" dependent on a switch. Now my question:
Is it better to save the points in an Array like "int[] Points1 = new int[5] { 12, 14, 17, 45, 70 }; and then draw the points in a loop or should I draw the points in the switch one after the other with the drawEllipse?
What's with the performance and what's with the resources?
Thanks in advance
BB
|
|
|
|
|
Hi all,
I want to build a switch for the ". How can I do this?
Thanks in advance
BB
|
|
|
|
|
You can use characters like double quotes by incorporating the escape character \. See also: .NET Framework General Reference - Character Escapes[^]
If you're switching a string try this:
switch (MyString)
{
case "\"":
MessageBox.Show("It's a \"!");
break;
}
Or rather this example, if you're switching a char :
switch (MyChar)
{
case '\"':
MessageBox.Show("It's a \"!");
break;
}
Best regards
Dennis
|
|
|
|
|
Thanks Dennis, I see I was on the right way but I allway forgot something.
|
|
|
|
|
Hello
could anyone help me to find a way to embed a numericupdown column in a dataGrid??
thanks..
bhshs
|
|
|
|
|
Hello..
Could anyone help me to find a way to embed a numericupdown column in a datagrid??
thanks..
bhshs
|
|
|
|
|
hi,
will you please see documentation for
DataViewGrid.
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
I have two forms and form1 call form2 where some data are in arraylist with some struct. How can send the struct with data from Form2 back to 1.
Best Reagrds
Danyboy
|
|
|
|
|
|
I had implement event to do this but i still have problems. there is any example.
form_1
...
bt_Click(object sender, System.EventArgs e)
{
form2 myform = new form2();
form2.Notify+=new eventHandler(myform2_Notify);
form2.show();
}
myform2_Notify(object sender,EventArgs e)
{
// AS far i undertand here i put the code to //handle the data thats comes form form2 by //"sender"
// I want to receive form form2 the struc Reg
}
.....
form2
public event Eve EventHandler Notify;
protected void OnNotify()
{
if(NotifyDaddy !=null)
//Reg is public struct
NotifyDaddy(Reg,EventArgs.Empty);
}
private void bt2_Click(object sender, System.EventArgs e)
{
this.OnNotifyDaddy();
this.Close();
}
DanyBoy
|
|
|
|
|
Here is a quick example:
public class Form1 : System.Windows.Forms.Form
{
public delegate void AddDataEventHandler(string s);
public event AddDataEventHandler DataChanged;
public void OnDataChanged(string s)
{
if(DataChanged != null)
DataChanged(s);
}
private void someButton_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.DataChanged += new AddDataEventHandler(f2.AddData);
f2.Show();
}
private void AddData_Click(object sender, EventArgs e)
{
string val = this.textBox1.Text;
if(val != null)
this.OnDataChanged(val);
}
}
public class Form2 : System.Windows.Forms.Form
{
private ListBox listBox1;
public void AddData(string s)
{
this.listBox1.Items.Add(s);
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
Thanks for your help
I try to adapt for wath I want but only works with a string if a define on delegate
some class src.tools ...
public delegate void test1Event(main1.sub1 Reg);
form1
public event src.tools.teste1Event DataChanged
will not work. Reg.val1 we always =0
if i try to read by Reg.Tostring(); I got "main1.sub1+Reg"
best Regards
DanyBoy
|
|
|
|
|
sorry its working Thanks.....
DanyBoy
|
|
|
|
|
Hi,
I'm trying to implement a journal playback proc in C#...the journal record seems to be working fine...however the HC_SKIP in playback proc seems to never get reached...apparently windows keeps calling the playback proc with code HC_GETNEXT only...so i can't advance to the next message until HC_SKIP is called...has anyone ever had this issue?
Thanks
|
|
|
|
|
Hi
How do you handle inserting " ' " character in program?
e.g
sqlcommand.CommandText = "INSERT INTO Table1 VALUES ('" + value1 + "', '" + value2 + "')";
if value1 is "'" (single quotation), it will cause error. To solve this, it is need to test value1 have got single quotation or not. If yes, insert one more single quotation before the original one. However, I think it is too trouble. Is there any other way to solve it? Thanks!
|
|
|
|
|
You can use the Replace() function.
Use it before executing the sqlcommand:
It is of the form:
Replace(TheString, WhatToReplace, ReplaceItWithWhat)
So apply it this way on all your variables before inserting them into the sqlcommand string:
Replace(value1, "'", "''")
Replace(value2, "'", "''")
Cheers

|
|
|
|
|
Hi!
I think you really should use parameterized queries. That way you don't have to worry about such things as how to place your quotes and are less prone to SQL injection attacks.
mav
|
|
|
|
|
Is there a way to make VS.net 2005 ignore the "Illegal Cross Thread Opperation" in debug mode?
/\ |_ E X E GG
|
|
|
|