Mad Libs-style program problem?

  • Thread starter Thread starter Alice
  • Start date Start date
A

Alice

Hi -- I am trying to code a "mad libs" style program, in
which the user puts in nouns, adjectives, etc., into text
boxes. Then the user clicks on a button which opens a
message box. Inside the message box is a story with blanks
filled in by what the user entered into the text boxes.

Only when my message box opens, it does not plug in the
user's words.

Can anyone tell me what I'm doing wrong, or does anyone
have a sample of this kind of code?

Many thanks!
 
Hi Alice,

Sounds pretty simple - you just want to search and replace in a large
string. Please show us your code - specifically the code that parses the
long string - and I'm sure someone here can help.

HTH,

Bernie Yaeger
 
Thanks for responding.

Here is my code:

Public Class frmStoryGenerator
Inherits System.Windows.Forms.Form


' Declare variables for user input text boxes

Dim gstrSetting As String
Dim gstrActivity As String
Dim gstrVillain As String
Dim gstrPluralNoun1 As String
Dim gstrPluralNoun2 As String
Dim gstrAdj1 As String
Dim gstrAdj2 As String
Dim gstrAdj3 As String
Dim gstrAnimal As String
Dim gstrCountry As String
Dim gstrVerbIng1 As String
Dim gstrVerbIng2 As String
Dim gstrTimeOfYear As String

' Generate story in a message box

Private Sub btnStory_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnStory.Click


txtSetting.Text = gstrSetting
txtTimeOfYear.Text = gstrTimeOfYear
txtActivity.Text = gstrActivity
txtVillain.Text = gstrVillian
txtPluralNoun1.Text = gstrPluralNoun1
txtPluralNoun2.Text = gstrPluralNoun2
txtAdj1.Text = gstrAdj1
txtAdj2.Text = gstrAdj2
txtAdj3.Text = gstrAdj3
txtAnimal.Text = gstrAnimal
txtCountry.Text = gstrCountry
txtVerbIng1.Text = gstrVerbIng1
txtVerbIng2.Text = gstrVerbIng2

MessageBox.Show("Once, when I was in " &
gstrSetting & " in " & gstrTimeOfYear _
& ", I was out " & gstrActivity & " and came across a " &
gstrAdj1 & " sight. Of course, I was alone " _
& " at the time, so I had to face the danger alone. Though
it was a " & gstrAdj2 & " situation, I dove right in." _
& gstrVillain & " was trying to take over the world by " &
gstrVerbIng1 & gstrPluralNoun1 & ". The scheme" _
& " had to be stopped. But there were " & gstrAdj2 & " " &
gstrAdj3 & " in the way, not to mention voracious" _
& gstrPluralNoun2 & ". Still, it was all up to me to stop
it. I was victorious by " & gstrVerbIng2 & " into a large"
_
& " lake full of " & gstrAnimal & " and wrestling them all
while single-handedly convincing the local superhero of " _
& gstrCountry & " to step in and help. We started to
drive " & gstrVillian & " away. But just then, the
superhero " _
& " turned to wood, so it was all up to me. I stuffed the
villian into a large gunnysack and launched him to the
moon." _
& "Believe me, it happened. It's true." _
& " Really. The End.")

End Sub


End Class
 
Hi Alice,

You have your assignments backwards - gstrsetting = txtsetting.text not the
other way around.

Bernie
 
Why would you need to use replace?

Why not just build the text into the string in the first place.

This is a VERY basic version of what Alice was talking about:

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents tbxName As System.Windows.Forms.TextBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents tbxHome As System.Windows.Forms.TextBox

Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox

Friend WithEvents btnShow As System.Windows.Forms.Button

Friend WithEvents btnOK As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.tbxName = New System.Windows.Forms.TextBox()

Me.Label1 = New System.Windows.Forms.Label()

Me.Label2 = New System.Windows.Forms.Label()

Me.tbxHome = New System.Windows.Forms.TextBox()

Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()

Me.btnShow = New System.Windows.Forms.Button()

Me.btnOK = New System.Windows.Forms.Button()

Me.SuspendLayout()

'

'tbxName

'

Me.tbxName.Location = New System.Drawing.Point(88, 24)

Me.tbxName.Name = "tbxName"

Me.tbxName.TabIndex = 0

Me.tbxName.Text = ""

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(16, 24)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(56, 23)

Me.Label1.TabIndex = 1

Me.Label1.Text = "Name"

Me.Label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft

'

'Label2

'

Me.Label2.Location = New System.Drawing.Point(16, 56)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(64, 23)

Me.Label2.TabIndex = 3

Me.Label2.Text = "HomeTown"

Me.Label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft

'

'tbxHome

'

Me.tbxHome.Location = New System.Drawing.Point(88, 56)

Me.tbxHome.Name = "tbxHome"

Me.tbxHome.TabIndex = 2

Me.tbxHome.Text = ""

'

'RichTextBox1

'

Me.RichTextBox1.Location = New System.Drawing.Point(200, 24)

Me.RichTextBox1.Name = "RichTextBox1"

Me.RichTextBox1.Size = New System.Drawing.Size(208, 96)

Me.RichTextBox1.TabIndex = 4

Me.RichTextBox1.Text = ""

'

'btnShow

'

Me.btnShow.Location = New System.Drawing.Point(88, 96)

Me.btnShow.Name = "btnShow"

Me.btnShow.TabIndex = 5

Me.btnShow.Text = "Show"

'

'btnOK

'

Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK

Me.btnOK.Location = New System.Drawing.Point(168, 160)

Me.btnOK.Name = "btnOK"

Me.btnOK.TabIndex = 6

Me.btnOK.Text = "Ok"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(424, 221)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnOK,
Me.btnShow, Me.RichTextBox1, Me.Label2, Me.tbxHome, Me.Label1, Me.tbxName})

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Me.RichTextBox1.Text = "My name is " & Me.tbxName.Text &
Microsoft.VisualBasic.ChrW(10) & "I was born in " & Me.tbxHome.Text

End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click

Dispose()

End Sub

End Class
 
Bernie,

You absolutely rock! I can't believe I didn't see that!
Thank you so much! My program works perfectly now.

RDI,
Thank you for showing me an alternative way I could do
this kind of program!

Many thanks,
Alice
 
Hi Alice,

Your very welcome.

Bernie
Alice said:
Bernie,

You absolutely rock! I can't believe I didn't see that!
Thank you so much! My program works perfectly now.

RDI,
Thank you for showing me an alternative way I could do
this kind of program!

Many thanks,
Alice
 
Back
Top