Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What is the problem to my code? It always results in "No record found!" even if what am I searching is correct.
private void button3_Click(object sender, EventArgs e)
{
string[] lines = System.IO.File.ReadAllLines(#"C:\Users\kulet\Desktop\file.txt");
System.Console.WriteLine("Contents of file.txt = ");
foreach (string line in lines)
{
if (textBox14.Text == line)
{
label28.Text = "File exists!";
}
else
{
label28.Text = "No record found!";
}
Console.WriteLine("\t" + line);
}
You should place a break; after you found a match, since now it will always show the match of the last line:
label28.Text = "File exists!";
break;
The break will bail out of the foreach.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need equivalent for Application.OpenForms[0].InvokeRequired in WinForms for wpf.I tried with
var dispatcher = myDispatcherObject.Dispatcher;
if (dispatcher.CheckAccess()) { /* ... */ }
but no luck
Try the following extension method:
public static void TryToExecuteOnUI(this Action uiAction)
{
var uiDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
if (uiDispatcher.CheckAccess() == false)
{
uiDispatcher.Invoke(uiAction);
return;
}
uiAction();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Upon tick of my timer i am reading 9 byte of data from my sensor, i am converting the read string to HEX form the data is 01-03-04-0A-D5-15-A9-26-FD
right now the only data concerned with me is 4th & 5th element i am using split command to split my string but still i am unable to access exception pops out Indexoutofrangeexception, when itry to access 4th element by index 3,
private void timer1_Tick(object sender, EventArgs e)
{
// timer1.Enabled = false;
serialPort1.Write(query, 0, query.Length);
incoming = serialPort1.ReadExisting();
ba = Encoding.Default.GetBytes(incoming);
var hexString = BitConverter.ToString(ba);
textBox1.Text = incoming;
textBox2.Text = hexString;
string[] splittedResult = hexString.Split('-');
textBox3.Text = splittedResult[3];
//label1.Text = splittedResult[1];
// textBox3.Text = Convert.ToString(hexString.Length);
timer1.Enabled = true;
}
Maybe hexString is null or it's value is shorter than you think. Try this:
...
string[] splittedResult = hexString.Split('-');
if (splittedResult.Length >=4 ) // check lenght
textBox3.Text = splittedResult[3];
...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I list the files in the folder with checkedlistbox?
I am a student and need help with the project.
Put a Button and a CheckedListBox on a Form. See button handler example:
private void btnListFiles_Click(object sender, EventArgs e)
{
try
{
var fileNames = Directory.GetFiles(Directory.GetCurrentDirectory());
foreach (var fileName in fileNames)
{
// cbListBox.Items.Add(fileName); // Full path
cbListBox.Items.Add(fileName.Split('\\').Last()); // Just filename
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Note that Directory.GetFiles will lock your app for some time if there are too many files inside. In this case consider to iterate them using Directory.EnumerateFiles method.
//Get file list with full path
string[] filesFullpath = Directory.GetFiles(#"Directory Path");
//get just file names
files = filesFullpath.Select(s => Path.GetFileName(s)).ToList();
//set checkedListBox datasource
checkedListBox1.DataSource = files;
Full Code
string[] filesFullpath = Directory.GetFiles(#"Directory Path");
files = filesFullpath.Select(s => Path.GetFileName(s)).ToList();
checkedListBox1.DataSource = files;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I was just wondering if someone could help me out, I am trying to compile this c# program and csc.exe keeps returning the error
Exercise_.cs<11,25>: error CS1002: ; expected
Though I cannot find a missing semicolon...any help would really be appreciated.
using System;
namespace _8546574 {
class exercise_8 {
static void Main(string[] args){
bool running = true;
While(running = true){
Console.WriteLine("Enter a number between 1 and 3");
Console.Clear();
int input = int.Parse(Console.ReadLine());
switch(input){
case 1:
Console.WriteLine("You entered 1");
running = false;
Console.ReadLine();
break;
case 2:
Console.WriteLine ("You entered 2 :)");
running = false;
Console.ReadLine();
break;
case 3:
Console.WriteLine("You entered 3");
running = false;
Console.ReadLine();
break;
default:
Console.WriteLine("failed");
Console.ReadLine();
break;
}
}
}
}
replace your line While(running = true){ with while(running){ that should work
while must be lowercase and the check can (running) or (running == true)
From your code I just noticed that you have While and it should be while.
while(running == true)
or
while(running)
From copying your code exactly as written, you need to make while lower case, and add an extra } to the end of the code. everything else compiles fine
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I get this error for the code shown below and don't know why.
my code is:
protected override void OnStart()
{
if (WCHSBMobileApplication.Current.SuperBillObject == null)
{
AddSuperBill();
}
else
{
EditSuperBill();
}
}
what can I do?
The exception message says it all:
You removed this line:
base.OnStart();
You need to add it back:
protected override void OnStart()
{
base.OnStart();
if (WCHSBMobileApplication.Current.SuperBillObject == null)
{
AddSuperBill();
}
else
{
EditSuperBill();
}
}