Click to See Complete Forum and Search --> : using Trim on a filename.bms


bigjoe11a
August 2nd, 2009, 01:36 PM
What I'm doing is I have a ListView call m_Playable. When I click
on that item in that list. I want it to display the related files for that name in another Listview called m_ListRealted.

What the problem is I need to trim off the last 3 chars in the file name

here's the code I have

Any ideas?


private void m_Playable_MouseClick(object sender, MouseEventArgs e)
{
if (m_Playable.Items.Count == 1)
{
string filename = Convert.ToString(m_Playable.CheckedItems);
label3.Show();
label3.Text = "Selected File Name " + filename + ".bms";
//string [] charsToTrim = {'bms'};

//filename.TrimEnd(charsToTrim);

DirectoryInfo dr = new DirectoryInfo(@MLHSetting.Default.PlayableMaps);
List<string> relatedfiles = new List<string>();

foreach (FileInfo related in dr.GetFiles(filename + ".*"))
{
m_ListRealted.Items.Add(related.Name);
}



}

Mutant_Fruit
August 2nd, 2009, 03:07 PM
Trim removes whitespace from the start/end of a page. That's all.

If you want to get a substring, use the SubString comand!

string mystring = "file.ext";
string filename = mystring.SubString (0, myString.LastIndexOf ('.'));

Shuja Ali
August 2nd, 2009, 03:11 PM
How about using Path class's GetFileNameWithoutExtension (http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx) method? It should be really easy. :)

Sometimes documentation helps too :D

On a side note, you should also take a look at the Path class (http://msdn.microsoft.com/en-us/library/system.io.path.aspx) and its members, it might be helpful in the future.

bigjoe11a
August 2nd, 2009, 03:39 PM
Cool. thanks Guys, Can I get some more help.

In a listview. When I click on an item in the listview. How can I return what I selected

Any ways that what I'm trying to do.
From above heres the rest of my code.


private void m_Playable_Click(object sender, EventArgs e)
{

string filename = GetFileNameWithoutExtension(m_Playable.FullRowSelect);
//string filename = Convert.ToString(m_Playable.FullRowSelect);

label3.Show();
label3.Text = "Selected File Name : " + filename;

m_ListRealted.Items.Clear();

string path = textBox2.Text;
DirectoryInfo dr = new DirectoryInfo(@path);

List<string> relatedfiles = new List<string>();

foreach (FileInfo related in dr.GetFiles(filename + ".*"))
{

relatedfiles.Add(related.Name.ToString());

}

}


What heppens is when some one clicks on one item in the m_Playable list.
How would I return the name of what's in that list.

vcdebugger
August 3rd, 2009, 07:10 AM
Since the return type of this function is 'void' you cannot return what is selected in the listview.
write one diff function and call it inside this function

monalin
August 3rd, 2009, 12:39 PM
Since the return type of this function is 'void' you cannot return what is selected in the listview.
write one diff function and call it inside this function

Huh? Why would he need to return anything?

bigjoe11a
August 3rd, 2009, 01:31 PM
Read the posts and find out

monalin
August 3rd, 2009, 02:37 PM
Read the posts and find out

Read it, still confused. You're trying to return the item that you selected? Return it to where? That function gets fired from an event.

Are you just trying to get the item that is selected whenever the button is clicked? Your post isn't clear on exactly what you're trying to do.

bigjoe11a
August 3rd, 2009, 03:10 PM
string fname = Path.GetFileNameWithoutExtension(m_Playable.SelectedItems[0].Text);

label1.Text = "Current File " + fname + ".bms";


That's what I'm trying to do

Arjay
August 3rd, 2009, 11:53 PM
Your code will crash if there aren't any selected items.

bigjoe11a
August 4th, 2009, 12:15 AM
if there's nothing in the list. Then there's nothing to select. I do plan on adding more error handling.