Cita de: laut3n en 17 Enero 2015, 07:52 AMAlguno que otro va a saltar diciendo que lo haga a mano por codigo pero mi idea es que sea dinamico y que se almacene en el listbox.
No me ha quedado muy claro lo que quieres hacer, ¿ambos datos, preguntas y respuestas, quieres mostrarlos en un ListBox?, ¿o solo quieres mostrar las preguntas y ligar las respuestas a cada pregunta?.
En el primer caso, un ListBox no está diseñado para almacenar/mostrar parejas de items, así que si pretendes mostrar ambos datos eso significa que le estás dándole el enfoque erroneo a tu aplicación, en su lugar puedes crear tu propio control, o utilizar un ListView por ejemplo, con 2 columnas, una para mostrar preguntas y en la otra respuestas.
En el segundo caso, una solución sería almacenar las preguntas y respuestas en una colección se strings, o por ejemplo un KeyValuePair o una Tupla (entre otras), o tambiénbien puedes crear tu propio Type, ejemplo:
VB.Net:
Código (vbnet) [Seleccionar]
''' <summary>
''' Implements a kind of Question/Answer KeyValuePair.
''' This class cannot be inherited.
''' </summary>
Public NotInheritable Class QA
''' <summary>
''' Gets the question.
''' </summary>
''' <value>The question.</value>
ReadOnly Property Question As String
Get
Return Me.mQuestion
End Get
End Property
Private mQuestion As String
''' <summary>
''' Gets the answer.
''' </summary>
''' <value>The answer.</value>
ReadOnly Property Answer As String
Get
Return Me.mAnswer
End Get
End Property
Private mAnswer As String
''' <summary>
''' Initializes a new instance of the <see cref="QA"/> class.
''' </summary>
''' <param name="question">The question.</param>
''' <param name="answer">The answer.</param>
''' <exception cref="System.ArgumentNullException">
''' question;value cannot be empty.
''' or
''' answer;value cannot be empty.
''' </exception>
Public Sub New(ByVal question As String, ByVal answer As String)
If String.IsNullOrEmpty(question) Then
Throw New ArgumentNullException("question", "value cannot be empty.")
ElseIf String.IsNullOrEmpty(answer) Then
Throw New ArgumentNullException("answer", "value cannot be empty.")
Else
Me.mQuestion = question
Me.mAnswer = answer
End If
End Sub
''' <summary>
''' Prevents a default instance of the <see cref="QA"/> class from being created.
''' </summary>
Private Sub New()
End Sub
End Class
+
Código (vbnet) [Seleccionar]
Public Class TestForm
''' <summary>
''' The Question/Answer list.
''' </summary>
Public ReadOnly QAlist As New List(Of QA) From {
New QA(question:="pregunta1", answer:="7"),
New QA(question:="pregunta2", answer:="9"),
New QA(question:="pregunta3", answer:="5")
}
''' <summary>
''' Handles the Load event of the TestForm form.
''' </summary>
Private Sub TestForm_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load
' Add the questions into Listbox.
Me.ListBox1.Items.AddRange((From qa As QA In QAlist Select qa.Question).ToArray)
End Sub
''' <summary>
''' Handles the SelectedIndexChanged event of the ListBox1 control.
''' </summary>
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles ListBox1.SelectedIndexChanged
Dim lb As ListBox = DirectCast(sender, ListBox)
If Not lb.SelectionMode = SelectionMode.One Then
Throw New NotImplementedException("SelectionMode is not implemented.")
End If
Dim qa As QA = QAlist.Find(Function(x As QA) x.Question.Equals(lb.SelectedItem.ToString))
Debug.WriteLine(String.Format("Q: {0}", qa.Question))
Debug.WriteLine(String.Format("A: {0}", qa.Answer))
Debug.WriteLine(String.Empty)
End Sub
End Class
CSharp:
Código (csharp) [Seleccionar]
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
/// <summary>
/// Implements a kind of Question/Answer KeyValuePair.
/// This class cannot be inherited.
/// </summary>
public sealed class QA
{
/// <summary>
/// Gets the question.
/// </summary>
/// <value>The question.</value>
public string Question {
get { return this.mQuestion; }
}
private string mQuestion;
/// <summary>
/// Gets the answer.
/// </summary>
/// <value>The answer.</value>
public string Answer {
get { return this.mAnswer; }
}
private string mAnswer;
/// <summary>
/// Initializes a new instance of the <see cref="QA"/> class.
/// </summary>
/// <param name="question">The question.</param>
/// <param name="answer">The answer.</param>
/// <exception cref="System.ArgumentNullException">
/// question;value cannot be empty.
/// or
/// answer;value cannot be empty.
/// </exception>
public QA(string question, string answer)
{
if (string.IsNullOrEmpty(question)) {
throw new ArgumentNullException("question", "value cannot be empty.");
} else if (string.IsNullOrEmpty(answer)) {
throw new ArgumentNullException("answer", "value cannot be empty.");
} else {
this.mQuestion = question;
this.mAnswer = answer;
}
}
/// <summary>
/// Prevents a default instance of the <see cref="QA"/> class from being created.
/// </summary>
private QA()
{
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
+
Código (csharp) [Seleccionar]
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class TestForm
{
/// <summary>
/// The Question/Answer list.
/// </summary>
public readonly List<QA> QAlist = new List<QA> {
new QA(question: "pregunta1", answer: "7"),
new QA(question: "pregunta2", answer: "9"),
new QA(question: "pregunta3", answer: "5")
};
/// <summary>
/// Handles the Load event of the TestForm form.
/// </summary>
private void TestForm_Load(object sender, EventArgs e)
{
// Add the questions into Listbox.
this.ListBox1.Items.AddRange((from qa in QAlistqa.Question).ToArray);
}
/// <summary>
/// Handles the SelectedIndexChanged event of the ListBox1 control.
/// </summary>
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender;
if (!(lb.SelectionMode == SelectionMode.One)) {
throw new NotImplementedException("SelectionMode is not implemented.");
}
QA qa = QAlist.Find((QA x) => x.Question.Equals(lb.SelectedItem.ToString));
Debug.WriteLine(string.Format("Q: {0}", qa.Question));
Debug.WriteLine(string.Format("A: {0}", qa.Answer));
Debug.WriteLine(string.Empty);
}
public TestForm()
{
Load += TestForm_Load;
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
Debug Output:
Código [Seleccionar]
Q: pregunta1
A: 7
Q: pregunta2
A: 9
Q: pregunta3
A: 5