blah blah blah is here! blah blah » Close

up0down
link

I need to compare combobox items (text) with the filename.
I tried doing something like this:

for(int x =0; x>comboBox1.Items.Count; x++)
{
if (comboBox1.Items[x].ToString() == filename)
// something
}


But this is not working.

Can I do something like this:
If comboBox1 (all items) = (logpath + *.log) ???

string logpath is the path for the log file.

foamy
2499

Is the entire path entered into the combobox? If so, the method you have should work. How do the paths look when you step through the code?

gsvirdi
412

No comboBox is just having string (names) like G S Virdi, I'm trying to compare if the filename on the disk is equal to any Item of the combobox????

last answered one year ago

1 answers

up0down
link

The problem is that you're using 'greater than' when you should be using 'less than':

for (int x = 0; x < comboBox1.Items.Count; x++) // less than Count needed here
{
if (comboBox1.Items[x].ToString() == filename)
{
// something
break;
}
}

You can also use:
if  (comboBox1.Items.Contains(filename))
{
// something
}

However, because of the complexities of object equality and the way strings are handled internally by .NET, I can't guarantee that this will always work properly.

I didn't really follow the second part of your question but, if you're asking whether 'filename' can contain wildcards in the above code, then the answer is 'no'. You'd probably need to use regular expressions instead.

gsvirdi
412

I'm trying to fetch the filename like this: string filename = Form1.Paths.logpath + comboBox1.Text + ".tmp"; But this is not working. Why so??

vulpes
17279

Three possibilities occur to me : (1) Form1.Paths.logpath doesn't contain a path with a closing backslash, (2) there isn't actually a file showing in the textbox part of the combobox or (3) there is a file showing in the combobox but it already includes an extension.

gsvirdi
412

I've chekd (1)the logpath's value and it is "V:\\", (2) Not able to understand "file showing in the textbox part of the combobox". combobox is list of names only (GS Virdi).(3) no extension in the list of names in combobox. I've just tried to create a temp file with the name of the logged in user, and I'm trying to fetch the name of the user so that it can be displayed in the popup message to another user if they try to login.

gsvirdi
412

Solution found Thx a lot for ur help vulpes. :)

Feedback