vb6 check to see if textbox is empty - textbox

There is a similar question to this but it is for C#, Check if TextBox is empty and return MessageBox?.
There is another solution that check if textbox is empty https://www.daniweb.com/software-development/visual-basic-4-5-6/threads/414651/checking-if-textbox-is-empty, but this works if you are going to check all the textbox in the form. I would like to check some textbox in the form if they are empty or not.
I've written this code to check if textboxes are empty
Private sub checkEmpty()
If text1.text = "" Or text2.text="" Then
blank = true
End If
End Sub
Then added this code to my command button
Private Sub command1_Click()
checkEmpty
If blank = true Then
Msgbox "a text box is empty"
Else
Msgbox "Text box has text"
End If
End Sub
The problem when I start the program it gives the output "Text box has text" even if there are no text in the text boxes.
What is wrong with this code?

You need to change your procedure to a function that returns a value (I'd change the name at the same time to make it more clear what it does).
Private Function AnyTextBoxEmpty() As Boolean
AnyTextBoxEmpty = text1.Text = "" or text2.Text = ""
End Function
Private Sub command1_Click()
If AnyTextBoxEmpty Then
Msgbox "a text box is empty"
Else
Msgbox "Text box has text"
End If
End Sub

Related

Maintain Focus with two textbox and button using Brightscript

I created a one Login Form in the bright script. It's following
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
TextBox 1 ' Here the focus is active I set by default in TextBox field active = true
TextBox 2 ' Here the press down key to active true
Button 1 ' Here again press down key to focus true
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Here, I maintain the 3 Items using 3 different key. Now I want to maintain the single key for all 3 items using the down key. Anyone idea to How to maintain Focus using Brightscript.
I used the one function for key handling It's here
function onKeyEvent(key as String, press as Boolean) as Boolean
.................
end function
Now, I maintained the key like
I set TextBox Focus active in ByDefault to XML File.Now I apply Logic to below.
First item focus set on XML file by default.
if key = "down" then
'Here Second item focus
m.keypass.active = true ' Here work successfully First time
if key = "down" and m.keypass.active = true and m.btnsub.active = false then
'Here not maintain successfully its directly call here I press the down key.
m.keypass.active = false
m.btnsub.active = true 'Here third item focus is not maintained
end if
end if
I first-time press the down key It's working fine But its second time How handling the Focus. I used the same thing in Up key.
Here I am using "and" then the issue will happen is there any idea.
Pls, Check Here's an image really what I want to do.
Edited Post:
I handle with up and down key with below code. It's working But, Its only work in a single time.
if key = "up" or key = "down"
if key = "down"
?"here down key"
if m.keypass.id = "instructpass" and m.keypass.active = true
? "down key if part"
m.btngrp.setFocus(true)
m.keypass.active = false
handled = true
else if m.keyid.id = "instructid" and m.keyid.active = true
?" down key else part"
m.keypass.active = true
m.keyid.active = false
handled = true
else if m.btngrp.buttonSelected = 0
m.keyid.active = true
m.btngrp.setFocus(false)
handled = true
end if
handled = true
else if key = "up"
? "here up key"
if m.keypass.active = true
?"up key if part"
m.keyid.active = true
m.keypass.active = false
handled = true
else if m.keyid.active = true
?"id key"
m.btngrp.setFocus(true)
m.btngrp.focusButton = 1
m.keyid.active = false
handled = true
else if m.btngrp.focusButton = 0 and m.btngrp.buttonSelected = 0
?"up key else part"
m.keypass.active = true
m.keypass.setFocus(true)
m.btngrp.setFocus(false)
handled = true
end if
handled = true
end if
handled = true
end if
Thank you.
Check here.
You should use .setFocus(true) and .hasFocus(), which are available to most renderable nodes such as TextEditBox and Button.
E.g.
if key = "down" then
if textBox1.hasFocus() then
textBox2.setFocus(true)
elseif textBox2.hasFocus() then
button.setFocus(true)
end if
end if
if key = "up" then
if button.hasFocus() then
textBox2.setFocus(true)
elseif textBox2.hasFocus() then
textBox1.setFocus(true)
end if
end if

How to set a variable to nil if the user leaves their answer as blank?

I'm learning ruby and am a bit stuck. They want us to set a variable as nil if the user leaves the question blank. Otherwise convert their answer to an integer. I came up with the following, but when I leave the answer blank, it prints 0. Could you steer me in the right direction?
puts "What's your favorite number?"
number = gets.chomp
if number == ' '
number = nil
else
number = number.to_i
end
p number
You're only testing if the entered number is explicitly a single space. If you're testing for 'blankness' you probably want to strip the input you receive and then test if it is empty?.
E.g.
number = gets.strip
if number.empty?
number = nil
else
number = number.to_i
end
You've tagged this with ruby-on-rails so I'm assuming you are considering a string to be blank if blank? returns true (i.e. the string is empty or consists only of whitespace. If you are using rails then you can use that blank? method to test the input:
number = gets
if number.blank?
number = nil
else
number = number.to_i
end
You've got an extra space - if number == ' ' - this should be if number == ''
An alternative way would be to say if number.length == 0
you can do like this
if number.empty?
number = nil
else
number = number.to_I
end
or in single line you can do like this
number = number.empty? ? nil : number.to_i

How to check if the (+X) last text

I have the following code:
local text = 'CIA'
if text == text..'+X' then
print 'true'
else
print 'false'
end
I want check if last text ends with ('+X')
The string.sub function extracts a substring. Negative indices start from the end.
if string.sub(text, -2) == '+X' then
-- Ends with +X, do stuff accordingly.
end

Field Validation in Admin when field are dependent on other fields

How can i apply validation in admin on various fields when they are dependent on each other ?
e.g. Let say in i have a Field A(BooleanField) and Field B (CharField) what i want to do is if in admin user select the Field A(checkbox) and does not enter anything in Field B
and if he tries to save ,it should throw an error like a normal blank=False gives. So how can i do this kind of validation in admin .
E.g Use Case
I have a table having the following structure :-
INTERVIEW_TYPES = (
('default', 'None'),
('Paired Visit','Paired Visit'),
('Time Series', 'Time Series'),
),
class Interview(models.Model):
ic_number = models.CharField(verbose_name ="Visit Configuration Number",max_length=20,unique=True,null =True,blank=True)
ic_description = models.TextField(verbose_name ="Visit Configuration Description",null = True,blank=True)
title = models.CharField(verbose_name ="Visit Configuration Title",max_length=80,unique=True)
starting_section = models.ForeignKey(Section)
interview_type = models.CharField(verbose_name = "Mapped Visit",choices=CHOICES.INTERVIEW_TYPES, max_length=80, default="Time Series")
select_rating = models.CharField(choices=CHOICES.QUESTION_RATING, max_length=80, default="Select Rating")
view_notes = models.CharField(choices=CHOICES.VIEW_NOTES, max_length=80, default="Display Notes")
revisit = models.BooleanField(default=False)
.....and so on ......
class Meta:
verbose_name = 'Visit Configuration'
verbose_name_plural = 'Visit Configurations'
# ordering = ('rpn_number',)
def __unicode__(self):
return self.title
Its admin.py
class InterviewAdmin(admin.ModelAdmin):
list_display = ('id','title', 'starting_section','ic_number','show_prior_responses')
raw_id_fields = ('starting_section',)
admin.site.register(Interview, InterviewAdmin)
In admin , If i select the checkbox of revisit and in the field interview_type(which will show a dropdown having choices None,Paired Visit , Time Series) if a User has selected None from that dropdown and then press save button it should throw me an error like a normal blank=False shows, saying "This field is required"
How can i do this kind validation where fields are dependent on each other ?
Please Ignore syntax error is any .
Thanks
I got confused in response_change and overriding clean method finally this is what i did
override clean method by making a model form in admin.py
class InterviewAdminForm(forms.ModelForm):
class Meta:
model = Interview
def clean(self, *args, **kwargs):
cleaned_data = super(InterviewAdminForm, self).clean(*args, **kwargs)
if self.cleaned_data['interview_type'] == "default" \
and self.cleaned_data['Revisit'] == True:
raise forms.ValidationError({'interview_type': ["error message",]})
return cleaned_data
class InterviewAdmin(admin.ModelAdmin):
# call the form for Validation
form = InterviewAdminForm
#....and so on ....

Ruby detecting if value is set in variable setting

I have a function generate_username that generates a username (obviously).
The values fname and lname are mandatory, so no issues there. However, mname is NOT a mandatory field so, it may be blank, which breaks this code.
Any suggestions on how to ask ruby to only print the mname value if it exists or is set and ignore it if the user left it blank?
def generate_username
self.username = fname.to_s.split("")[0] + mname.to_s.split("")[0] + lname.to_s
end
You could try this (parentheses are important):
def generate_username
self.username = fname.to_s.split("")[0] << (mname.to_s.split("")[0] || "") << lname.to_s
end
Throwing a simple ternary operator in to check if the value is blank? should do the trick.
def generate_username
self.username = fname.to_s.split("")[0] + (mname.blank? ? "" : mname.to_s.split("")[0]) + lname.to_s
end
In ruby 1.9
def generate_username
"#{fname[0]}#{mname.to_s[0]}#{lname}"
end
or
def generate_username
fname[0]+mname.to_s[0].to_s+lname
end
In ruby 1.8, replace all the [0] with [0, 1] (This point added after being pointed out by Peter).
mname.to_s ensures you get a string; when mname is nil it will be an empty string "".
String#[0] picks up the first character of that string; when the string is empty, it will return nil.
#{ } within " " expands the ruby code, and turns it into a string; particularly turns nil into an empty string "".

Resources