Barbfrog

how do you set the focus to a item in list view...

This is what I found so far also found some stuff for the mouse but not needed

selected highlights the item but doesn't setfocus to it....

if I click the row with the mouse then it sets the focus to that row...

I need to be able to set the focus on the row using code.....

Thanks....

List_Of_Programs_To_Startup.Focus();

List_Of_Programs_To_Startup.Items[x-1].Selected = true;

MessageBox.Show (List_Of_Programs_To_Startup.FocusedItem.ToString());



Re: Windows Forms General list view set focus to item

TaylorMichaelL

In almost all cases the currently selected item in the list view is the one that will have focus when the list view is active. Therefore you can either set the Selected property of the ListViewItem desired or you can add the item to the SelectedItems collection of the list view manually.

Alternatively you can use the Focused and FocusedItem properties to accomplish the same thing. However focused items are not necessarily selected so you should also select them.

Michael Taylor - 6/6/07

http://p3net.mvps.org





Re: Windows Forms General list view set focus to item

Barbfrog

ok thanks

I'm starting to find out you have to do more in C# to manipulate stuff.

For now I'll call them add-ons or extension till I find out what they they are really called.

LOL had to use stringbuilder to manulate strings.... In basic it is done with less code...

Anyway got around what I thought I needed focus on the row by using listviewitem item

I have no idea how listviewitem item sees all the row just PFM for now...

For now I'll assume it is an add-on like stringbuilder is to strings..

ListViewItem reached in and grabbed the info I needed so that is good enough for me...........

List_Of_Programs_To_Startup.Focus();

ListViewItem Item = List_Of_Programs_To_Startup.Items[Programs_Left_To_Startup-1];

string File_Name_Arguments = "";

string File_Name = Item.SubItems[1].Text;

string Data = Item.SubItems[1].Text;

The problem with finding working code examples on the net is most of it is c_r_a_p that doesn't work ..
I never found a real working start up delay program to delay startup times of software during boot up so I did my own ..................... Visual Studio 2005 C# does most of the work... You just have to fill in the details....




Re: Windows Forms General list view set focus to item

TaylorMichaelL

Sort of an aside about StringBuilder...

You don't need to use StringBuilder to manipulate strings. VB and C# share the same core set of functionality. If you want to concatenate strings together then use +. You should only need to use StringBuilder when creating complex strings that include formatting. This provides for better memory management. VB has the same requirements. Here are some examples:

//Simple concat

string str = "Hello " + strUserName + "!";

//Formatting

string str = String.Format("Hello {0}! It is currently {1}.", strUserName, DateTime.Now);

//Complex (optimize memory usage0

StringBuilder bldr = new StringBuilder();

bldr.Append("SELECT ");

foreach(string column in Columns)

bldr.AppendFormat(" {0},", column);

bldr.Append("FROM ");

foreach(string table in Tables)

bldr.AppendFormat(" {0},", table);

bldr.Append("WHERE ");

foreach(string where in WhereClauses)

bldr.AppendFormat(" {0} AND", where);

string strQuery = bldr.ToString();

Since strings are immutable each time you modify one a new copy is made. Concatenation inhererently creates a new string for each +. Therefore if you were to do a bunch of concatenations then you'd create a bunch of strings which is a waste. StringBuilder creates a single buffer and appends data to it. When the buffer gets full it allocates a new one. Only when you call ToString does it generate the final string. Hence it uses memory efficiently.

String concatenation in C# and VB are optimized such that they will actually call the String.Concat method which internally optimizes the memory usage. Therefore when joining strings use a simple concatenation expression as it is efficient. When you need to build strings and you can't create a single concatenation expression then prefer StringBuilder. In the cases where you need to concatenate a non-string value to a string value then you can use String.Format but since it has a higher runtime cost prefer to use ToString whenever possible. The exception is for highly formatted strings where String.Format is easier to read and understand.

Michael Taylor - 6/6/07

http://p3net.mvps.org





Re: Windows Forms General list view set focus to item

Barbfrog

that is fine and dandy but when you get into chopping char out to work on...

you get errors that you can't convert from char to string or string to char...

string builder works wonders...........

it turns the string into an array that you can work with,,,,

string Data = Item.SubItems[1].Text;

StringBuilder sb = new StringBuilder(Data.Length);

char c = Data[0];

if (c == '"'){ // is first char a quote//

for (int i = 1; i < Data.Length; i++){

c = Data [ i ] ;

if (c == '"'){ // find second quote and split string

File_Name = Data.Substring(1,i-1);

File_Name_Arguments = Data.Substring(i+1,Data.Length-i-1);

break;

}

}

}





Re: Windows Forms General list view set focus to item

TaylorMichaelL

I'd do it this way:

Code Snippet

string Data = Item.SubItems[1].Text;

if (Data[0] == "\"")

{

int nPos = Data.IndexOf("\"", 1);

if (nPos > 0)
{

File_Name = Data.Substring(1, nPos - 1);

if (nPos < Data.Length)

File_Name_Arguments = Data.Substring(nPos + 1, Data.Length - nPos - 1);

};
};

Personally I find the index calculations to be difficult to remember so I use a helper class that exposes methods to get portions of a string to the left and right of a particular character.

Code Snippet

string Data = Item.SubItems[1].Text;

File_Name = StringHelper.LeftOf(Data, "\"", 1); //Assumes the first character is a quote

File_Name_Arguments = StringHelper.RightOf(Data, "\"", 1); //Skips the first quote and gets everything to the right of the other

Then again if all you really want to do is strip out the first string in quotes then Split works just as well although it is a little harder to follow:

Code Snippet

string[] tokens = Item.SubItems[1].Text.Split(new char[] { '\"', 2, StringSplitOptions.RemoveEmptyEntries);

switch(tokens.Length)

{

case 1 : File_Name = tokens[0]; break;

case 2 : File_Name = tokens[0]; File_Name_Arguments = tokens[1]; break;

};

Just a couple of helpful hints.

Michael Taylor - 6/26/07

http://p3net.mvps.org