Jump to content
  • Advertisement
Sign in to follow this  
rexTroianus

VB beginner

This topic is 4920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, I started with VB after few years working with QBasic. I have few questions. I want to make a message box with Yes, No and Cancel options and different actions for each one. Here is the code I wrote but it doesnt work properly:
If MessageBox.Show("The text has been changed. Do you want to save changes?", "TextEditor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
            RichTextBox1.SelectAll()
        ElseIf DialogResult.No Then
            RichTextBox1.Clear()
        Else

        End If
Dont pay attention on action after Yes is clicked, I put it just for testing. One more thing. How to go in the next row in the middle of the text (\n in c++) ? Thanks, -rex

Share this post


Link to post
Share on other sites
Advertisement

Dim Msg1 As Integer
Msg1 = Msgbox("Message text" & Chr(13) & "Second line of message text", vbYesNoCancel, "Messagebox title")
If Msg1 = vbYes Then
'do something
ElseIf Msg1 = vbNo Then
'do something else
ElseIf Msg1 = vbCancel Then
'do nothing (unless for some reason you want to do something... whatever)
End If




To move to the next line, you insert character 13, by putting Chr(13) into the string at the appropriate point.

/EDIT: Just noticed how extremely different your code sample looks... are you using .Net? (If so, the code sample I've given is probably completely invalid).

Share this post


Link to post
Share on other sites
Ok it appears he is not using VB6 because of the way he is using the new MessageBox object that is in .net.

here is a example I put together using VB.Net 2003 will work in VB.Net 2002 as well.


If (MessageBox.Show("Click Yes or No", "Yes or No ?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes) Then
MessageBox.Show("You clicked Yes", "Yes")
Else
MessageBox.Show("You clicked No", "No")
End If


Share this post


Link to post
Share on other sites
Quote:
What version of Visual Basic are you using.

Definitely looks like VB.Net to me. Not sure that it matters whether its 2002/2003/2005 given the relatively simple code snippet.


Quote:
To move to the next line, you insert character 13, by putting Chr(13) into the string at the appropriate point.

I think the preferred way was to use vbCr or vbCrLf instead of the raw character codes, but then again, I suppose its just a matter of preference [smile].

However, what Kazgoroth said is correct - but you will probably need to look up the .Net equivelent..

Quote:
it doesnt work properly:

What exactly do you mean it doesn't work properly? just that your code isn't executed?

I've got a feeling that you'll need to re-arrange your logic to get it working. At a guess:


dim result as integer
result = MessageBox.Show(
"The text has been changed. Do you want to save changes?",
"TextEditor",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Exclamation
)

If ( result = DialogResult.Yes ) Then
'execute code for "yes" result here

ElseIf ( result = DialogResult.No ) Then
'execute code for "no" results here

Else
'execute code for cancel here

End If





The reason I suggest that way, is that in your original code you weren't checking the No result properly - if the first case (Yes) didn't work then it'd fall down to checking whether DialogResult.No is true or false without comparing it to anything. If VB.Net is anything like VB6 then if DialogResult.No is non-zero it will be considered as if it were true.

hth
Jack

Share this post


Link to post
Share on other sites
Everything is working fine now! Guys thank you very much!
I used "& Chr(13) &" for new line and jollyjefers' code for yes,no,cancel message box.

Thank you,
-rex

Share this post


Link to post
Share on other sites
Not a problem, apolagies for assuming VB6 and posting a largely useless code sample. [wink]

As for the "& Chr(13) &", I just thought I'd post a brief explanation in case you werent aware how it works. The ampersand character basically appends 2 strings together, which can be useful for a number of things, including the use of variables within a largely hardcoded sentence. Chr() takes a character code, and the character code 13 is the code for the return key (in VB6 this was also stored in the constant VBKeyReturn, but I'm unsure about .NET).

So basically, you're providing the first part of the string, appending the character code for a new line, then appending to that the remainder of the string.

(Note: oversimplidied explanation)

Share this post


Link to post
Share on other sites
Hello,
I'm trying to get Bold letters. I put a check box and rich text box. Here is the code I have. I dont have any errors but the letters are always the same:

If CheckBox1.CheckState.Checked Then
RichTextBox1.Font.Bold.GetType()
End If


Thanks,
-rex

Share this post


Link to post
Share on other sites
Actually I did it with following code:

RichTextBox1.Font = New Font(Me.Font, Fontstyle.Bold)


but when (e.g.) both Italic and Bold are select the text wont be Bold AND Italic, it will be the last you checked.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!