Jump to content
• Advertisement

[.net] Buncha C# Questions

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

I figured Id make one post with all my questions, instead of multiple posts. 1. How can I modify a control element (button, textbox, label) which is placed on my main windows form from a user control? 2. Is it possible for me to hide specific tab pages until certain conditions are met or do I need to delete and readd each time? 3. Is it possible to prevent the user from moving to a new tab page when certain conditions are met? An example of this would be to make the player stick out the battle and not have the option to exit the game, or move to a different tab page. 4. How can I add some form of opacity to specific images within a graphic/bmp? Can I add opacity to the image before it gets drawn onto the graphic? 5. When using a message box, with a yes or no button can I change the test of the yes or no to something different (accept, decline). 6. How would I go about having two classes on a user control and displaying both. The need for this arises with my explore system. One part retrieves the map from the database and stores it in a 2d array. The second part displays the map from this array. When the user presses a key or button to move his character, I only want the second part to be refreshed, but when the user transitions to a new map, I want both to be refreshed.

Share this post

Share on other sites
Advertisement
Just at a glance
1. Modify How? It probably comes down to getting the info needed from the user and then changing members of the control object instance to reflect the request
2. Controls can be hidden, so check that the conditions are met and then flag the control as visible when this happens, if any of the conditions change again, then hide
3. Similar to 2.
4. Depends on what libraries youre using, need more info
5. Im assuming you meant "text" and not "test" here, if so, sure why not?
6. Not sure what you mean here, but could you not just build the two maps together when you see fit and place them combined on this one control once...?

Share this post

Share on other sites
1. You should avoid writing any code that creates a circular dependency between a Form and a UserControl. A good way to do this is to create an event in the UserControl which is raised when you want the information changed, and then have the Form attach a handler to that event that makes the appropriate modifications.
2. Don't the tabs have a Visible property?
4. You will probably need to do your drawing manually to get transparency (which you are probably doing anyway to draw your map). If your images are png files with transparency then the transparency will be used automatically. If you're using System.Drawing, yes you can add transparency by drawing with an ImageAttributes object with a color matrix that you've set. Here's an example I used in a project of my own:

ImageAttributes imageAttributes = new ImageAttributes(); ColorMatrix colorMatrix = new ColorMatrix(new float[][]{    new float[] { Color.R / 255.0f, 0.0f, 0.0f, 0.0f, 0.0f },    new float[] { 0.0f, Color.G / 255.0f, 0.0f, 0.0f, 0.0f },    new float[] { 0.0f, 0.0f, Color.B / 255.0f, 0.0f, 0.0f },    new float[] { 0.0f, 0.0f, 0.0f, (float)Alpha, 0.0f },    new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f} });imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(mImage, (Rectangle)DestRect(0, 0),    SrcRect.X,    SrcRect.Y,    SrcRect.Width,    SrcRect.Height,    GraphicsUnit.Pixel,    imageAttributes);

I should warn you, overuse of that sort of thing likely will be slow.

6. I don't really understand your question, because "having" the classes is independent from what is displayed on the user control.

Share this post

Share on other sites
1. What I want to do is change the color of a button depending on the danger level of getting attacked. The values which represent the danger level are modified in a user control, while the button is displayed on the main form.
2. Unsure if they have a visible property, if so then I will use that, but in the properties view or wat not, visible is not found.
6. Its for my explore system. The map is part of a user control. In this user control I have two sections. One part queries the database and stores all the tile images and types in two 2d arrays. This is then used as long as the player remains in the same map area. The second part takes these two arrays and draws the map. When the user moves, I would like to only have the 2nd part reload, but when the player moves from one map to another, I want both parts to be reloaded. The problem I have here is I'm unsure how to segregate these within the same user control. Can I just make two classes?

Share this post

Share on other sites
1. Yeah, I would have something like a DangerLevel property and then a DangerLevelChanged event in your user control. Anytime the level of danger changes call the event and in the form have the event handler check the DangerLevel property and set the color of the button.
6. Yeah, you can have two classes or as many as you like, there is no practical limit.

