i try to search for 2 similar images (like in a memory game)
i know how to do it, but it for sure is not the best way i guess...
anyone has a better solution?
here is the code how i have done it
private void first_button (object sender, System.Windows.Input.GestureEventArgs e) //so button get pressed
{
field_one = true;
StoryboardBack11.Begin();
StoryboardBack11.Completed += new EventHandler(AfterAnimation);
i = i + 1;
}
// same for the other buttons
private void AfterAnimation(object sender, EventArgs e) //check here after storyboard completed
{if (i == 2) // check after 2 clicks
{
i = 0;
if (field_one == true && field_two == true)
{
if (imageUri_one.Equals(imageUri_two))
{
Fade_one.Begin(); //Storyboard starts
Fade_two.Begin();
field_one = false;
field_two = false;
}
}
//and this for every possible combination
//-> here is my problem because it is a lot to copy paste and thus not optimal i guess
if (field_one == true && field_three == true)
{
if (imageUri_one.Equals(imageUri_three))
{
Fade_one.Begin();
Fade_three.Begin();
field_one = false;
field_three = false;
}
}
// here is the shuffeling of the images
private void test1(object sender, System.Windows.Input.GestureEventArgs e)
{
List<string> Test = new List<string>(); //list of the images
Test.Add("Art1.png");
Test.Add("Art1.png");
Test.Add("Art2.png");
Test.Add("Art2.png");
Test.Add("Art3.png");
Test.Add("Art3.png");
Test.Shuffle(); //they get randomly shuffled here and then realigned
imageUri_one = Test[0];
imageUri_two = Test[1];
imageUri_three = Test[2];
// ..... and so on
// put the new image Uri to a button -> the same 6 time for everey button
BitmapImage bm_one = new BitmapImage(new Uri(#imageUri_one, UriKind.RelativeOrAbsolute));
image11.Source = bm_one; //but it in xaml Source
}
so the thing is that i want to make the Uri flexible, so that i can shuffle different Uri when using another list and this should already work. but is there a more elegant way to check for similar images?
hope the question is clear :)
Pseudo-code:
public class MyElement
{
public bool IsPressed { get; set; }
public string Uri { get; set; }
... anything else you want to know about that one tile
}
then build the logical board
MyElement[,] tiles = new MyElement[8, 8];
for(int i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
tiles[i, j] = new MyElement { IsPressed = false; Uri = whatever; ... };
}
}
Now create the "physical" board with the buttons and assign a custom value for all buttons which contains it's coordinates.
Create one click handler where you check the tiles array for that coordinate and check it's MyElement object. Assign this click handler to all button click event.
Related
I´m using WindowsForm and working with flat design. In the program there are 6 buttons, these buttons are made of a label and a panel. The label controls all the actions that the button can do. when i started writing the program i made one function for each button, now i like to use one function that controls all buttons. I have tried to make that work but I´m stuck and can´t find a way to solve it.
Been looking around at the forum for solutions but i think that i might not know what i´m looking for.
This is what i made so far.
Buttons[] cobra = new Buttons[5];
private class Buttons
{
private bool position;
private string name;
public bool Position
{
get { return position; }
set { position = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
private void SetButtons()
{
cobra[0].Name = "label3";
cobra[0].Position = false;
cobra[1].Name = "label4";
cobra[1].Position = false;
}
private void CheckStatusButtons(object import)
{
for (int i = 0; i < cobra.Length; i++)
{
}
}
private class ToggelFunction
{
private bool hawk;
public bool Hawk
{
get { return hawk; }
set { hawk = value; }
}
}
ToggelFunction tiger = new ToggelFunction();
private void label3_Click(object sender, EventArgs e)
{
if (tiger.Hawk == false)
{
button1.BackColor = Color.PaleGreen;
label3.Text = "ON";
if (myport.IsOpen)
{
send(new byte[] { 16, 128, 32, 16, 1 });
}
tiger.Hawk = true;
return;
}
if (tiger.Hawk == true)
{
button1.BackColor = Color.DarkSeaGreen;
label3.Text = "2";
if (myport.IsOpen)
{
send(new byte[] { 16, 128, 32, 8, 1 });
}
tiger.Hawk = false;
return;
}
}
"label3_Click" this is my function for button 1, all buttons look the same just different variables.
As I found on the forum, you can use object sender to i identify which button that made the click and from there use that in the function to make action.
So all buttons will use this functions, i´m not sure how to compare values in the if statement, if button 1 is click then it should check what values button 1 has.
My idea was to make a class "Buttons" and an array to store all the values of each button, it´s not completed yet. When a button is clicked it checks
with the array what values that button has and compare that in the function depending on what the action is. The first action would be to check if the button is on or off. If it´s off then it enters that if statement and there some actions will happen, change of color and the text, these values also have to be stored in the array i guess.
I have tried to compare the array with object sender, but i get some error saying that you can´t compare bool with object i think.
So i wonder if some one might have a solution or suggestions?
I have made a simple example of what i think you want to do. bare in mind this does not abide by all coding best practises but its not a mess either. You will need to put your safety null checks in.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class ButtonVariables
{
public int value1 { get; set; }
public int value2 { get; set; }
}
Dictionary<string, ButtonVariables> bv = new Dictionary<string, ButtonVariables>();
private void ProcessClick(object sender, EventArgs e)
{
ButtonVariables vars = GetVariables(sender);
//Do stuff with your variable set here
}
private ButtonVariables GetVariables(object sender)
{
ButtonVariables returnValue = new ButtonVariables();
switch (((Button)sender).Name.ToLower())
{
case "buttona":
return bv["A"];
case "buttonb":
return bv["B"];
case "buttonc":
return bv["C"];
default:
break;
}
return null;
}
private void ButtonA_Click(object sender, EventArgs e)
{
ProcessClick(sender, e);
}
private void ButtonB_Click(object sender, EventArgs e)
{
ProcessClick(sender, e);
}
private void ButtonC_Click(object sender, EventArgs e)
{
ProcessClick(sender, e);
}
}
I've basically added two methods to handle your method. One to identify the button and get its related values from a dictionary that you will have to populate with your Buttons class. and one to carry out the logic.
EDIT
As requested in the comments here's an easy (but not the only way) to point your event listeners towards the same method.
Initially you need to set up with one button and double click it or do some other way to create the forms Button_Click() Event Method. At this point an event listener delegate has been added to your Form.Designer.cs File. Open that file and you will see something like this:
//
// ButtonA
//
this.ButtonA.Location = new System.Drawing.Point(12, 12);
this.ButtonA.Name = "ButtonA";
this.ButtonA.Size = new System.Drawing.Size(75, 23);
this.ButtonA.TabIndex = 0;
this.ButtonA.Text = "button1";
this.ButtonA.UseVisualStyleBackColor = true;
this.ButtonA.Click += new System.EventHandler(this.ButtonA_Click);
What you need to do is create your other buttons and add this code in for them with a change to the last line new System.EventHandler(this.ButtonA_Click); This line basically states which method to call when the ButtonA.Click event is invoked. At this point you can add what ever method you want (as long as you name is nicely for good convention). So your example would be this :
//
// ButtonA
//
this.ButtonA.Location = new System.Drawing.Point(12, 12);
this.ButtonA.Name = "ButtonA";
this.ButtonA.Size = new System.Drawing.Size(75, 23);
this.ButtonA.TabIndex = 0;
this.ButtonA.Text = "button1";
this.ButtonA.UseVisualStyleBackColor = true;
this.ButtonA.Click += new System.EventHandler(this.ProcessClick);
//
// ButtonB
//
this.ButtonB.Location = new System.Drawing.Point(12, 12);
this.ButtonB.Name = "ButtonB";
this.ButtonB.Size = new System.Drawing.Size(75, 23);
this.ButtonB.TabIndex = 0;
this.ButtonB.Text = "B";
this.ButtonB.UseVisualStyleBackColor = true;
this.ButtonB.Click += new System.EventHandler(this.ProcessClick);
//
// ButtonC
//
this.ButtonC.Location = new System.Drawing.Point(12, 12);
this.ButtonC.Name = "ButtonC";
this.ButtonC.Size = new System.Drawing.Size(75, 23);
this.ButtonC.TabIndex = 0;
this.ButtonC.Text = "C";
this.ButtonC.UseVisualStyleBackColor = true;
this.ButtonC.Click += new System.EventHandler(this.ProcessClick);
Remember you need to physically create the buttons on the form itself.
Lets say you have three labels and a panel for each label. You can add the event handler to all of them and whenever that event fires the event handler will use that label as the sender. To keep the panel associated with the label, you could add the panel to the label's tag property. Then, in the event handler you can then get the panel from the label.
label1.Click += label_Click;
label2.Click += label_Click;
label3.Click += label_Click;
label1.Tag = panel1;
label2.Tag = panel2;
label3.Tag = panel3;
In the event handler, just cast sender to Label and there you have your label object to do whatever you want with and like I said, the panel is in the Tag property. I did a little refactoring to your code to make it cleaner looking.
private void label_Click(object sender, EventArgs e)
{
// this is what itsme86 was suggesting in the comments
var label = (Label)sender;
var panel = (Panel)label.Tag;
label.BackColor = tiger.Hawk ? Color.DarkSeaGreen : Color.PaleGreen;
label.Text = tiger.Hawk ? "2" : "ON";
if (myport.IsOpen)
send(new byte[] { 16, 128, 32, 8, 1 });
tiger.Hawk = !tiger.Hawk;
}
Let me know if you have any questions about this.
So I'd really like to drag and drop data between two instances of an application; however, if there is data present at the target point where I am dropping, I would like to swap that data with what is being dropped.
I'm trying to use a MemoryMappedFIle, and that seems to work most of the time, but it's not perfect. For example, if I do the drag / drop too quickly, the target data is simply overwritten (I assume it's not being written to the MemoryMappedFile quickly enough). Does anyone have any recommendations?
This is what I currently have:
private void pbSprite_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)(sender);
DataObject data = new DataObject();
if (pb.Name == pbSprite.Name)
{
data = new DataObject(DataFormats.Serializable, frmpkm);
}
else
{
data = new DataObject(DataFormats.Serializable, frmpkm2);
}
pb.DoDragDrop(data, DragDropEffects.Move);
MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen("name", 1000, MemoryMappedFileAccess.ReadWrite);
using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
{
PKMDS.Pokemon otherpkm = new PKMDS.Pokemon();
for (int i = 0; i < Marshal.SizeOf(otherpkm); i++)
{
FileMap.Read<byte>(i, out otherpkm.Data[i]);
}
if (pb.Name == pbSprite.Name)
{
frmpkm.Data = otherpkm.Data;
}
else
{
frmpkm2.Data = otherpkm.Data;
}
lblData.Text = frmpkm.SpeciesName;
lblData2.Text = frmpkm2.SpeciesName;
pbSprite.Image = frmpkm.Sprite;
pbSprite2.Image = frmpkm2.Sprite;
}
}
private void pbSprite_DragDrop(object sender, DragEventArgs e)
{
if (e.Data != null)
{
PictureBox pb = (PictureBox)(sender);
PKMDS.Pokemon otherpkm = (PKMDS.Pokemon)e.Data.GetData(DataFormats.Serializable);
MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen("name", 1000, MemoryMappedFileAccess.ReadWrite);
using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
{
for (int i = 0; i < Marshal.SizeOf(frmpkm); i++)
{
if (pb.Name == pbSprite.Name)
{
FileMap.Write<byte>(i, ref frmpkm.Data[i]);
}
else
{
FileMap.Write<byte>(i, ref frmpkm2.Data[i]);
}
}
}
if (pb.Name == pbSprite.Name)
{
frmpkm.Data = otherpkm.Data;
}
else
{
frmpkm2.Data = otherpkm.Data;
}
lblData.Text = frmpkm.SpeciesName;
lblData2.Text = frmpkm2.SpeciesName;
pbSprite.Image = frmpkm.Sprite;
pbSprite2.Image = frmpkm2.Sprite;
}
}
Two possible things to try
Move the call to pb.DoDragDrop(data, DragDropEffects.Move); after the creation of the memory mapped file. This will cause a delay in the start of the drag/drop action but should ensure that the data has been written to the file.
The other alternative is to write the data to the mmf in a separate thread and have it set and Event when the data is written. Then pbSprite_DragDrop can wait for the event to be signaled before reading from the file.
WARNING: Embedded software delevoper trying to build PC software!
I'm trying to interface a piece of hardware that communicates with the PC via serial interface. The PC software (C#) periodicaly sends a byte array, which I would like to adjust using some trackbars.
Instead of adding 8 trackbars on the design view, I add one to help me align it and then I create a List which I populate on load like so:
public partial class FormDmxTemplate : Form
{
// Controls
// Create a list of tracbars.
List<TrackBar> trackBarDmx = new List<TrackBar>();
public FormDmxTemplate()
{
InitializeComponent();
}
private void FormDmxTemplate_Load(object sender, EventArgs e)
{
// Add first instance on the list
trackBarDmx.Add(trackBarDmx1);
// Generate 7 more, 8 total, of each
// Copy settings, and place them next to each other
for (int i = 1; i < 8; i++)
{
// Trackbars
trackBarDmx.Add(new TrackBar());
trackBarDmx[i].TickStyle = trackBarDmx[0].TickStyle;
trackBarDmx[i].Orientation = trackBarDmx[0].Orientation;
trackBarDmx[i].Minimum = trackBarDmx[0].Minimum;
trackBarDmx[i].Maximum = trackBarDmx[0].Maximum;
trackBarDmx[i].Size = new System.Drawing.Size(trackBarDmx[0].Size.Width, trackBarDmx[0].Size.Height);
trackBarDmx[i].Location = new System.Drawing.Point(trackBarDmx[i-1].Location.X + 60, trackBarDmx[0].Location.Y);
this.Controls.Add(trackBarDmx[i]);
}
}
}
Is it possible to have events for all the List members like this one?
private void trackBarDmx1_Scroll(object sender, EventArgs e)
{
}
Which means I'd like update the relevant byte in my byte array to match the TrackBar value, using events if possible.
NOTE: This is a form template which I load and close via another form.
You can subscribe to the events when creating the TrackBars. All can have the same event handler:
trackBarDmx[i].Scroll += trackBarDmx1_Scroll;
Then in the handler you can find out which is this TrackBar and at which index it is (if necessary)
private void trackBarDmx1_Scroll(object sender, EventArgs e)
{
TrackBar bar = sender as TrackBar;
int trackBarIndex = this.trackBarDmx.IndexOf(bar);
}
Not sure if I understood what you are trying to achieve. Does this do what you need:
for (int i = 0; i < 7; i++)
{
TrackBar trackBar = new TrackBar();
trackBar.Tag = i;
// Other properties
trackBar.Scroll += new EventHandler(trackBar_Scroll);
}
In the handler:
void trackBar_Scroll(object sender, EventArgs e)
{
// Get the trackbar
TrackBar current = sender as TrackBar;
// Do something here. Use tag property to identify which byte array should be changed
}
BTW, do you really need to retain the list of TrackBar?
You can do this :
for (int i = 1; i < 8; i++)
{
// Trackbars
trackBarDmx.Add(new TrackBar());
trackBarDmx[i].TickStyle = trackBarDmx[0].TickStyle;
trackBarDmx[i].Orientation = trackBarDmx[0].Orientation;
trackBarDmx[i].Minimum = trackBarDmx[0].Minimum;
trackBarDmx[i].Maximum = trackBarDmx[0].Maximum;
trackBarDmx[i].Size = new System.Drawing.Size(trackBarDmx[0].Size.Width, trackBarDmx[0].Size.Height);
trackBarDmx[i].Location = new System.Drawing.Point(trackBarDmx[i-1].Location.X + 60, trackBarDmx[0].Location.Y);
this.Controls.Add(trackBarDmx[i]);
// Notice no number in the handler name
trackBarDmx[i].Scroll += trackBarDmx_Scroll;
}
Now in the handler the simplest thing to do would be :
private void trackBarDmx_Scroll(object sender, EventArgs e)
{
var tb = sender as TrackBar;
if(sender == null)
{return;}
switch (sender.Name)
{
case "trackBarDmx1_Scroll" :
// handle changes to bar 1
break;
// and so on
}
}
I am trying to make touch buttons in WP8 with all the states (Pressed, Released, Moved), but the TouchLocationState.Released is not working.
Here's my code:
Class variables:
bool touching = false;
int touchID;
Button tempButton;
Button is a separate class with a method to switch states when touched.
The Update method contains the following code:
TouchCollection touchCollection = TouchPanel.GetState();
if (!touching && touchCollection.Count > 0)
{
touching = true;
foreach (TouchLocation location in touchCollection)
{
for (int i = 0; i < menuButtons.Count; i++)
{
touchID = location.Id; // store the ID of current touch
Point touchLocation = new Point((int)location.Position.X, (int)location.Position.Y); // create a point
Button button = menuButtons[i];
if (GetMenuEntryHitBounds(button).Contains(touchLocation)) // a method which returns a rectangle.
{
button.SwitchState(true); // change the button state
tempButton = button; // store the pressed button for accessing later
}
}
}
}
else if (touchCollection.Count == 0) // clears the state of all buttons if no touch is detected
{
touching = false;
for (int i = 0; i < menuButtons.Count; i++)
{
Button button = menuButtons[i];
button.SwitchState(false);
}
}
menuButtons is a list of buttons on the menu.
A separate loop (within the Update method) after the touched variable is true
if (touching)
{
TouchLocation location;
TouchLocation prevLocation;
if (touchCollection.FindById(touchID, out location))
{
if (location.TryGetPreviousLocation(out prevLocation))
{
Point point = new Point((int)location.Position.X, (int)location.Position.Y);
if (prevLocation.State == TouchLocationState.Pressed && location.State == TouchLocationState.Released)
{
if (GetMenuEntryHitBounds(tempButton).Contains(point))
// Execute the button action. I removed the excess
}
}
}
}
The code for switching the button state is working fine but the code where I want to trigger the action is not.
location.State == TouchLocationState.Released mostly ends up being false.
(Even after I release the touch, it has a value of TouchLocationState.Moved)
And what is more irritating that it sometimes works!
I am really confused and stuck for days now. Is this the right way? If yes then where am I going wrong? Or is there some other more effective way to do this?
I finally found the solution myself. It does not need the released state of the TouchLocationState
Posting it here. Hopefully it'll help others.
Thanks if anyone was trying.
The class variables are renamed:
private Point _touchPoint;
private TouchLocation _touchLocation;
private int _touchID;
private Button _selectedButton;
private bool _touched;
private bool _launchEvent;
The update method now has the following code
TouchCollection touchCollection = TouchPanel.GetState();
if (!_touched && touchCollection.Count > 0)
{
_touched = false;
_launchEvent = false;
foreach (TouchLocation location in touchCollection)
{
for (int i = 0; i < menuButtons.Count; i++)
{
Button button = menuButtons[i];
_touchID = location.Id;
_touchPoint = new Point((int)location.Position.X, (int)location.Position.Y);
if (GetButtonHitBounds(button).Contains(_touchPoint))
{
button.SwitchState(true);
_selectedButton = button;
}
}
}
}
else if (touchCollection.Count == 0)
{
_touched = false;
for (int i = 0; i < menuButtons.Count; i++)
{
Button button = menuButtons[i];
button.SwitchState(false);
if (GetButtonHitBounds(button).Contains(_touchPoint) && _launchEvent)
{
OnReleased(i, PlayerIndex.One);
_launchEvent = false;
}
}
}
///
// This if statement checks whether the touch is still inside the button area.
// Then assigns a value of true to the _launchEvent variable.
//
// The 'try' block is used because if the first touch is not on button, then the
// value of the _selectedButton is null and it will throw an exception.
///
if (touchCollection.FindById(_touchID, out _touchLocation))
{
if (_touchLocation.State == TouchLocationState.Moved)
{
try
{
if (GetButtonHitBounds(_selectedButton).Contains((int)_touchLocation.Position.X, (int)_touchLocation.Position.Y))
_launchEvent = true;
else
_launchEvent = false;
}
catch { }
}
}
I'm making a page with LongListboxSelector which shows audiotracks. Each item has Button "play" and TextBlock with artist and title info. I'm using BackgroundAudioPlayer. My desire is to make next: when track is playing - image of the Button is "pause", when track is over - make its Button image "play", and make the next track's Button image "pause" (next track plays automatically). I tried somethink likes this
public AudioListPage()
{
InitializeComponent();
BackgroundAudioPlayer.Instance.PlayStateChanged += Instance_PlayStateChanged;
}
void Instance_PlayStateChanged(object sender, EventArgs e)
{
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Stopped)
{
IEnumerable buttons = AudioLLS.Descendants<Coding4Fun.Phone.Controls.RoundButton>();
var test = new BitmapImage(new Uri(#"/Images/appbar.transport.pause.rest.png", UriKind.Relative));
foreach (Coding4Fun.Phone.Controls.RoundButton item in buttons)
{
if (item.ImageSource == test)
{
MessageBox.Show("in new image");
item.ImageSource = new BitmapImage(new Uri(#"/Images/appbar.transport.play.rest.png", UriKind.Relative));
buttons.GetEnumerator().MoveNext();
item.ImageSource = new BitmapImage(new Uri(#"/Images/appbar.transport.pause.rest.png", UriKind.Relative));
}
}
}
}
PlayState.Stopped fires when track ends, and next is going to start.
I tried to take all buttons in my LLS using LinqToVisualTree helper in IEnumerable buttons = AudioLLS.Descendants<Coding4Fun.Phone.Controls.RoundButton>() and compare their images with test. But as I can see - I'm doing it wrong, because in block if(item.ImageSource == test) my app never comes. Please, tell me, what I'm doing wrong? And if my way to solve the problem is bad - please, tell me how to do it in easy way.
I added two property to my audio model
private bool isPlaing = false;
public bool IsPlaing
{
get { return isPlaing; }
set
{
isPlaing = value;
OnPropertyChanged("IsPlaing");
if (IsPlaing) Image = #"/Images/appbar.transport.pause.rest.png";
else if (!IsPlaing) Image = #"/Images/appbar.transport.play.rest.png";
}
}
private string image = #"/Images/appbar.transport.play.rest.png";
public string Image { get { return image; } set { image = value; OnPropertyChanged("Image"); } }
and in playerStateChanged i added
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Stopped)
{
for (int i = 0; i < AudioList.Count; i++)
{
if (AudioList[i].Url1 == currTrack)
{
AudioList[i].IsPlaing = false;
currTrack = AudioList[i + 1].Url1;
AudioList[i + 1].IsPlaing = true;
break;
}
}
}
BackgroundAudioPlayer.Instance.Play();
}
In this way I solved my problem. As I can see - all works fine.