简体   繁体   中英

How do I string Split text in a text file into a text box and change the text of the radio button?

I have a text file which contains a quiz of question and answer.

m1 | mcq | Which of the following is an animal | table#cat#keyboard#water

m2 | mcq | which of the following is a programming language |C++#water#Air#Phone

m3 | mcq | which is the prime number | 3#4#8#10 

I have try to split a text or string in a text file into a textbox, For example, Which of the following is an animal

This is my code:

try 
{
    ReadQuestionTextBox.Text = File.ReadAllText("C:\\Users\\ip700\\Documents\\Visual Studio 2015\\Projects\\FYP Project\\FYP Project\\bin\\Debug\\RichTextBox.txt");
}
catch
{
    ReadQuestionTextBox.Text = "Could not read file";
}

I am trying to go further by using more complicated part of string split where I am able to determine the location of the text in text file where text like

Which of the following is an animal? into a text box and my answer choice split into 4 radio button, table#cat#keyboard#water .

I got confused on how to proceed as I'm still new to C# WPF

About string split, I know how to split a line of text in the text file into a listbox.

You'll want to read the contents of the file into a variable, not the text box itself.

string[] lines = File.ReadAllLines("filename");

If each line of your file is a new question then each element in the string array will hold the question to parse.

Then to parse each question you could loop over the array and use string split again. Here's how you might setup one question with its options.

string[] line = lines[0].Split("|", split options);
string question = line[2].Trim();
string[] options = line[3].Split("#", split options);

string split options can be StringSplitOptions.RemoveEmptyEntries which removes empty entries from the array or StringSplitOptions.None which preserves them.

Then you could loop through options and create and add a new radio button to the form for each option.

It doesn't look like your text file contains the answers to the questions so you may want to add that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM