Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vb.net question

Status
Not open for further replies.

eHaviv

Technical User
Oct 24, 2019
8
IL
Hi

I have a form with one button
and one textbox

In the button event I set a boolean
flag that also exists in another form method that control a loop (exit from loop)

My question is this control is ok
If not what I need to do.

Thank you.
 
This forum is for VB Classic (i.e VB 3/4/5/6). You want to post in forum1867
 
He (she?) did - thread1867-1798175
Still not clear what the question is.... [ponder]


---- Andy

There is a great need for a sarcasm font.
 
Thank you all

'
'if I click Display message no message are displayed becaus flag is False
'When i click the button falg false to flip it to flag True.
'the app do not react.
'
'how I can make this simple app to react when fliping bettween
'true and false
'I am very thank you for any help

'
'
Imports System
Imports System.Windows.Forms

Module m1

Sub Main()

Dim myForm As New Form1
myForm.ShowDialog()

End Sub

End Module


Public Class Form1

Dim message1 As String = Nothing
Dim flag As Boolean = False

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
LengthyTask()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
flag = Not flag
Me.Button2.Text = "Flag = " & flag.ToString
End Sub

Public Sub LengthyTask()
While True
If flag Then
Exit While
End If
End While
msgbox("TextBox1.Text = " & TextBox1.Text)
End Sub

End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
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.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(102, 222)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(459, 22)
Me.TextBox1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(102, 124)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(234, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Display Message"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(362, 124)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(199, 23)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Flag = False"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(688, 458)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button

End Class


 
Fundamentally the problem is that your logic is needlessly obtuse.

Probably compounded by a lack of understanding of variable scope.

Why not use a control that inherently return a Boolean, instead of a button and a variable?

Why not use the Boolean directly to control your do nothing while loop.
 
mintjulep Hi and thank you very much.

The variable is in scope because if
Firstly I click the button to make
flag = true then the message is displayed.
When I click display message button.

But if I firstly click display message button when the flag = false then
the spp stack.

Thank you very much.
 
You can compile by this command line.


C:\WINDOWS\Microsoft.NET\Framework\v3.5\vbc /target:winexe samples_form1.vb >> f1.txt

And test it.
 
Your [tt]While[/tt] loop is an infinite loop when your [tt]flag[/tt] is False:

Code:
Public Sub LengthyTask()

While [red]True[/red]   [green]'While WHAT is True?[/green]
    If flag Then
        Exit While
    End If
End While

 msgbox("TextBox1.Text = " & TextBox1.Text)

 End Sub

You exit your loop with [tt]Exit While[/tt] when the flag is True, but there is no Exit when the flag is false, because your [red][tt]True[/tt][/red] is always True

(If that makes any sense...)


---- Andy

There is a great need for a sarcasm font.
 
Hi Andrzejek and thank you very much.

The while loop is ok.
Because if I firstly click display message
button the while enter into an infinite
loop because flag is false.
and now clicking flag button to change
Boolean variable to true and then the
message is displayed. This work ok.

But from this point when I click
the flag button to flip between
true and false this not working.

I don't know the reason.

Thank you very much.
 
>I don't know the reason.

I do, and I answered on the other thread in the proper forum earlier
 
Thank you all.

I am wrong along all the way

I need using threading for this.

Thank you again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top