|
zecodela wrote:
i think it related to using diff. thread to access GUI resource
You think correctly - you must only access form controls from the originating thread. Here's the keyword thast will solve all your problems: Invoke
There's a couple of articles here on CP that deal with multi-threading issues in Windows Forms - both are useful, especially the one by James T Johnson, look them up.
|
|
|
|
|
Dear members,
I'm making a listview that allows user to drag (row)items to different index recently. However, I can't find a good example for doing that.
Does anyone knows how to use drag-drop event on ListView? Or anyone knows where has good example?
In my ListView, suppose each item has 6 subitems, when I drag an item (e.g.) index 5 to index 1, then all data of item 5 are inserted into row 1.
I know this is not hard, and many of our members knows that. Please teach me
Please help me..... Thanks a lot~
I've tried to make the code, but it looks a bit strange.
e.g. I have items 1,2,3,....10.
if I selected 2,4,5,6 and drag to the position of item 3. Suppose, item 2,4,5,6 will move under item3 .. like 1,3,2,4,5,6...
but how can I prevent some selected items not to do anything if I move to there position? e.g. 4,5,6,7 are selected, if I move to 5 or 6, the program won't do anything.
Could you help me to see how to implement this code? Or do you know what is the best way to do this?
Here is the codes inside my listview drag drop function:
<br />
if(OList.SelectedItems.Count==0)<br />
return;<br />
Point cp = OList.PointToClient(new Point(e.X, e.Y));<br />
ListViewItem ToItem = OList.GetItemAt(cp.X, cp.Y); <br />
<br />
if(ToItem==null)<br />
return;<br />
<br />
int num = ODS_List.SelectedItems.Count;<br />
int locationIndex = ToItem.Index-1;<br />
for(int i=0; i { <br />
ListViewItem dragItem = OList.SelectedItems[i];<br />
if(locationIndex!=dragItem.Index) <br />
return;<br />
<br />
ListViewItem insertItem = (ListViewItem)dragItem.Clone();<br />
OList.Items.Insert(locationIndex, insertItem);<br />
locationIndex++;<br />
}<br />
<br />
foreach(ListViewItem olditem in OList.SelectedItems)<br />
{<br />
OList.Items.Remove(olditem); <br />
}<br />
Thank you very very much.
|
|
|
|
|
Hi,
Do a search for something like "ListView" and "Drag Drop" from this forum, there is a chance that you will find some examples. Here is one:
http://www.codeproject.com/cs/miscctrl/dragdroplistviewreorder1.asp?target=listview%7Cdrag%7Cdrop
Hope you get more tips
Khang
|
|
|
|
|
I have an ArrayList bound to a datagrid. I've developed a method of validating the data entered in the TextBoxes, and forcing the user to re-enter invalid data. Unfortunately, I use the CurrentCellChanged event to test if it's necessary to move the selection back to the cell with the invalid data in it. It works fine with one exception. If you're at the end of a row of data, and press the TAB key (return is fine), you get an extra CurrentCellChanged event. The first one takes you to the beginning of the same row, and the next one takes you to the next row! This messes with my logic that determines if it's necessary to move the selection back to the cell with the error in it.
Is there some way to inhibit the handling of the TAB key once you've determined that you want to select another cell?
|
|
|
|
|
Yeah but you gotta override the PreviewKeyMessage function. Also you need to creat a custom table style
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
i want to make an oledbCommand with a variable as in sql @variable..SELECT *Table From Table WHERE Column=(thatvariable),how can i declare this to use in my code, am using Microsoft Access
|
|
|
|
|
OleDbConnection oleConn = new OleDbConnection(<myconnectionstring>);
oleCOnn.Open();
OleDbDataAdpater oleAdapter = new OleDbDataAdapter("my select statemnent", oleConn);
DataSet ds = new DataSet("Access");
oleAdapter.Fill(ds, "MyTable");
now I would check the parameters because its been 2 weeks since I connected but it should work fine
ohh yeah to test
DataGrid1.DataSource = ds.Tables["MyTable"];
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
thx Ista for replying , but that wasnt my question i was just asking about how to declare an oledb parameter as in SELECT *Table From Table WHERE Column=(parameter),as declared in sql we use "@" b4 the parameter i was just asking wat i ll declare in ole instead of that "@".
|
|
|
|
|
doh
its just like the sql
myOleCommand.Parameters.Add("@Param1", OleDbType.SmallInt);
refer to it as
myOleCommand.Parameters["@Param1"].Value = 1;
i use a short way when assigning many
OleDbParameterCollection olePar = myOldCommand.Parameters;
olePar.Parameters.Add("@Param2", OleDbType.VarChar, 10);
olePar["@Param2"].Value = "Hey dude";
I think that answers it. Sorry for the confusion.
nick
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
thx Ista really for replying again
but i still ve the problem!
this is my debug error :
@PostionIndex error: managed EE does not understand expression's syntax
the code :
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Phones WHERE Contact = @PostionIndex");
cmd.Parameters.Add("@PostionIndex", OleDbType.SmallInt);
cmd.Parameters["@PostionIndex"].Value = int.Parse(this.editID.Text);
|
|
|
|
|
change it for a question mark
check this link out
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbCommandClassParametersTopic.asp
and for info and parameter arrays
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbparameterclasstopic.asp
its like OleDbParameter[] oleParam;
but I would just stack them on. like in your code, just replace the parameters with ? and make sure you add them in order.
nick
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
I've been dealing with this problem off and on for the past couple weeks, and I'm stumped.
I have a Panel that I've been using as a desktop for my app, and on this desktop are many custom controls I've also extended from Panel. I'm trying to draw arrows between some of the custom controls. That has worked fine until I had to deal with scrolling. I'd like for the arrows to be drawn correctly on this virtual desktop, so if a custom control is barely outside the viewport, the arrow should be pointing in exactly that custom control's direction. Alas, I have arrows pointing in the wrong direction.
One of the problems is that the Location property doesn't seem to take into account the virtual location, after scrolling. So, it seems that two different controls can both be at Location (100, 0), even if they might be 1000 pixels apart in the virtual space. Somehow Windows Forms knows to display only the controls that belong in that viewport, but I can't figure out how to extract the virtual coordinates... and using them with DrawLine() would be even more useful.
PointToScreen() and PointToClient() don't seem to help. AutoScrollPos seems to be the way to go, maybe, but I haven't been able to do anything useful with it.
Does anyone have any tips and tricks for working with GDI+ and custom controls on a scrollable control (Panel)?
Thanks,
Arun
|
|
|
|
|
While I can't discuss issues specific to the Panel control, I can give you some insight into problems I overcame when extending the ListView to provide control container functionality - perhaps it can help.
In my control, I needed to get the actual pixel location of each ListViewItem's SubItem in the ListView - taking into account the current scroll position. I'm guessing that your situation is similar.
The first task was to find each controls location, relative to it's container:
int x = Left;<br />
for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent)<br />
{<br />
if (thisParent.Parent != null && thisParent.Left > 0)<br />
x += thisParent.Left;<br />
}<br />
<br />
int y = Top;<br />
for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent)<br />
{<br />
if (thisParent.Parent != null && thisParent.Top > 0)<br />
y += thisParent.Top;<br />
}
Once you've done this, you should have a good reference to everything, and can paint your lines etc. The only other thing you'll need to is is get notified of scroll events so you can repaint everything properly. For the ListView control, I was forced to override the WndProc method to get my notifications:
private const int WM_HSCROLL = 0x114;<br />
private const int WM_VSCROLL = 0x115;<br />
<br />
protected override void WndProc(ref Message msg)<br />
{<br />
if ((msg.Msg == WM_VSCROLL) || (msg.Msg == WM_HSCROLL))<br />
{<br />
}<br />
base.WndProc(ref msg);<br />
}
Naturally this could all be irrelevant to the Panel control (something I've never bothered to base a new control off), But I hope it helps anyway.
|
|
|
|
|
Interesting and insightful response, thanks! I fear that my lack of Win32/C++ makes me forget aboud WndProc().
I think the way to go might be to create and set a VirtualLocation in my child controls which get updated in a LocationChanged event handler. I may have to use WndProc() to create some HScroll and VScroll events -- odd, I think, that they aren't part of the Windows Forms library.
I still think it's odd that the Location property doesn't reflect the virtual location of the autoscrolling parent control, just that of the location within the viewport. It seems like one can use the Location property to move a child control to a virtual location, though.
Thanks,
Arun
|
|
|
|
|
OdbcDataAdapter da = new OdbcDataAdapter(sql,con);
DataSet ds = new DataSet();
da.Fill(ds,"Contacts");
dataGrid1.SetDataBinding(ds,"Contacts");
i need to get the total rows returned from my sql query similar to how ExecuteNonQuery() works.
how can i get the total rows in a data grid, data set, or data adapter?
|
|
|
|
|
Use ds.Tables["MyTable"].Rows.Count property to get the total number of rows in MyTable DataTable.
Alexandre Kojevnikov
MCAD charter member
Leuven, Belgium
|
|
|
|
|
|
For some reason DataSource,DisplayMember, and ValueMember don't seem to be properties of the CheckedListBox control. I even created a new project, dragged an new CheckedListBox and looked in the properties and tried to type it into the code.
This is my first time using this particular control with C#. In VB it still pops up in the Intellisence.
What am I missing?
Robert L. Edwards
|
|
|
|
|
public bool BeginsAlpha(string strToCheck)
{
System.Text.RegularExpressions.Regex pattern = new Regex("^[a-zA-Z]*");
return pattern.IsMatch(strToCheck,0);
}
BeginsAlpha("1") returns true no matter if my string starts with a letter or a number. What am I doing wrong here?
|
|
|
|
|
* means "0 or more characters", so when you try to match against:
3djd
you successfully match zero alpha characters.
If you get rid of the *, you'll have to match 1 character, which I think is what you want.
|
|
|
|
|
thank you! with that in mind, i used a + instead of a * and it worked great!
|
|
|
|
|
Hi,
Im looking to create a client/server c# application. I've been reading many articles on the subject, but the developers always seem to be arguing about the best practices after the article.
I was just wondering if anyone has any tips? to use synchronous or asynchronous, etc. Polling or Events etc?
I need to support upto 10 clients.
I've created a client/server app with Java before where (for the server) I created a thread to listen for connections, when a connection was made it passed the socket to a clientservice class and ran it in a new thread, then saved a pointer to the class in a vector array. Is this the same approach I should take with c#?
Thanks in advance,
Matt.
|
|
|
|
|
The best way is to allow for both synchronous and asynchronous calls. One thing that CP articles typically miss is reusability and portability. It's always better to make your application scalable and extensible so that clients can use it however they want (those posing restrictions isn't always a bad thing) and you could even mix sync and async calls. Sometimes one is better than the other - it just depends upon the circumstance. That's the server.
The client is a slightly different matter. The server should be an extensible architecture that scales. The client must choose how to use the server. In the case of Web Services, async methods are generated automatically (if you want). If there is a process in which you want the user to wait and it can't / shouldn't be cancelled - use a sync call. If they can cancel it or you want to update the UI or something while waiting, use an async call.
Now, if you use remoting, you have some further options. Providing scalability is important, but your hardware is limited no matter how big you are. You could always support a certain number of async calls, but after so long the hardware gets sluggish. You could inform your client apps (using channels for example) to start using sync calls to cut-down on the server load.
Basically, everything you're reading is right and wrong. There is no "right" or "wrong" way for every circumstance, but there are "right" and "wrong" times to use different techniques.
I know this answer is a little abstract, but I hope is solves your dilema.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks very much for the detailed reply Heath, I've taken note of many of your points, Generally I think I need to put time aside to study the networking features of .net to better understand the best solution for my requirements.
Thanks Again,
Rgds, Matt.
|
|
|
|
|
To echo what Heath said, it depends on what your client server application is like. Which way does the information flow? Who initiaties the information transfer? What kind of latency requirements do you have?
To speak in general terms, if your clients ask for data and get back a response, you can choose your technology. Remoting gives you rich types, but only works with .NET programs, while web services give you reach but less richness in the type area. Neither are particularly good when the server needs to tell the client something asynchronously, though you can get away with polling or blocking calls until a timeout or something interesting happens.
Sockets give you the most flexibility, but you'll probably be writing a bit more low-level code. I wrote some C# code recently that does what you describe using sockets, and implements a nice event-based approach. The client calls a method on a object, which sends a message to the server. This gets rebuilt into an object on the server, and an event on the server object is fired. Same thing when the value comes back to the client. This gives you more flexibility, but moves you into a message-based approach rather than the RPC idiom that web services and remoting use.
Hope that helps.
|
|
|
|
|