Do PictureBox controls have another attribute other than .Name for identification - c#

I couldn't find anything here on StackOverflow that answered my question. I hope I make it clear here.
I've created a windows form application which is the game Yahtzee. I'm doing it for fun and I'm almost finished. What I'm trying to accomplish is the following.
When the user wants to save a score for a particular category, they just click on the picturebox that is associated with their category of choice. Some validation and equations are executed, which is followed by a display() method. This method logs to the text area of the GUI how many points they just scored.
I am trying to make the logging more easy to read and more meaningful. Here is the before and after that I'm trying to accomplish.
current code
...AppendText("\nscored: " + NewYahtzee.Rollscore + " points for " + ((PictureBox)sender).Name)
current output
scored 50 points for pictureBoxYahtzee
Output I want
Scored 50 points for your Yahtzee
So I'm trying to replace the name of the control with a friendly/display name. Is there such an attribute or way of doing this?
Thanks!

As both the commenters above said.
1. You could use the .Tag property of the PictureBox.
When you want to assign some information to the PictureBox to hold just do
String info = "MyInfo";
pictureBox1.Tag = (object)info;
The fun thing is, with .Tag being a object you can box anything into the .Tag property. So you could create a custom class filled with info and assign it to the .Tag
To retrieve the info you need to make sure that the .Tag value is of the same type you assigned it, so following the above example
if(pictureBox1.Tag is String)
{
String info = (String)pictureBox1.Tag;
}
2. The other option you have is to extend the PictureBox class and add public properties to the class.
class PictureBoxEx : PictureBox
{
public String info { get; set; }
public PictureBoxEx()
{
}
}
Then find the definition of your PictureBox object as it is now and just change its type from PictureBox to PictureBoxEx and everything should be the same, except now your PictureBoxEx reference has the public field info.

1)You could make your own derived class like stated above.
2)You could change the name of the control temporarily.
3)You could store it somewhere else(settings perhaps).
I would just add the property to the pictureBox.

Related

Assign from object to a class instance

I have a Class Dogs, and one of its properties is called MyPictureBox (type of PictureBox). I also have four picture boxes on the windows form.
I have this piece of code assigning corresponding pictureBoxes to the instance of each Dog. And this works just fine.
Dogs[0].MyPictureBox = pictureBoxDog1;
Dogs[1].MyPictureBox = pictureBoxDog2;
Dogs[2].MyPictureBox = pictureBoxDog3;
Dogs[3].MyPictureBox = pictureBoxDog4;
I spent some time trying to cycle through Dogs and assign pictureBox using its name (as the only difference in picture box name is the ending 1,2,3,4) with no luck.
Any ideas are greatly appreciated (hate to move ahead without solving this first).
From what I can get from your question, I think your dog should have a method like showOnPictureBox(PictureBox p) and maybe a property that holds the PictureBox that the dog is currently "in".

C# Custom Control Properties

I am building some applications that use OPC to control some industrial automation. I have decided it might be a good idea to create some custom controls for standard things I will use such as buttons and text boxes. One of my main reasons was that I felt in an ideal pattern the end object i.e. the button is the one who holds information about the OPC item it refers to otherwise in a typical application my Form class gets polluted by tons of variables that are in my mind scoped more global then they should. I started then by using the Tag property of the button but this requires some overhead code that is the same for each instance. I felt like the right thing to do is subclass controls that I would like to use and provide properties to configure each one.
With that context in mind my real question is this. To make it as portable as possible I decided there should a property to define the OPC Group. I declared a property like this
public class OPCButton : Button
{
[Category("OPC")]
public OPCConnectedGroup
{
get { return _OPCGroup; }
set { _OPCGroup = value; }
}
}
This shows up int the property list when I add the control but I am unable to bind this property even though my Form1 contains
public OPCConnectedGroup Connection1 = new OPCConnectedGroup();
I have resolved that the way to solve this is to probably create an additional control like OPCGrp that can be added to a Form then the Controls can reference this. To test I added a property of type Button and sure enough when I added it and browsed to the property it gave me options for all the buttons on the Form. I have no huge problems with this approach I just want to make sure that Im following a prototypical pattern because I will be responsible for maintaining the control library but not always implementing and Im trying to get it down to a 1-2 step process to implement a control.
Thanks
Matt
When defining OPCConnectedGroup you can inherit Component class.
This way if you have a property of type OPCConnectedGroup in your OPCButton, then at design time, you can put instances of OPCConnectedGroup on the form, and then if you choose your OPCButton at designer, that property of type OPCConnectedGroup will show as a drop down list that you can select one of instances that you put on the form for it.
Example:
If I have such MyButton and MyClass:
public class MyButton : Button
{
public MyClass MyClassInstance { get; set; }
}
public class MyClass : Component
{
public string SomeProperty {get;set;}
}
Then you can put some (or one) instance of MyClass on the component tray of the form:
And then if you select MyButton on your form, you can choose one of MyClass instances from in property grid:

Want to take data from one form to use them as variable in an other

