Question

C# coding assignment: I want to fill the contents of my ComboBox with the contents of...

C# coding assignment:

I want to fill the contents of my ComboBox with the contents of a text file, but it wont load.

This is my code:
(I have tried it under the form load event and even created my own event. Not sure what I'm doing wrong!)

try
{
StreamReader inputFile;
inputFile = File.OpenText("Occasions.txt");
while (inputFile.EndOfStream)
{
occasionsComboBox.Items.Add(inputFile.ReadLine());
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.Close();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am guessing that it is because the while loop is never executed. Please change the loop to use ! like this

while (!inputFile.EndOfStream)

{

occasionsComboBox.Items.Add(inputFile.ReadLine());

}

You should check if it NOT end of file, then read and add an item. So use ! as I have shown above. That should solve your problem

Add a comment
Know the answer?
Add Answer to:
C# coding assignment: I want to fill the contents of my ComboBox with the contents of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What am I doing wrong on this? Chapter 17, Problem 3E // Michael Cole // 6/28/2017...

    What am I doing wrong on this? Chapter 17, Problem 3E // Michael Cole // 6/28/2017 // Name spaces for application using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace Week8 {     public partial class Form1: Form     {         // Get the location where the data file would be created         string fileLoc = Application.StartupPath +             "\\Datafile.txt";         /* Constructor of the class to create the GUI of the form */         public Form1()        ...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...

    /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR NAME HERE * Class: PRG/420 * Creation Date: TYPE TODAY'S DATE HERE ************************************************************************************* * Program Summary: * This program converts a given date to a string. * The code includes exception handling for a ParseException. ************************************************************************************/ package prg420week5_codingassignment; import java.util.*; // wildcard to import all the util. classes import java.text.*; // wildcard to import all the text classes public class PRG420Week5_CodingAssignment { public static...

  • I need help with my C coding homework. If possible can you please comment to help...

    I need help with my C coding homework. If possible can you please comment to help me understand whats going on in the code. Thanks in advance. Create a C program (3 points): o At the top of the file include the following comments: Your name The purpose (core concept found below) of the program The date created o Create two int variables: Initialize each to different values. o Create three pointers: Point the first two pointers to the first...

  • This is just part of a C# windows form Application. When the user enters their information, checks the check box to save info and hits the payment button the information is saved to a .txt file. When...

    This is just part of a C# windows form Application. When the user enters their information, checks the check box to save info and hits the payment button the information is saved to a .txt file. When user enters last name and clicks autofill if name is found in file it will auto fill the boxes but i'm having issues with this part. For some reason it skips the firstname box and enters the last name in the first name...

  • I need help with my computer science 2 lab. First I am asked to run the...

    I need help with my computer science 2 lab. First I am asked to run the following program and check output? public static void main(String []args) { String filename= "output.txt"; File file = new File(fileName);    try{ FileWriter fileWriter = new FileWriter(file, true); fileWriter.write("CS2: we finished the lecuter\n"); fileWriter.close(); }catch (Exception e){ system.out.println("your message"+e); } String inputfileName = "output.text"; File fileToRead = new File (inputfileName); try { Scanner = new Scanner (fileToread); while(Scanner.hasNextLine()){ String line = scanner.nextLine(); system.out.println(line); } }catch(Eception...

  • Hi everyone, For my Assignment I have to use C# in order to create a validation...

    Hi everyone, For my Assignment I have to use C# in order to create a validation form and include the following things to register and validate: Email Address, Username, Password, Retype Password, and a Submit Button. I have figured out some of the code for the Email Address validation: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Validation4 {     public partial class Form1 : Form     {        ...

  • I have the code DebugDataOne1.txt Somewhere over the rainbow. DebugDataOne2.txt There's no place like home. Are...

    I have the code DebugDataOne1.txt Somewhere over the rainbow. DebugDataOne2.txt There's no place like home. Are you a good witch or a bad witch? DebugThirteen1.java import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOException; public class DebugThirteen1 { public static void main(String[] args) { try { Path file1 = Paths.get("C:\\Java\\Chapter.13\\\\DebugData1.txt"); Path file2 = Paths.get("C:\\Java\\Chapter.13\\\\DebugData2.txt");// Please use the path of the file from where you want to access it. BasicFileAttributes attr1 = Files.readAttributes(file1, BasicFileAttributes.class); System.out.println("File: " + file1.getFileName()); System.out.println("Creation time " + attr1.creationTime()); System.out.println("Last...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • How do I correct this error in my code? When it got to the file type...

    How do I correct this error in my code? When it got to the file type I can only go "null" from a certain point and I'm wondering how to correct this error. Also can I get a word document on this coding strategy for this problem? How much of this am I doing correctly? The original problem description: Create a program that provides a listing of all the files in a directory. The program should be a Java application...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT