-
Button edges and font size
1. I see the toolbox shows buttons with rounded corners, but when I instantiate them, they have square corners.
2. I cannot change the font size of buttons as it inherits the font size from the form. Is there no way I can change this?
3. How do I hide the borders of a button?
My own thinking if this cannot be done, is to rather use a label as a button or load a picture on the button.
Thanks for the help.
PK
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
1. I see the toolbox shows buttons with rounded corners, but when I instantiate them, they have square corners.
The appearance of Buttons and other controls depends on the OS. .NET Buttons will look different on various versions of Windows. XP and 7 will show rounded corners while 8.x and 10, which have a flat appearance, will show square corners.
Quote:
Originally Posted by
Peekay
2. I cannot change the font size of buttons as it inherits the font size from the form. Is there no way I can change this?
Of course you can change the font size. Select the Button, open the Properties window, expand the Font property and set the Size.
Quote:
Originally Posted by
Peekay
3. How do I hide the borders of a button?
Set the FlatStyle property to Flat and the FlatAppearance.BorderSize property to 0.
-
Re: Button edges and font size
Thanks you so much, jmc....
1. I have Windows 7 and mine looks square, although, when I look through a binocular, it might be rounded.
2. I am instantiating the controls at run time, and when I try to set the property of the font size there, it says that font size is a read only property.
-
Re: Button edges and font size
And whilst I am at it.
In VB6 you had controls with the same name and an Index. Here I see that I can use an array, but it does not have an index property.
To overcome that I try to index them by setting the Tag property to its index.
The trouble is that when I click it, I do not know which button was clicked.
I tried : If sender=btnFn(1) then ... - but it does not work.
-
Re: Button edges and font size
Yeah, they got a (few) things wrong with vb.net imo, and one of them is the lack of control arrays (or rather, the lack of simplicity aka VB6 model) and the annoying read-only for some stuff that I can't remember ever causing me annoyance before. I know some people will disagree, but that's my opinion.
As for the font issue, you need to change the whole font instead of "editing" it like you're used to doing:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MyFont As New Font("Arial", 13)
Button1.Font = MyFont
End Sub
You have a bunch of overloads to work with there (you can obviously set "Bold" etc).
One way to replicate controls arrays would be:
Code:
Public Class Form1
Private MyButton(2) As Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyButton(0) = Button1
MyButton(1) = Button2
MyButton(2) = Button3
End Sub
End Class
Now you can work with them like you're used to, eg
Code:
For i as Integer = 0 to 2
MyButton.Text = "Hello"
Next i
There are probably more convenient ways than that though.
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
The trouble is that when I click it, I do not know which button was clicked.
Of course you do. The 'sender' IS the Button. Control arrays exist in VB6 for a reason and that reason doesn't exist in VB.NET, hence control arrays don't exist. That reason is to be able to handle an event for multiple controls with a single method. You create a control array and then handle the event of the array. In VB.NET, if you want to handle multiple events*with a single method then you simply put multiple events in the Handles clause, e.g.
vb.net Code:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Inside the event handler, the*'sender'*is the object that raised the event, i.e. the Button that was clicked, e.g.
vb.net Code:
If sender Is Button1 Then
MessageBox.Show("Button1 was clicked")
ElseIf sender Is Button2 Then
MessageBox.Show("Button2 was clicked")
ElseIf sender Is Button3 Then
MessageBox.Show("Button3 was clicked")
End If
If you've added the Buttons at run time then you might want to do something like this:
vb.net Code:
Dim index = myButtonArray.IndexOf(DirectCast(sender, Button))
-
Re: Button edges and font size
Hi Nirwanda,
Thanks for your help.
I said previously that I am downgrading from VB6 to VB.NET, but find it very wasterful as I loose too many functionality.
The following are serious shortcomings in VB.NET:
1. Control arrays are easy and practical to work with
2. The MSFlexgrid control is powerful and versatile and editing data in it is a breeze.
3. There is no Gosub anymore.
4. DAO was easy, user friendly and really fast
Why do I want to change?
-
Re: Button edges and font size
I haven't really come into contact with anything other than 1) on your list, so I can't comment on that.
In general though, vb.net is simply more modern and much better than vb6 in almost every way. The vast majority of stuff that has changed has become better. I didn't use to think that when I started programming with it, but that's because I'm stubborn by nature.
Classes and lists are very poweful tools, and while they exist in vb6, they are nowhere nearly as well made or convenient as their .net counterpart.
Also, there are some stuff that is simply not possible in vb6, because the language is outdated. One major thing is threading. You can't even code for the mouse scrollwheel unless you do some messy subclassing, because of course the scroll wheel didn't exist back then!
The debugging aspect is much improved too; you can spot a lot of stuff before your application even starts. And FINALLY the application doesn't hang up and forces you to ctrl+alt+del out of it when you mess up your code and get stuck in an endless loop like I always do. :D
This is just my take on it. I'm not a wiz .net programmer by any stretch of imagination, and the stuff I've mentioned have been the stuff that I have noticed myself. Everyone will have their view on it.
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
1. Control arrays are easy and practical to work with
That is no shortcoming at all. I have been working in .NET since 2003 and I can count the number of times that I've wished I could create a control array on zero fingers. As I said, control arrays exist in VB6 for the sake of event handling. They're not needed for that reason in VB.NET so they don't exist. On the rare occasions that you actually do need an array of controls then, lo and behold, you simply create an array of controls. They are no different to any other arrays. If you think you need more than that then you're doing it wrong. If you are used to riding a horse and then you get a car but you keep trying to ride it like a horse, you might think that cars have serious shortcomings.
Quote:
Originally Posted by
Peekay
2. The MSFlexgrid control is powerful and versatile and editing data in it is a breeze.
I've never used the MSFlexGrid so I can't speak to specifics but the DataGridView is powerful and versatile and editing data is a breeze. It may not be able to do some things out of the box that the MSFlexGrid can do but it is extremely customisable and I bet that it has some tricks of it's own.
Quote:
Originally Posted by
Peekay
3. There is no Gosub anymore.
How is that a shortcoming exactly? Why would you not simply put the subroutine code in its own method?
Quote:
Originally Posted by
Peekay
4. DAO was easy, user friendly and really fast
I've never had any issues with ADO.NET.
Quote:
Originally Posted by
Peekay
Why do I want to change?
To be frank, most of this simply comes down to what you're used to. Even if something newer is better, which isn't always the case, if you keep expecting it to work like the old then you're never going to be happy with the outcome. If you don't want to change then there's a very simple solution that will avoid a waste of everyone's time.
-
Re: Button edges and font size
Quote:
Originally Posted by
jmcilhinney
To be frank, most of this simply comes down to what you're used to. Even if something newer is better, which isn't always the case, if you keep expecting it to work like the old then you're never going to be happy with the outcome. If you don't want to change then there's a very simple solution that will avoid a waste of everyone's time.
Indeed. I was quite resistant to .Net early on and rightfully so in some areas for what I was doing. While I did have one project in it around 2008 I kept to VB6 until late 2014 (when I finally joined up here - surprise, surprise). I've done pretty much only .Net since then and it pains me when I need to go back to VB6 code now. Unfortunately, there is still a ton about .Net I don't know well enough yet (though perhaps will never use either).
-
Re: Button edges and font size
I've programmed with VB6 for years. After I went to VB.NET 2003, it was difficult. I also missed DAO and ControlArrays, but once you learn how to do these things in VB.NET, you will never want to go back to using them again. As for Gosub, Gosub was useless. It's extra words that don't need to exist. ControlArrays are also now useless. I can, in less than a dozen lines of code, tell my program to put 1000 buttons on the screen and know which one was pushed. Even DAO, you realize how useless it is, when you start working with ADO.NET. I can't even think of a single thing better with DAO.
The problems you're having Peekay, are the same problems I had. You don't understand the language yet. It LOOKS a lot like VB6, but it has as much in common with VB6 as a bicycle does with a Mercedes. I'll mention it's hardly a downgrade. I can do things in VB.NET that I couldn't dream of doing in VB6 without thousands of external library calls or pages of complex routines. What used to take me an entire page of typing, now takes a few lines because there are so many built-in tools.
Don't sell yourself short as a programmer and refuse to understand the language. If I did that 12 years ago, I would be one of those VB6 programmers who worry every new version of Windows whether it would be the last one my programs would work with and constantly wonder how people are able to do so much more in such little time with .NET. Learn it and love it. I guarantee, you'll never look back.
-
Re: Button edges and font size
Quote:
Originally Posted by
jmcilhinney
...
Quote:
Originally Posted by
Peekay
3. There is no Gosub anymore.
How is that a shortcoming exactly? Why would you not simply put the subroutine code in its own method?
...
I didn't use Gosub often in VB6 so it wouldn't be an issue for me porting existing code to VB.Net, but there was a use for Gosub that could be considered justifiable.
Say you have your code divided up into a number of methods, and you're using a lot of local scope variables in a method that is doing a several repetitious type of processing referencing those local variables, you could segregate the repetitious code into one or more blocks at the bottom of the method and call them when needed with GoSub. The code would have access to all the local variables of the method and do the necessary calculations using them without having to pass a lot of variables to a method by reference.
Sort of like a built in macro capability.
In .Net I guess if you really needed to port code written that way fairly directly to .Net you could create a Shared (static) class that encapsulates the method as a Public Shared Method, the local variables become shared class level variables, and all the GoSubs would be Private Shared Methods of the class.
If you didn't mind instantiating one instance of the class, at startup somewhere then it wouldn't have to be a Shared class.
-
Re: Button edges and font size
Quote:
Originally Posted by
jmcilhinney
...
Quote:
Originally Posted by
Peekay
3. There is no Gosub anymore.
How is that a shortcoming exactly? Why would you not simply put the subroutine code in its own method?
...
I didn't use Gosub often in VB6 so it wouldn't be an issue for me porting existing code to VB.Net, but there was a use for Gosub that could be considered justifiable.
Say you have your code divided up into a number of methods, and you're using a lot of local scope variables in a method that is doing a several repetitious type of processing referencing those local variables, you could segregate the repetitious code into one or more blocks at the bottom of the method and call them when needed with GoSub. The Gosub code would have access to all the local variables of the method and do the necessary calculations using them and modifying them without having to pass a lot of variables to a method by reference.
Sort of like a built in macro capability within a method.
In .Net I guess if you really needed to port code written that way fairly directly to .Net you could create a Shared (static) class that encapsulates the method as a Public Shared Method, the local variables become shared class level variables, and all the GoSubs would be Private Shared Methods of the class.
If you didn't mind instantiating one instance of the class, at startup somewhere then it wouldn't have to be a Shared class.
p.s. Peekay, don't know if you could use an example of a port of VB6 (originally VB3) code of a card game which used a control array of pictureboxes for the cards that was ported to VB.Net. I actually only ported it to VB.Net in response to a post about porting VB6 code with control arrays and it didn't take too long to do the port.
A second thread on the same topic has links to the earlier thread, so here is a link to that second thread's post.
I agree with what others are saying. The paradigm shift from VB6 to VB.Net was a bit of a shock, but I don't really miss too much about it, and do all my work in VB.Net with no regrets.
-
Re: Button edges and font size
jmcilhinny,
I am so sorry that I have wasted your time and some of the other folks you mentioned. It was never my intention.
To the others (passel, Jenner and Topshot): I really enjoyed your comments which gave me some insight, as you gather that I am trying to change over to VB.NET for the tenth time and it is quite traumatic for me. I still do not know what I need over and above VB6. I have been programming since VB3 and it is about 25 years now. Never did I have a problem with not having enough functions or power in VB6.
My view is that I love building an engine doing all the parts carefully for that specific engine, in stead of grabbing one from the .NET framework. I maintain many programs written in VB6 and am trying to imagine them in VB.NET, but not knowing the power of VB.NET makes it difficult, thus my reason for wasting some of you folks's time.
The instances I have enumerated above is but a small collection of my irritations with VB.NET.
I have been programming since 1968 with Fortrans IV and it was never a problem for me to transform to the newer versions. In fact, my oldest program I wrote in Fortran IV ( a 3D structural analysis program) I have over the years upgraded about 200 times and still use it today.
I appreciate all you folks's contribution to my knowledge base.
Thanks so much!
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
I said previously that I am downgrading from VB6 to VB.NET
Sorry but how is this a downgrade, I loved VB6 at the time, but there is nothing that I miss about it, the fact that it is different, may be a struggle for the transition, but the differences are not bad.
Quote:
Originally Posted by
Peekay
1. Control arrays are easy and practical to work with
I don't really understand this, control arrays were horrid, they were the only way to do things in vb6...
- In .Net you have alot more control, you can add controls and create lamda events for them with out a need to even have a reference to the control!...
- You can store the controls in a dictionary / list / array etc.
- You can inherit a control to give it additional properties and use linq to lookup the controls by that criteria from a list / another controls children etc.
Quote:
Originally Posted by
Peekay
2. The MSFlexgrid control is powerful and versatile and editing data in it is a breeze.
Can't really speak of this as I used the SGrid instead.
Quote:
Originally Posted by
Peekay
3. There is no Gosub anymore.
You can define Lamdas and call them like this:
VB.Net Code:
Dim Test1 = Sub(Text As String)
MsgBox(Text)
End Sub
Test1.Invoke("Hey there")
Dim Test2 = Function(Text As String) As String
Return UCase(Text)
End Function
MsgBox(Test2.Invoke("Hey there"))
..I find these much better and cleaner than GoSub
I do this quite a bit, as occasionally with methods you have a very specific thing you want to do several times within a method, but is so specific you don't really care about it from outside.
Quote:
Originally Posted by
Peekay
4. DAO was easy, user friendly and really fast
You should really look into Linq2SQL etc... I can call things like this:
VB.Net Code:
Dim AveAge = dbml.Person.Extend.Average(Function(x) x.Age)
(this will get the average age of everyone in my program)
.. and have it generate the SQL for me ...
.. check here for the post
Kris
-
Re: Button edges and font size
Nah, you're going overboard. Control arrays were certainly not "horrid". They did a perfectly good job of indexing your controls at design time, which is often the only thing you need.
The only thing I really felt lacking was the ability to make them multi-dimensional.
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
jmcilhinny,
I am so sorry that I have wasted your time and some of the other folks you mentioned. It was never my intention.
It was primarily your time that I was referring to, but then that leads on to everyone else whom you ask for help to do things in VB.NET that you can already do in VB6. My point was basically that no one is making you to VB.NET so either don't do it or accept that you're doing it and don't hold yourself back by wishing it was VB6 every time you encounter something different.
-
Re: Button edges and font size
Quote:
Originally Posted by
passel
In .Net I guess if you really needed to port code written that way fairly directly to .Net you could create a Shared (static) class that encapsulates the method as a Public Shared Method, the local variables become shared class level variables, and all the GoSubs would be Private Shared Methods of the class.
I can see how that might be an issue porting existing VB6 code that uses GoSub and it may seem like an issue if you're used to using it and can't but, much like control arrays, if you're writing good VB.NET code to begin with then you will pretty much never find yourself in a position where they seem like a good idea.
-
Re: Button edges and font size
I agree.
I was thinking along the lines of saying that while it might make a quick code port quicker, it would be better to redesign, which is why I couched the response with the phrase "could be considered justifiable", not that it was definitely justifiable.
-
Re: Button edges and font size
Folks, you have almost convinced me to become a VB.NET proselyte. Thanks for your contributions.
Back to control arrays.
I have five function buttons on the top of my form next to each other where a user can select which function he wants.
When he clicks a function, I wish to make the backcolor of that control yellow and make the backcolor of all the other four controls blue - one of these four may still be yellow from a previous selection.
This is the code I am using, but it does not work:
For i = 1 To 5
If DirectCast(sender, Button).Name = "MF" & CStr(i) Then
DirectCast(sender, Button).BackColor = Color.FromArgb(255, 227, 161)
Else
DirectCast(sender, Button).BackColor = Color.FromArgb(0, 0, 120)
End If
Next i
I have set the .UseVisualStyleBackColor = False for all controls.
And as a bonus please tell me why I have to cast it and why I cannot just say : sende.backcolor=yellow
Thanks
-
Re: Button edges and font size
As has been said, indexes are irrelevant here. What matters is which Button was clicked. E.g.
vb.net Code:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button5.Click,
Button4.Click,
Button3.Click,
Button2.Click,
Button1.Click
Dim buttons = Me.Controls.OfType(Of Button)()
Dim color1 = Color.FromArgb(255, 227, 161)
Dim color2 = Color.FromArgb(0, 0, 120)
For Each button In buttons
button.BackColor = If(button Is sender, color1, color2)
Next
End Sub
That specific code relies on the form, or whichever container's Controls collection you use, containing no other Buttons than the ones involved here. If that's not the case, just create the list of Buttons explicitly:
vb.net Code:
Dim buttons = {Button1, Button2, Button3, Button4, Button5}
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
And as a bonus please tell me why I have to cast it and why I cannot just say : sende.backcolor=yellow
That's because the 'sender' parameter is type Object and BackColor is not a member of the Object. With Option Strict Off, you could still do it thanks to late-binding but you should always have Option Strict On except in the specific cases where you need late-binding.
Note that BackColor is actually a member of the Control class so you could actually have cast as Control to access BackColor.
-
Re: Button edges and font size
Thanks jmcilhinney,
I will try that although I fail to understand the difference between casting it as control or casting it as button, because a button is a control as I understand - and both are objects.
I think I have the same problem with the graphics on the button.
I have made a picture box and loaded a picture (call it picBlue) into it. When I specify the .image=picblue, it tells me that I cannot use it as it must be an image. I do not think I understand the difference between picture and image. I have also made an imagelist and specified one if its images as the button background. Is the first or the last correct?
Thanks
PK
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
Thanks jmcilhinney,
I will try that although I fail to understand the difference between casting it as control or casting it as button, because a button is a control as I understand - and both are objects.
There is no difference between casting as type Control or as type Button. That's what I said. BackColor is a member of type Control so if you haver a reference of type Control or any type that inherits Control then you can access the BackColor property.
Yes, all controls are objects but not all objects are controls. Consider this. Let's say that I give you a box that you know contains an animal. What can you tell me about that animal's wings? Obviously you can't tell me anything because you don't even know whether it has wings. If I open the box and you see that it contains a parrot then you know that it has two wings. A parrot is a bird and birds have two wings.
It's the same situation in your code. Casting is for the compiler. If you have an Object reference then the compiler can't use a BackColor property because it doesn't know that the object has such a property. If, at run time, that reference happens to refer to a DataTable then obviously trying to access a BackColor property would fail. By casting as type Control or type Button, you are telling the compiler that the object will indeed have a BackColor property and it is safe to access it.
Of course, casting isn't magic. It's still up to you to write your code in such a way that you guarantee that the object will be a control. If your method only handles events of controls then you're guaranteed that the 'sender' will indeed be a control. If you handle an event of something that isn't a control though, a run time exception will be thrown because the cast will fail.
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
I think I have the same problem with the graphics on the button.
I have made a picture box and loaded a picture (call it picBlue) into it. When I specify the .image=picblue, it tells me that I cannot use it as it must be an image. I do not think I understand the difference between picture and image. I have also made an imagelist and specified one if its images as the button background. Is the first or the last correct?
Thanks
PK
There is no "picture". Do you understand the relationship between a String and a Label? One is an object that contains text and one is a control for displaying the contents of the other. It's basically the same with an Image and a PictureBox. The Image is the object in memory that contains the image data and the PictureBox is the control that displays the image to the user. When you set the Image property of a PictureBox, you must assign an Image object.
An ImageList is a type for storing lists of Images, generally for objects that need multiple images. That is most notably the ListView and TreeView controls. For those controls you must use an ImageList. I would suggest not using one if you don't have to because they have certain limitations. If you want to group multiple Images then, generally speaking, you should do it the same way you would for any other type, i.e. use an Image array or a List(Of Image).
-
Re: Button edges and font size
{p.s. jmc posted twice while I was composing, so I'm sure a lot of this is redundant.}
It is a matter of specificity.
An object is very broad, not specific. It can hold a reference to pretty much anything.
When you reference an object directly you only see the properties that belong to an object, that is things that are common to every object.
Not all objects have an .Image property so you won't see a .Image property when referencing an object.
A Control is a more specific Object. It would have properties that are common to all controls.
Not all controls have an .Image property, so you won't see an .Image property if you're referencing a Control object.
You will see a .Backcolor property because have a Backcolor is common to all Controls (but not all Objects).
A Button is a specific Control. You will see a property .Image because Buttons have a .Image property. You will also see a .Backcolor property because a Button is a Control.
If you create an Object (Dim anObject As Object) and type in its name followed by a ".", e.g "anObject." you should have intellisense showing you a list of properties and methods. It should be a short list because an Object is not specific and very few things apply to all objects.
If you create a Control and do the same thing, you should see a longer list because a Control is more specific and more things are applicable to all controls.
Lastly create a button and now intellisense should show you an even longer list of things that are applicable to a Button.
As far as your question about graphics on the button I'm not sure I quite understand.
But I think you're saying you've assigned an image/bitmap to a picturebox, and now are trying to assign the image from the picturebox to the image of the button.
But your code is assigning the picturebox control to the Image, not the Image you have loaded in the Picturebox.
If that is the case, then you want to assign the image in the picturebox to the image of the button.
myButton.Image = picBlue.Image
Be aware, since the .Image is also an object (a reference type), you have now set the button's Image to the same Image as picBlue.
That is, they both reference the same image, the button is not a copy of the image. If you were to draw on the image of the picturebox, you would see that drawing show up on the button as well.
If you wanted the button to have a copy of the image that was in the picturebox, one way you could do it:
myButton.Image = picBlue.Image.Clone
Another way would be to create a New Bitmap object, assign it to the button, and then draw the image (picBlue.Image) onto the Button's image.
Code:
myButton.Image = New Bitmap(myButton.ClientSize.Width, myButton.ClientSize.Height)
Using g As Graphics = Graphics.FromImage(myButton.Image)
g.DrawImage(picBlue.Image, 0, 0)
End Using
There are over 20 variations of DrawImage I believe, and the one above won't scale the image so you may see only a part of the picture.
I think assigning a picture (either directly or using the .Clone method) to a button will scale the image by default.
Also, be aware that if what you've done is set the backcolor of the picturebox to Blue, and then expect to assign that blue color as an image to the image of the button that won't work.
The backcolor of a picturebox is the backcolor of the picturebox, it is not part of the image.
If you haven't assigned something to the .Image property of the picturebox, then it is still nothing regardless of what you set the backcolor to.
If you wanted the button's backcolor to be blue, then either assign it directly
myButton.Backcolor = Colors.Blue
or you could assign the color to another color that is assigned elsewhere.
myButton.Backcolor = picBlue.Backcolor
In this case, Backcolor is not a Class, it is a Structure so is not a reference type.
myButton's Backcolor and picBlue's Backcolor do not reference an object. They each have a copy of the Color Structure, a value type, so if you change the backcolor of picBlue it will not affect the backcolor of the Button.
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
...
I think I have the same problem with the graphics on the button.
I have made a picture box and loaded a picture (call it picBlue) into it. When I specify the .image=picblue, it tells me that I cannot use it as it must be an image.
I believe that you may be having an issue due to the Default Property syntax differences between VB6 and VB.Net. I believe the default property on a VB6 PictureBox is its Picture property and the usage you attempted would have been valid in VB6.
In VB.Net, the Default Property usage is restricted to usage for an indexed value (similar to an array index).
vb.net Code:
Dim lst As New List(Of Int32)
lst.Add(1)
Dim val As Int32
val = lst(0) ' retrieves the default property Item
' and is equivalent to :
val = lst.Item(0)
See the documentation for a full description.
Default Properties
-
Re: Button edges and font size
I thank all three of the last contributors for excellent advice. I beginning to understand that and will experiment.
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
Folks, you have almost convinced me to become a VB.NET proselyte. Thanks for your contributions.
Back to control arrays.
I have five function buttons on the top of my form next to each other where a user can select which function he wants.
When he clicks a function, I wish to make the backcolor of that control yellow and make the backcolor of all the other four controls blue - one of these four may still be yellow from a previous selection.
This is the code I am using, but it does not work:
For i = 1 To 5
If DirectCast(sender, Button).Name = "MF" & CStr(i) Then
DirectCast(sender, Button).BackColor = Color.FromArgb(255, 227, 161)
Else
DirectCast(sender, Button).BackColor = Color.FromArgb(0, 0, 120)
End If
Next i
I have set the .UseVisualStyleBackColor = False for all controls.
And as a bonus please tell me why I have to cast it and why I cannot just say : sende.backcolor=yellow
Thanks
I know below this post of yours is a conversation about how to access the Button's properties vs a control's properties and what an Object is, but might I suggest since the user needs to select a function you could suggest using a RadioButton (if only 1 function can be selected at a time) or series of CheckBoxes (if multiple functions can be selected at a time) control for this with their Appearance property set to Button (so it looks like a button). You can use their Changed event to set the backcolor of the control to Blue/Yellow depending on whether it's selected or not.
I could provide a code example if you'd like.
-
1 Attachment(s)
Re: Button edges and font size
To all who participated - thank you. As a resoltion to the questions and thread I am publishing here my solution to this question. It basically does what VB6 can do easily with control arrays and it does it just as simple and directly.
I attach a screenhot for elucidation:
Here is my code:
Creating the button arrays for the top and lower row:
Code:
ReadControlsDatabase()
For Counter As Integer = 1 To 5
btnFn(Counter) = New Windows.Forms.Button
With btnFn(Counter)
.Size = New Drawing.Size(178, 60)
.Location = New Drawing.Point(8 + (Counter - 1) * 185, 8)
.Text = FunctionName(Counter)
.Name = "MF" & CStr(Counter)
.ForeColor = Color.FromArgb(0, 0, 120)
'.FlatStyle = FlatStyle.Flat
'.FlatAppearance.BorderSize = 0
.UseVisualStyleBackColor = False
.BackColor = Color.FromArgb(140, 175, 255)
.Tag = Counter
Dim MyFont As New Font("Verdana", 12)
.Font = MyFont
.Visible = True
End With
AddHandler btnFn(Counter).Click, AddressOf Me.btnFn_Click
Me.Controls.Add(btnFn(Counter))
MainFunctions = MainFunctions + 1
Next Counter
For Counter As Integer = 1 To 22
btnSFn(Counter) = New Windows.Forms.Button
With btnSFn(Counter)
.Size = New Drawing.Size(178, 58)
.Location = New Drawing.Point(8 + (Pos(Counter) - 1) * 185, 75)
.Text = "F" & CStr(Counter) & " - " & SubFunctionName(Counter)
.Name = "SF" & CStr(Counter)
'.BackColor = Color.FromArgb(255, 227, 161)
.FlatStyle = FlatStyle.Flat
.FlatAppearance.BorderSize = 0
'.ForeColor = Color.FromArgb(0, 0, 120)
.Tag = Counter
'.Font.Size = 9
.Visible = False
'Dim PicBlue As New Windows.Forms.PictureBox
.Image = picBlue.Image
End With
AddHandler btnSFn(Counter).Click, AddressOf Me.btnSFn_Click
Me.Controls.Add(btnSFn(Counter))
'btnSFn(Counter).ForeColor = Color.FromArgb(0, 0, 120)
SubFunctions = SubFunctions + 1
Next Counter
Here are the events to change the properties of the buttons when they are clicked:
Sub btnFn_Click(ByVal sender As Object, ByVal e As EventArgs)
For i = 1 To MainFunctions
If sender Is btnFn(i) Then
btnFn(i).BackColor = Color.FromArgb(255, 227, 161)
btnFn(i).ForeColor = Color.FromArgb(0, 0, 120)
MainFunction = i
Else
btnFn(i).BackColor = Color.FromArgb(140, 175, 255)
btnFn(i).ForeColor = Color.FromArgb(0, 0, 120)
End If
Next i
For i = 1 To SubFunctions
If SFmain(i) = MainFunction Then btnSFn(i).Visible = True Else btnSFn(i).Visible = False
Next i
SubFunction = 0
Refresh()
Exit Sub
End Sub
Sub btnSFn_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer
For i = 1 To SubFunctions
If sender Is btnSFn(i) Then
btnSFn(i).Image = picYellow.Image
btnSFn(i).ForeColor = Color.FromArgb(0, 0, 120)
SubFunction = i
Else
btnSFn(i).Image = picBlue.Image
btnSFn(i).ForeColor = Color.FromArgb(0, 0, 120)
End If
Next i
Refresh()
ShowGridView()
Exit Sub
End Sub
Attachment 143439
-
Re: Button edges and font size
Peekay,
I'm glad to see that you are making progress. :)
One thing that I wish to bring up is the handling of Disposable objects. These are objects that internally allocate native resources (memory, file handles, gdi handles, etc.) that should be released as soon as you are done with the object. This is accomplished by calling the Dispose method on the object.
This may be an issue where you are assigning the image in you btnSFn_Click method. I say may, because I do not know if picYellow.Image and picBlue.Image are being changed or are used as static values. I tend to use the following when swapping out images.
Code:
Public Shared Sub SwapPBImage(pb As PictureBox, newImage As Image)
Dim oldimage As Image = pb.Image
pb.Image = newImage
If oldimage IsNot Nothing Then oldimage.Dispose()
End Sub
You do not want to call Dispose on an object until you are certain that none of your code that references it is done with it though.
Also, your code is creating a new Font object for each button.
Code:
Dim MyFont As New Font("Verdana", 12)
You will you less resources if you create this as a shared resource and assign that single instance to each created Button. Just declare MyFont at the class level instead of in the method loop.
-
Re: Button edges and font size
Thanks TnTinMN,
It is the same same jpg that I swop to make the one which is current, yellow.
Thanks for your font comment - I see the point.
-
Re: Button edges and font size
If you put your code between [code][/code] tags it will be easier to read.
Code:
Private Sub Label4_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label4.MouseMove
Static lpos As Point
Static locOpacity As Single = 1.0
If e.Button = Windows.Forms.MouseButtons.Left Then
locOpacity += CSng((e.X - lpos.X) / Label4.ClientSize.Width)
If locOpacity < 0.08F Then locOpacity = 0.08F
If locOpacity > 0.99F Then locOpacity = 0.99F
Label4.Text = String.Format("Opacity {0}%", CInt(locOpacity * 100))
Opacity = locOpacity
End If
lpos = e.Location
End Sub
You can just hit the last button above the reply area (the one with the #) to insert the tags, or select the code and hit the button to insert the tags around the selected code.
I usually just type them in myself, as I've been doing that a long time before I knew about the # button.
Also, the button next to it, "VB", which looks like "VE" in a lot of browsers, provides a different style of code windows with line numbers and colored syntax.
Here I input "vb.net" when asked for the highLight option after pressing the "VB" button.
vb.net Code:
Private Sub Label4_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label4.MouseMove
Static lpos As Point
Static locOpacity As Single = 1.0
If e.Button = Windows.Forms.MouseButtons.Left Then
locOpacity += CSng((e.X - lpos.X) / Label4.ClientSize.Width)
If locOpacity < 0.08F Then locOpacity = 0.08F
If locOpacity > 0.99F Then locOpacity = 0.99F
Label4.Text = String.Format("Opacity {0}%", CInt(locOpacity * 100))
Opacity = locOpacity
End If
lpos = e.Location
End Sub
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
become a VB.NET proselyte.
That's an awesome word, and so totally fitting.
VB.NET vs VB6 is pretty much a holy war. In fact, like most holy wars, the VAST majority don't really care that much about one position or the other, but for the ones who do....hoo boy, Watch out!!!
I used VB6 for a few years before moving to .NET. I loved VB6, but would never willingly go back. The changes in the IDE are such that the old VB6 IDE feels really awkward. That's just because of improved understanding in how people interact with IDEs, though.
I was surprised that you liked the MSFlexGrid. I always felt that it was largely used because it was all that was readily available without going third party. I never thought it all that good, and felt that the DataGridView was a considerable improvement. There ARE a lot of options, though, and options aren't always helpful.
On the other hand, DAO was seriously FAST! It was much faster than ADO, though even by the dawn of .NET, ADO had largely supplanted DAO as the DB technology to be using in VB6....according to the literature I was reading. That made me hesitant to go to ADO.NET, but ADO.NET is such a different animal from ADO or DAO that I'm not sure whether a straight up comparison of speed between the two is even possible. ADO.NET was based around the idea that you'd go to the DB, get your data back into an in-memory data structure, then disconnect from the DB and not go back to it until you needed to either get new data, or push changes back to it.
-
Re: Button edges and font size
Shaggy, at last someone who understands that I are here trying to join another religion. Some people get killed for that!
I think my main concern is editing in a grid. I still do not know how to edit a datagridview, but the MSHFlexgrid was ideal, as you could edit any field anytime anywhere, just by placing another control over the relevant cell - and you could show or not show columns anytime anywhere. Maybe Datagridview can do that - and that is this proselytes next ordeal.
And then - why do I want to disconnect from a database driven by an awesome DAO in a limited user environment?
-
Re: Button edges and font size
Quote:
Originally Posted by
Peekay
The following are serious shortcomings in VB.NET:
...
2. The MSFlexgrid control is powerful and versatile and editing data in it is a breeze.
Quote:
Originally Posted by
Peekay
Maybe Datagridview can do that
Can you see the issue here? You just said that there's something wrong with VB.NET because it can't do what you want but you don't actually know whether it can do what you want or not. Exactly how does moaning about VB.NET not being VB6 help you to learn VB.NET? How does assuming that VB.NET can't do certain things help?
-
Re: Button edges and font size
Hi jmcilhinney,
I think it is quite appropriate to explore VB.NET and exploring the DatagridView. I have not said it cannot do those things that MSHFlexgrid can do - I give it a try.
Kind regards