I'm almost done doing a Connect4 game with VS2012 using WinForms. Everything is working well but I wanted to bring the options for the user on a dedicated Start Menu window. On that menu I have two comboBoxes I need to take the text from to use them as a value for two variables in my other form (the game window). I also have one New Game button that should call a method from my other form if that's possible (basically, I made an "Initialization()" method in my game form and I'd like it to be launched when I click the "New Game" button on the other form).
I only found tutorials that show how to do very basic things from one form to an other (such as labeling texts) but I I didn't find an answer to my specific problem.
I used this in my main form to instantiate the menu form
public FormMenu myMenu;
myMenu = new FormMenu();
What I want to do is that I could do something like this in the other form :
amountOfRows = Int32.Parse(myMenu.comboBoxRows.Text);
amountOfColumns = Int32.Parse(myMenu.comboBoxColumns.Text);
Any idea how I could do that?
I would love to see some example code so I can help see where your confusion lies. WinForms requires the other form to be instantiated.
OtherForm form = new OtherForm();
Once the form is instantiated you should be able to run code from it.
EDIT:
Based on your implementation I would suggest making public methods within FormMenu that return these int values.
public int ReturnRows()
{
return Int32.Parse(myMenu.comboBoxRows.Text);
}
public int ReturnColumns()
{
return Int32.Parse(myMenu.comboBoxColumns.Text);
}
Then from the other form in which myMenu is instantiated you can call myMenu.ReturnRows() and myMenu.ReturnColumns()
The easiest way would be to store a reference to your form in the menue as a variable. (you already named it myMenu)
Then you should create the property/properties you need in the form an add a setter for the values. (see example here)
Last you update the form fields with
myMenu.property = newvalue;
That`s all about it

Retrieve activity name

I'm working in a WPF project and I need one requirement.
This is my main window:
http://img210.imageshack.us/img210/9752/rule.jpg
So, I want that when I drag and drop a custom activity (aggregate Functions), display his name in Compute TextBox... Is there some way to do it????
For example:
1) Drag and drop MaxActivity
2) Display in TextBox Compute: MaxActivity
3) Drag and drop SumActivity
4) Display in TextBox Compute: MaxActivity + SumActivity
5) Drag and Drop MaxActivity
6) Display in TextBox Compute: MaxActivity + SumActivity + MaxActivity1
Thanks a lot!
I would strongly suggest you avoid this design, as however you end up implementing it will introduce brittleness to your code without any benefit (I can already see you are aggregating max and sums, why tell me again?)
If you go ahead, get rid of that "Compute" TextBox. TextBoxes = enter text. You are just rephrasing what is already in the design surface. It makes no sense to allow me to change the text you generate. Use a TextBlock or Label.
Do not, I repeat, not save this in your Activity. This is all UI tasks and should not be saving this information in your Activity configuration
If you have this
public sealed class MyActivity : Activity
{
public string Compute {get;set;} // NO!
and this
<!-- NO! -->
<Label>Compute</Label><TextBox Text="{Binding ModelItem.Compute}"/>
you're probably doing this wrong.
So, how do you do it?
You can parse out your ModelItem and generate this string by listening to changes in the ModelItem and constructing this string every time. Note, your ModelItem will not be set when the constructor is called, so you are going to have to listen to changes to the ActivityDesigner.ModelItem property. There is no event for this, so you will have to know how to listen to changes in a DependencyProperty.
Once you are listening to changes in your ModelItem, whenever a change happens, you can walk down your ModelItem (which is hard) or just get the Activity from the ModelItem and examine it.
var child = ModelItem.Properties["Child"].Value.GetCurrentValue();
if(child == null)
computeTextBox.Text = "Please add some computation stuff.";
else
// hard stuff goes here...
Walking down the path from your Activity's child to whatever activities are held inside can be treacherous, as you can't assume your user has thrown in a Sequence rather than a single MaxActivity, or that they don't have seven nested Sequences, etc etc. So you have to have a pretty complex and brittle set of if else logic to try and parse out what is contained below. And if you start changing properties of the Activity tree outside of the ModelItem you're going to get out of sync with the UI.
Now, after reading that, go back to point 1. I listed in this answer and follow my advice. Drop the idea completely.

C# saving state of the form with all controls

I have a form and I have some buttons doing stuff.
When I press buttons the windows form controls, like textboxes or group-boxes, buttons appear and disappear and change place on my form, for it is a dynamic form :)
However, what I'd like to do is have a button ( BACK ) that will get my form to the state it was before an action of a button, putting back the controls in the place and state they were before action.
I thought of a C class MyState() that will have something like an array of Form1.
I will be saving the form state in that array and when I'll press the back button to get from array that "copy" of the Form state and maybe an index for indexing states.
I have no idea how to implement this, unfortunately. :|
Can anyone show me the right way to do this?
class Mystate
{
private Form1 [] state;
public Mystate(int n)
{
this.state = new Form1[n];
}
public Form1 this[int index]
{
get
{
return state[index];
}
set
{
this.state[index] = value;
}
}
}
Sounds like you want an high level undo/redo feature for your forms.
Here is a framework for such things: http://www.codeproject.com/Articles/10576/An-Undo-Redo-Buffer-Framework
Here is an answer that is close but not exactly the same as your question (The pattern implimented is the same though): How to implement good and efficient undo/redo functionality for a TextBox
MementoPattern: http://www.codeproject.com/Articles/18025/Generic-Memento-Pattern-for-Undo-Redo-in-C
Nothing like this is built-in. You have to do this on your own.
I'd do it like this: First, define precisely what state you want to save. Example:
Control.Bounds
Control.Text
Checkbox.IsChecked
NumericUpDown.Value
...
Now we know exactly what needs to be saved.
Seconds, we need a way to create a snapshot of the current state of the form and recursively for all controls. You can implement this using reflection so that everything will be automatic no matter how many controls you have.
Third, you need to be able to apply a snapshot to an instance of Form. This is the opposite process of (2). This also can be done using reflection.

Categories

Resources