jeudi 23 avril 2015

How can I pick a random string from an array and display it in a text box?

What I am trying to do is to pick a string from my array that I have set up at random and then display it int a text box named txtResults. I am not sure how to achieve this as I am fairly new to C# as well as creating GUIs. Any advice or help is greatly appreciated.

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 BrowerD_Final_Project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnMexican_Click(object sender, EventArgs e)
        {

            string[] mexicanRestaurants = 
            {
                "Jose Locos/n853 N Glenstone Ave, Springfield, MO 65802/n(417) 831-1300",
                "Tortilleria Perches\nElfindale Center, 1601 W Sunshine St, Springfield, MO 65807\n(417) 864-8195",
                "Purple Burrito\n5360 S Campbell Ave Springfield, MO 65810\n(417) 883-5305",
                "Amigos Mexican Restaurant\n2118 S Campbell Ave, Springfield, MO 65807\n(417) 887-1401",
                "Cantina Laredo\nAddress: 4109 S National Ave, Springfield, MO 65807\n(417) 881-7200" 
            };

            Random rand = new Random();


        }
    }
}
namespace BrowerD_Final_Project
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnMexican = new System.Windows.Forms.Button();
            this.txtResults = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btnMexican
            // 
            this.btnMexican.Location = new System.Drawing.Point(320, 76);
            this.btnMexican.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.btnMexican.Name = "btnMexican";
            this.btnMexican.Size = new System.Drawing.Size(173, 28);
            this.btnMexican.TabIndex = 0;
            this.btnMexican.Text = "Mexican";
            this.btnMexican.UseVisualStyleBackColor = true;
            this.btnMexican.Click += new System.EventHandler(this.btnMexican_Click);
            // 
            // txtResults
            // 
            this.txtResults.Location = new System.Drawing.Point(222, 441);
            this.txtResults.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.txtResults.Multiline = true;
            this.txtResults.Name = "txtResults";
            this.txtResults.Size = new System.Drawing.Size(367, 107);
            this.txtResults.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(789, 704);
            this.Controls.Add(this.txtResults);
            this.Controls.Add(this.btnMexican);
            this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnMexican;
        private System.Windows.Forms.TextBox txtResults;
    }
}



Aucun commentaire:

Enregistrer un commentaire