Assign from object to a class instance - c#

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".

Related

Reference object in main window from another script? (VS, C# and WPF)

Firstly my apologies for my stupid question. I am very new to this.
I have created my first ever windows application, yay, but now I am stuck again and google doesn't seem to be any help and smothering me with Unity based forums, which I followed the solutions to but doesn't seem to be helping me.
Ok so my issue is that I need to reference objects that are in the main window of Visual Studio from another script. Basically, as a start, I want a selection in the task bar menu to know if a tickbox (WPF: CheckBoxName.IsChecked) is true from my checkbox script.
Clearly I have to reference the script in my taskbar script, but how? My main window objects were dragged in from the asset selection and stored in MainWindow.XAML.
(I'm using VS22, or Blend or whatever you crazy kids are calling it now days)
The answer shouldn't be too complicated, I'm just new and dumb. Any help will be gladly appreciated :)
if i get this correctly:
You want to access a variable / an object that is stored in another form/class?
for the variables make them public:
string myName = "Faylasouf";
public string myName = "Faylasouf"
for an object:
a. click the object, then click properties, change the modifiers property to "Public".
Now you can go to the other form/class (the one will access the variable/object):
Class/Form_Name myClass/myForm = new myClass/myForm();
ex:
MainForm mainForm = new MainForm();
then you can use:
mainForm.ObjectName; or mainForm.VariableName;

How to select multiple elements in a Revit drawing

I'm trying to create a "Multi-Select" method in Revit (for 2016/2017) where the users can select specific parameters of instances contained within a drawing (like Nominal Diameter, Pipe Type and so on), and it will select all the instances within the drawing based on their selections. First, screen shot:
Let's predicate this with the fact that this window is created dynamically based on contents of the drawing. Nothing gets put in this window unless there is an element in the drawing that contains/meets one or more of these parameters.
So, ideally, when I Click the DO IT! button, I would like it to select all of the elements in the drawing that have meet any of these parameters. I can filter through this window and find all of my selections - now I just don't know what do with the selections.
I've looked through the Revit.chm and source and found the Selection namespace and class. There are functions like:
PickObject(ObjectType objectType);
that seem like they would be what I want, but I don't know if it's REALLY what I need. Furthermore, if that IS, in fact, what I'm looking for, I don't know the syntax of how to use it.
A little code:
I have a method that collects all of the users' selections:
private List<CheckBox> GetUserFilterPrefs()
{
//CYCLES THROUGH ALL THE PANELS AND BOXES IN THE WINDOW
return lstCheckBox;
}
Now I want to create my EventHandler for btnDoIt_Click...
I started it, but I'm walking in the dark on this part.
private void btnDoIt_Click(object sender, RoutedEventArgs e)
{
int itr = 0;
GetUserFilterPrefs();
List<Reference> lstRefs = new List<Reference>();
foreach (CheckBox cb in lstCheckBox)
{
if (lstElts[itr].Name == cb.Name)
{
//HOW DO I SELECT ALL ITEMS LIKE THE GIVEN ELEMENT
//THAT ARE RELATED TO THE CHECKBOX SELECTION??
}
itr +=1;
}
I'll obviously keep looking around; but if anyone knows a way, or can point me in the right direction, it would be massively helpful!
THANKS!!!
The PickObject function that you've found is one that asks the user to select an object in the model. Based on your description, that is not what you are looking for.
The function you need is:
SetElementIds(ICollection<ElementId> elementIds)
It is also part of the Selection class. This will highlight the desired elements in the model. To clear the selection in the model, pass an empty List as your argument. Passing null will cause an exception to be thrown.
To focus on the elements, you need the function:
UIDocument.ShowElements
There are a number of overloads for this function. Note that none if the elements are in the currently open view, Revit will attempt to find the best view for you, a task that it generally performs very poorly if there are many views in the model.
PickElement prompts the user for interactive element selection, which is not what you are after.
The one and only way to programmatically access elements in the Revit database is to use a filtered element collector:
http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.9

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

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.

databinding in c# acting oddly

I have a question regarding my code:
I have a list of player "private List player = new List();" and Player is a class that I mad myself, I also created an Usercontrol and all variable of player are databinded.
And it's working perfectly fine in my main form, but I have a strange issue when I change form.
I give the player list as a parameter to another form and here is the code:
public Result(List<Player> player, string format)
{
InitializeComponent();
_player = player;
ExtentionHelpers.Shuffle<Player>(_player);
}
Shuffle as the name said, just shuffle the list, but it should shuffle _player which is a local variable of my second form, I never use player in this form.
But for some strange reason, my list player from my main form end up being shuffled too, and I don't want that, specially when that mess up my databinding.
What's happening here ??
Your player variable is a reference type, so it is the same data you are working on in both forms. You'll need to clone your player list if you want to manipulate it separately.
When calling your constructor, try this:
var frm = new Result(new List<Player>(player), "some format");
Creating a new list means that you can manipulate the new list independently from the first BUT if you change data within the Player type, it will affect data from the other form (you'll need to clone each item separately before adding to a new list if you want this).

Display final score from windows 8 game c#

I created a game in c# for windows 8 and everything works like it is supposed to but after the user plays the game the score is displayed in a text box and it is supposed to also display on the following screen displaying the score and text and thumbs up images depending on how high it is.
The problem is the score isn't being carried over. The code I use gets the constructor from the game page then the score, the code is below:
Var arcade = new arcadeMode();
ScoreNum.text = arcade.score.toString();
//then sample code to display images based on score
if(arcade.score < 100)
{
Usermessage.text = "try again.";
Thumbdown.visibility = visibility.visible;
}
I think my problem is actually getting the saved score from the game because it must only be getting the textbox value from before the game started. Not really sure how to save the score without a database though. Any suggestions on how to go about this?
It looks like ArcadeMode is essentially your data model, but gets the constructor from the game page doesn't really have meaning. Assuming you have an instance of ArcadeMode on your game page and it includes the actual score, with the code above, you've just created a brand new second instance of that same class with the score initialized to 0.
One quick way, since it appears it's just a simple piece of data you want to pass, is use the second parameter of Navigate. Wherever it is you navigate to the thumbs-up page, add the current value of your 'score' variable as the second parameter, for example:
Frame.Navigate(typeof(ThumbsUpPage), score)
Then in the OnNavigatedTo event of ThumbsUpPage you can access the value you passed via the Parameter property of the argument, for example:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
String score = e.Parameter;
...
Another option is to give the ArcadeMode class instance a larger scope (i.e., not make it a variable on the main page). You could make it a member of the App class, for instance, and then it would be available for all the pages in your app.

Categories

Resources