Public Group

# VB beginner

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

## 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 on other sites
What version of Visual Basic are you using. VB6, VB.Net 2002, or VB.Net 2003.

##### Share on other sites
Dim Msg1 As IntegerMsg1 = Msgbox("Message text" & Chr(13) & "Second line of message text", vbYesNoCancel, "Messagebox title")If Msg1 = vbYes Then  'do somethingElseIf Msg1 = vbNo Then  'do something elseElseIf 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 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 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 integerresult = 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 hereElseIf ( result = DialogResult.No ) Then    'execute code for "no" results hereElse    'execute code for cancel hereEnd 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 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 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 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 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 on other sites
Can anyone help me with this please?

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009769
• ### Who's Online (See full list)

There are no registered users currently online

×