如何将下四行文本加载到文本框中 [英] How to load next 4 lines of text to a textbox

查看:102
本文介绍了如何将下四行文本加载到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个多选测验的应用。我有一个问题。我有一个按钮单击事件,可以将4行文本加载到4个单独的文本框中,如何再次单击相同的按钮,将文本文件中的下4行文本添加到文本框中?这是我到目前为止的代码:





Hi, I'm trying to make an app that is a multiple choice quiz. I have one question. I have a button click event that loads 4 lines of text to 4 separate textboxes, how can I click the same button again to add the next 4 lines of text in a text file to the textboxes? This is my code so far:


Public Class Form1
    Dim lines() As String
    Dim index As Integer = 0
    Dim answers() As String







Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lines = IO.File.ReadAllLines("C:\Quiz\GenesisQuiz.txt")
        answers = IO.File.ReadAllLines("C:\Quiz\GenesisAnswers.txt")










Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
        TextBox1.Text &= lines(index) & Environment.NewLine
        index += 1
        If index > lines.GetUpperBound(0) Then Button40.Enabled = False
    End Sub





当我点击下一个按钮时,上面的代码一次将我的问题加载到一个文本框中。







The above code loads my questions to a textbox 1 at a time as I hit the next button.


Private Sub Button40_MouseClick(sender As Object, e As MouseEventArgs) Handles Button40.MouseClick
        If File.Exists("C:\Quiz\GenesisAnswers.txt") Then
            Dim ioFile As New StreamReader("C:\Quiz\GenesisAnswers.txt")
            TextBox2.Text = ioFile.ReadLine()
            TextBox3.Text = ioFile.ReadLine()
            TextBox4.Text = ioFile.ReadLine()
            TextBox5.Text = ioFile.ReadLine()
        End If
    End Sub





此代码将4个单独的文本框中的4个答案加载到文本框右侧的单选按钮,以选择要给出的答案。当我加载问题2的新问题时,我需要帮助将我的文本文件中的后4个答案加载到4个单独的文本框中。感谢您的建设性批评!



This code loads my 4 answers to 4 separate textboxes with radio buttons at the right of the textbox to choose which answer to give. I need help on loading the next 4 answers in my text file to the 4 separate textboxes as the new question, question 2, is loaded. Thanks for your constructive criticism!

推荐答案

如果这是一个Windows窗体应用程序,

在窗体范围内声明文件变量,而不是本地。在表单加载中打开文件。



如果这是一个Web应用程序,则需要在隐藏字段中保留最后一行 - 否。在下一次单击之前跳过该标记,然后再加载文本框中的下一行。
If this a windows form application,
declare the file variable in form scope, instead of local. Open the file in form load.

If this is a web application, you need to keep the last line-no in a hidden field. On next click skip upto that mark before loading the next lines in your textboxes.


您好,



请使用以下代码。



Hi,

Please Use below code.

Private Function ReadAnswers(ByVal Offset As Integer) As ArrayList

       Dim arrAnswers As New ArrayList

       'Read all lines from the text file and fill the lines array
       Dim strLines() As String = File.ReadAllLines(Application.StartupPath & "\1.txt")


       Dim StartIndex As Integer = ((Offset - 1) * 4)
       Dim EndIndex As Integer = StartIndex + 3

       For i = StartIndex To EndIndex
           arrAnswers.Add(strLines(i).ToString)
       Next
       Return arrAnswers
   End Function





在上述功能中,您可以通过问题编号。作为抵消的参数。

如果您有任何疑问,请告诉我。



谢谢。!!!

Hiren Lad。



In above function you can passed the Question No. as a parameter of offset.
Please let me know if you have any query.

Thanks.!!!
Hiren Lad.


这篇关于如何将下四行文本加载到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