Sign in to follow this  

VB beginner

This topic is 4706 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

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
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
I have another problem now :(

  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim IzabraniFont As String = ComboBox1.SelectedItem
RichTextBox1.Font = New Font(IzabraniFont, 12, Fontstyle.Regular, 0)
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim IzabranaVelicina As Integer = ComboBox2.SelectedItem
RichTextBox1.Font = New Font("Arial", IzabranaVelicina, Fontstyle.Regular, 0)
End Sub


Those are 2 different subs so I cant use strings from first sub in second sub. I declared a string named "IzabraniFont" in first sub, and an integer named "IzabranaVelicina" in second sub. How can I connect those two variables so I can use both in both subs?
I mean, I want to have a line like this:
RichTextBox1.Font = New Font(IzabraniFont, IzabranaVelicina, Fontstyle.Regular, 0)

in both subs

Please help,
-rex

Share this post


Link to post
Share on other sites
What you need is to declare a variable with a larger scope. Basically, those variables can't be seen outside the proceedure in which they are declared. You have a few options here, with the simplest and probably most appropriate one being to make the variables available to the whole form. To do this, declare them using the same method, but in general declerations (right up the top of the forms code). Oh, and don't forget to remove the old decleration.

Don't forget that this will slightly change the behavior of the variables, in that when the sub is called, they will still contain the value from last time it was called.

(This is all assuming that VB.Net hasn't changed the fundamental structure too much since VB6).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sry i don't know how to put the code in a frame
You should see something like this:

Public Class frmTest
Inherits System.Windows.Forms.Form
Dim IzabraniFont As String
Dim IzabranaVelicina As Integer

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
IzabraniFont = ComboBox1.SelectedItem
RichTextBox1.Font = New Font(IzabraniFont, 12, Fontstyle.Regular, 0)
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
IzabranaVelicina = ComboBox2.SelectedItem
RichTextBox1.Font = New Font("Arial", IzabranaVelicina, Fontstyle.Regular, 0)
End Sub
End Class


somewhere in the middle (between 'Integer' and 'Private') of that code you will also see 'Windows Form designer generated code'. i suggest u to click the next link ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vbconHelloWorld.htm or just copy paste this into your browser (i believe you had help installed?) and press enter. u will find a basic basicprogram and (more interesting) links to other help files. If you do't know how to declare variables at with larger scope, you click help>index>type 'scope'>see what comes up. Maybe the first thing isn't the right, but do search on for 5 minutes. You can also get a book on vb.net from your library. Maybe you are an expert programmer in qbasic (with which i started too) but there have been some drastic changes in code models since then. Personnally i think a book is the best thing for you. If you see in the beginning ho that's way to easy, then just go straight to the next chapter.

Share this post


Link to post
Share on other sites

This topic is 4706 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this