Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio
  4. An "If" command for multiple conditions!

An "If" command for multiple conditions!

Scheduled Pinned Locked Moved Visual Studio
tutorialquestion
3 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 13336882
    wrote on last edited by
    #1

    Hello! I have a form with 2 textboxes and one buttton. Now, suppose the button displays a messagebox, something like this : msgbox("Hello world")! So, what i want is, the button wouldn't show the msgbox until both the textboxes have something typed in it! i've tried the "IF" command, but as far as i see it, the if command only adds one condition! For example , "IF TEXTBOX1.TEXT="" then msgbox("please fill in the form") else msgbox("Hellow world")"" But as i just said i have 2 textboxes, so is there a way to put both the textboxes in one single IF command? Something like: "IF THEXTBOX1.TEXT="" AND TEXTBOX2.TEXT="" THEN MSGBOX("PLEASE FILL IN THE FORM") ELSE MSGBOX("HELLO WORLD")?? One more thing , i want the msgbox to to show "HELLO WORLD" only when both the textboxes have something , if any of them are remained empty then msgbox will say " PLEASE FILL IN THE FORM" So , how should i do it ?

    T 1 Reply Last reply
    0
    • M Member 13336882

      Hello! I have a form with 2 textboxes and one buttton. Now, suppose the button displays a messagebox, something like this : msgbox("Hello world")! So, what i want is, the button wouldn't show the msgbox until both the textboxes have something typed in it! i've tried the "IF" command, but as far as i see it, the if command only adds one condition! For example , "IF TEXTBOX1.TEXT="" then msgbox("please fill in the form") else msgbox("Hellow world")"" But as i just said i have 2 textboxes, so is there a way to put both the textboxes in one single IF command? Something like: "IF THEXTBOX1.TEXT="" AND TEXTBOX2.TEXT="" THEN MSGBOX("PLEASE FILL IN THE FORM") ELSE MSGBOX("HELLO WORLD")?? One more thing , i want the msgbox to to show "HELLO WORLD" only when both the textboxes have something , if any of them are remained empty then msgbox will say " PLEASE FILL IN THE FORM" So , how should i do it ?

      T Offline
      T Offline
      Thomas Daniels
      wrote on last edited by
      #2

      You do not share which language you use, but it appears to be Visual Basic so I'm going to assume that for this answer. Visual Basic has an And operator which does exactly what you want (in fact, it will look like it did in your question :)):

      If TextBox1.Text = "" And TextBox2.Text = "" Then
      MsgBox("Please fill in the form")
      Else
      MsgBox("Hello World")
      End If

      (As a side note, you can also use String.IsNullOrEmpty[^] or String.IsNullOrWhiteSpace[^] rather than = "") About your final question: that change is very simple, all you have to do is replace your And operator by Or:

      If TextBox1.Text = "" Or TextBox2.Text = "" Then
      MsgBox("Please fill in the form")
      Else
      MsgBox("Hello World")
      End If

      The quick brown ProgramFOX jumps right over the Lazy<Dog>.

      Richard DeemingR 1 Reply Last reply
      0
      • T Thomas Daniels

        You do not share which language you use, but it appears to be Visual Basic so I'm going to assume that for this answer. Visual Basic has an And operator which does exactly what you want (in fact, it will look like it did in your question :)):

        If TextBox1.Text = "" And TextBox2.Text = "" Then
        MsgBox("Please fill in the form")
        Else
        MsgBox("Hello World")
        End If

        (As a side note, you can also use String.IsNullOrEmpty[^] or String.IsNullOrWhiteSpace[^] rather than = "") About your final question: that change is very simple, all you have to do is replace your And operator by Or:

        If TextBox1.Text = "" Or TextBox2.Text = "" Then
        MsgBox("Please fill in the form")
        Else
        MsgBox("Hello World")
        End If

        The quick brown ProgramFOX jumps right over the Lazy<Dog>.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        If it's VB.NET, you'll want to use AndAlso[^] / OrElse[^] to allow the operators to short-circuit. The Ballad of AndAlso and OrElse – Panopticon Central[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups