VB.net

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

Just starting to have a mess about with vb.net. I need to assign the backcolour of a form (called this variable sColour) to a variable, so i can echo this in a message box E.g. "You are using Green as a backcolour". What type of variable would i set this as and how can i define sColour? Any ideas?

Thanks
 
This should do it:
Code:
sColour As Color = SomeForm.BackColor
Where "SomeForm" is the name of the form whos background colour you want to get.
 
Welshy said:
This should do it:
Code:
sColour As Color = SomeForm.BackColor
Where "SomeForm" is the name of the form whos background colour you want to get.

I tried that first but i got a message says frmMain cannot relate to itself, use Me instead. So at the minute im using Me.Backcolour.....
 
suarve said:
I tried that first but i got a message says frmMain cannot relate to itself, use Me instead. So at the minute im using sColour = Me.Backcolour. Its weird cuz when i step through the code sColour has say Green stored for the colour of frmMain (e.g. whatever colour the form is), i'm still getting errors though :( ....
 
Welshy said:
Show us yer code :p

ok i have a form called frmMain that has a backcolor as red.

After a button has been clicked a message box should appear saying something like "Background colour is Red". For msg box the code is:

Code:
		Dim sColour As Color
		sColour = Me.BackColor

		'MessageBox.Show("Using the Scheme:  " & sColour)
 
Might be getting the wrong end of the stick, but...


If you create a new button, and add the following to the click event, doesn't this do what you want?

Dim usrBGColor as Drawing.Color
usrBGColor=Me.BackColor
MsgBox(usrBGColor.ToString)

Or even...

Msgbox(me.Backcolor.tostring)


:confused:
 
suarve said:
Some errors about operators & not being defined. meh
That's because you're trying to concatenate a string with a colour, doesn't really make a lot of sense, so they haven't defined the & operator to work in this context.

What you need is to get a string representation of the colour so you can concatenate the string together. As I'm sure you know all objects in .NET expose a ToString method so replace your last line with:

Code:
MessageBox.Show("Using the Scheme:  " & sColour.ToString())
 
Haircut said:
That's because you're trying to concatenate a string with a colour, doesn't really make a lot of sense, so they haven't defined the & operator to work in this context.

What you need is to get a string representation of the colour so you can concatenate the string together. As I'm sure you know all objects in .NET expose a ToString method so replace your last line with:

Code:
MessageBox.Show("Using the Scheme:  " & sColour.ToString())

Thanks for replies everyone, got it working now. As i have literally only just started learning vb.net a week ago, can you explain the tostring() bit more?
 
suarve said:
Thanks for replies everyone, got it working now. As i have literally only just started learning vb.net a week ago, can you explain the tostring() bit more?

Think in terms objects and types.

You obviously can't do the following:

result = 5 + " peas"

That makes utterly no sense at all.

What makes more sense is:

result = "5" + " peas"


Which is the same as doing

result = 5.ToString() + " peas"

:)
 
Right, time for more noob questions. I have the following:

Code:
MessageBox.Show("Hello " & txtName.Text & vbCrLf & "You Have Chosen the " & frmMain.BackColor.value "scheme")

the txtName.txt bit displays properly, but I cant get the background colour of the form (frmMain) to display. Any ideas?
 
What are the error messages it gives?

From first glances, you have forgotten the concatenation operator before "scheme" and I don't think .value is a property of a colour?

Surely Visual Studio should tell you things like that at design time?
 
Haircut said:
What are the error messages it gives?

From first glances, you have forgotten the concatenation operator before "scheme" and I don't think .value is a property of a colour?

Surely Visual Studio should tell you things like that at design time?

When i run the program the second line doesn't display at all :/

The second line should read as follows:

you have chosen the [what color the form backcolor is] scheme

As fopr the .value im sure it was something along those lines.....have just come a bit stuck :/
 
Haircut said:
Well, what you have posted won't even compile so I fail to see how the first line displays :confused:

txtName is just the txt from a text box and frmMain is a form with a backcolir set (need to output this colour on a message box).
 
Haha! Finally made it work. :o

Code:
MessageBox.Show("Hello " & txtName.Text & vbCrLf & _
			"You Have Chosen the " & Me.BackColor.Name & " Scheme", "Greetings", _
			MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
 
Just a quik tip. I would use the 'Me' keyword when reffering to controls as this will bring up intellisense and reduce spelling errors. :)

For example: Me.txtName.Text...

TrUz
 
Back
Top Bottom