Share this post

Share on other sites
When I said about the two classes, I meant in one user control. Is that allowed or even good practice? I don't want to separate the two bits of code, id like to keep them in the same file, but have some method of reloading only one chunk while leaving the other.

Share this post

Share on other sites
What exactly do you mean by having two classes in one user control? I can think of three different possibilities:

Do you mean you have references to the two classes like this:
public class MyControl : UserControl{    SomeClass a;    SomeOtherClass b;}

This is perfectly valid code, and just fine as far as programming practice is concerned. If this is what you meant, then go with it.

----

Or are you defining the classes inside the user control like this:
public class MyControl : UserControl{    public class SomeClass    {        // SomeClass members    }    public class SomeOtherClass    {        // SomeOtherClass members    }}

This is also valid code. But I don't recommend this approach, for several reasons. If you need to refer to the classes outside of the MyControl.cs file then you will have MyControl.SomeClass everywhere, which makes your code unnecessarily hard to read. When you nest classes like this it implies that SomeClass and SomeOtherClass are used only to describe the internal state of the MyControl class, and they should not be used very much outside the MyControl class. Especially if you are going to have other UserControls which depend on these classes, you create a very confusing dependency of OtherControl on a class defined inside MyControl, which implies the OtherControl is dependent on the MyControl class (even though it probably isn't). Not good programming practice.

----

Or are you just putting the classes inside the same file like this:
public class MyControl : UserControl{    // stuff}public class SomeClass{    // SomeClass members}public class SomeOtherClass{    // SomeOtherClass members}

Well, this is valid, although I think for the designer to work the class deriving from the UserControl must be listed first in the file. I don't recommend this approach, because classes deriving from UserControls tend to be rather large and it can easily "hide" the fact that other classes are defined in the same file. I'd say, if you want to keep SomeClass and SomeOtherClass in the same file, go ahead, but put them in a file separate from the UserControl.

Share this post

Share on other sites
I have no idea how I'm gonna set it up, kinda why I'm asking here. I'm not sure which would be the best way to do it. As i've explained before I need two classes, the first will query the db and put the current map into an array, while the second one will take this array and display a selected portion of it on the screen. I need to do it this way because on the tab page I have buttons which represent north, west, south, and east. When clicked if the player is not moving to a new map, then only the second class needs to be refreshed, while if the player is moving to a new map the first and second class need to be refreshed.

I have it setup like this so far:

public partial class explore: UserControl{
public explore() {
//code for converting db query to array
}
public void exploredisp() {
//code for displaying the array
}

Now its only processing the first class and not the second. I tried to make the second class public exploredisp() bt it said that "Class, struct, or interface method must have a return type". Im not too sure how to solve it.

[Edited by - Selacius on June 15, 2007 12:22:18 PM]

Share this post

Share on other sites
It sounds like you're confusing classes with methods here.
You have a class explore which derives from UserControl, which is defined by the line "public partial class explore : UserControl" Class definitions always have the work "class" in them.

public explore() after that is not a class, it's a method. In fact, it's the constructor for the class explore, so the code in it gets called when an explore object is created.

public void exploredisp() is another method. Since it doesn't have the same name as the enclosed class it's just a normal function. The compiler was complaining when you just did "public exploredisp()" because all methods except constructors need return types.

Oh, and to answer the question I think you're asking, a constructor can't be called again once the object is created. You would have to destroy the control and recreate it. But I have a better solution:

Add a class to your project (Right click on the project in the solution exploder -> Add -> Class) and name it something like MapData, and then put the code in a method in that class for converting the information in the database to an array.

Create a property in your UserControl for with the type of MapData (example here if you need one).

In your Form startup code (say, in the Load event handler), create a MapData object in the startup code for your form and set the UserControl's property created in the last step to the object you just created.

Then in your UserControl, add a handler to the Paint event and call your exploredisp function to display it.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5
• Advertisement

• 10
• 10
• 12
• 13
• 9
• Forum Statistics

• Total Topics
632200
• Total Posts
3004741

×

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!