C++/CLI multiple buttons maintaining variable value - visual-studio-2010

OK let me explain my problem.
I'm working on a program where I have a button. Clicking it causes the number "1" to appear, then, after that, any further clicks will increment that value until it reaches the value of "9". (It's a string). I wrote this code which declares an int variable to 0 (Yes, this was a mistake but let me continue) then increment it and parse it to string and show it on the button text(This is the code that executes on button click):
private: System::Void a0_Click(System::Object^ sender, System::EventArgs^ e) {
int i = 0;
i++;
a0->Text = i.ToString();
}
However, as you can suspect, I did the foolishness of declaring i with 0 for each button press, so the result was that 1 was the only value showing on the button. The next thing I tried doing, was declaring i as global variable with the value of 0. However, I came to another problem. I have 82 buttons of that kind, and I'm going for the easiest sollution I can find, so sharing the i variable seemed logical,
The next problem was that if I pressed 5 times the first button, the number displayed on it would be "5" however if I pressed another button, the value wouldn't be "1" by default, it would be "6" (The value of the first button incremented by one). Basically it would inherit the value of the first.
Now I'm at a dead end. I have no idea what to do. I tried using i and i2 but I was just chasing my own tail. Is there a very easy solution to this? Keep in mind I've got 82 buttons (Yes I know it's alot) which are by default 0. When I click each one I need it to increment by one, starting from 0. Any ideas?
Notes: OS is Windows XP, IDE is Visual Studio 2010m using windows forms app, C++/CLI. If I forgot to mention anything post in comments and I'll add it.

You can inspect sender to find out what button was clicked.
void anybutton_Click(System::Object^ sender, System::EventArgs^)
{
Button^ btn = dynamic_cast<Button^>(sender); // or safe_cast
int i;
if (System::Int32::TryParse(btn->Text, i)) {
i++;
btn->Text = i.ToString();
}
}

Related

ContextMenuStrip disappears after a quarter second display

Well, I'm back. I solved the ListBox problem by using a ContextMenuStrip — or so I thought!
It comes in perfect, then in a quarter second, two things happen: it disappears, and the TreeNode unselects.
But for that quarter second, it looks perfect!
The way that progresses is, I step through the lines of code on the right of the screenshot, 5 lines, then one more F11, and it appears for a 1/4 second, and the TreeNode UNSELECTS.
But here's another interesting thing:
In Design Mode, I clicked the reference to contextMenuStrip1 in the dialog bar beneath the Form, and it appears in the place I dropped it.
Then a click on the TreeView (white as I suppose you know) and it disappears.
Ah, but then I take the mouse and pull the TreeView over to the right, again click the reference below, again the ContextMenuStrip appears, then I click the TreeView, and AGAIN it disappears!
What am I missing, dear friends? Something to do with the focus?
Much obliged for any help.
Got it! I found that responding to Closing could be stopped for study with a break point. Then poked around in the Properties, and tried a few things. Then ultimately googled and found the answer at
Do not close ContextMenuStrip on selection of certain items. I added AppFocusChange as not a reason to close.
So far, I'm now able to click the members and select them. Progress!
private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked ||
e.CloseReason == ToolStripDropDownCloseReason.AppFocusChange)
e.Cancel = true;
}
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
{
for (int i = 0; i < 4; i++)
{
if (contextMenuStrip1.Items[i].Selected == true)
{
bool res = DoChoice(i);
if (res == true) e.Cancel = false;
}
}
}
Then, at DoChoice, I'll invoke the Add or Edit accordingly, and always returna Close to the strip.

Unity - Score stays on 1

Looking around in the forum. Finally, I decided to join in this big community for the support it provides!
I am creating this post because I am struggling in a 2D game that I am creating in Unity.
The game keeps scoring the number one once the play button is clicked. It increases the score once the food is collected, but the problem here is that once I click on "Play" button the score is already on "1" instead of "0".
Code in the following statement:
Scoring System
Collect C#
Thank you for your help!
You could do 2 things:
Add a Start() function in Scoring System script.
private void Start(){
// This would make sure that score is 0 from the first frame
scoreText.GetComponent<Text>().text = " " + 0;
}
2)Instead of directly running the code on trigger use tags, assign the tag of Player to your player gameObject, then add this code.
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player"){
ScoringSystem.theScore += 1;
// Destroy(collision.gameObject);
}
}

Arduino keypad matrix example? ( teensyduino )

I'm a beginner using Arduino with a Teensy 3.2 board and programming it as a usb keyboard.
I have two 4 button membrane switches. Their button contacts are on pins 1-8, and the 9th pin holds a soldered together wire of both membrane switches' "ground" line or whatever it's true name is; the line that completes the circuit.
Basically when you press the buttons they are supposed to simply type "a, b, c..." respectively. I've been told I need to use a matrix for this.
I'm looking for an example of how to code a keyboard matrix that effectively supports a one row/9 column line (or vice versa?) I've been unable to find that solution online.
All I have so far is this code which, when the button on the second pin is pressed, sends tons of "AAAAAAAAAAAAAAAA" keystrokes.
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button is pressed
if(digitalRead(2)==LOW){
//Send an ASCII 'A',
Keyboard.write(65);
}
}
Would anyone be able to help?
First of all, a 1-row keypad is NOT a matrix. Or better, technically it can be considered a matrix but... A matrix keypad is something like this:
You see? In order to scan this you have to
Pull Row1 to ground, while leaving rows 2-4 floating
Read the values of Col1-4. These are the values of switches 1-4
Pull Row2 to ground, while leaving rows 1 and 3-4 floating
Read the values of Col1-4. These are the values of switches 5-8
And so on, for all the rows
As for the other problem, you are printing an A when the button is held low. What you want to achieve is to print A only on the falling edge of the pin (ideally once per pressure), so
char currValue = digitalRead(2);
if((currValue==LOW) && (oldValue==HIGH))
{
//Send an ASCII 'A',
Keyboard.write(65);
}
oldValue = currValue;
Of course you need to declare oldValue outside the loop function and initialize it to HIGH in the main.
With this code you won't receive tons of 'A's, but however you will see something like 5-10 'A's every time you press the button. Why? Because of the bouncing of the button. That's what debouncing techniques are for!
I suggest you to look at the class Bounce2 to get an easy to use class for your button. IF you prefer some code, I wrote this small code for another question:
#define CHECK_EVERY_MS 20
#define MIN_STABLE_VALS 5
unsigned long previousMillis;
char stableVals;
char buttonPressed;
...
void loop() {
if ((millis() - previousMillis) > CHECK_EVERY_MS)
{
previousMillis += CHECK_EVERY_MS;
if (digitalRead(2) != buttonPressed)
{
stableVals++;
if (stableVals >= MIN_STABLE_VALS)
{
buttonPressed = !buttonPressed;
stableVals = 0;
if (buttonPressed)
{
//Send an ASCII 'A',
Keyboard.write(65);
}
}
}
else
stableVals = 0;
}
}
In this case there is no need to check for the previous value, since the function already has a point reached only when the state changes.
If you have to use this for more buttons, however, you will have to duplicate the whole code (and also to use more stableVals variables). That's why I suggsted you to use the Bounce2 class (it does something like this but, since it is all wrapped inside a class, you won't need to bother about variables).

Showing a windows with XCB / Strange Behaviour

I'm trying to show a window in xcb, inside the main window, but actually without luck.
The idea is that when the user press a button (in that case the X button) a small white window is shown (just for test).
But actually i'm stuck on that step. I watched the example code here:
http://en.wikibooks.org/wiki/X_Window_Programming/XCB
And tried to do the same in my application.
[EDIT 28/10/2013] Now with that code i can show a window, but if i try to add other variable like int i=0, or whatever, the window doesn't appear, and no expose events were raised (all events that were raised are or 0 or 2 (even if i add the variables inside other events). Any idea?
This is the XCB_KEY_PRESS event handler code:
Edit (with the new code)
case XCB_KEY_PRESS:{
xcb_key_press_event_t *kp = (xcb_key_press_event_t *)ev;
if(kp->detail==53){
printf("X pressed\n");
uint32_t vals[2];
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
vals[0]=screen->white_pixel;
vals[1]=XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
win = xcb_generate_id(connection);
xcb_create_window(
connection,
XCB_COPY_FROM_PARENT,
win,
root,
80,80,
150,150,
10,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
mask, values);
mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
vals[0]=screen->white_pixel;
vals[1]=0;
background=xcb_generate_id(connection);
xcb_create_gc(connection, background, win, mask, vals);
xcb_map_window(connection,win);
xcb_flush(connection);
printf("finished\n");
}
printf("KEY_PRESS - Pressed: %d\n", kp->detail);
}
root is the root window obtained from xcb_screen_t variable.
The definition of background and win are the following:
xcb_window_t win;
xcb_gcontext_t background;
And i added even a XCB_EXPOSE event handler:
case XCB_EXPOSE:{
printf("EXPOSE NEW WINDOW CREATED\n");
xcb_poly_fill_rectangle(connection, win, background,1,&rectangle);
xcb_flush(connection);
}
What is wrong with that code? What am i missing? (I'm trying to develop a very basic window manager, just for fun)
(My idea for that program is that when x is pressed an input box is shown, do you have any suggestion on how to do that?)

How to add my own right click menu?

How to add my right click menus in SSMS 2008R2\2012 Object Explorer?
I researched this topic.
I do this way:
private void Provider_SelectionChanged(object sender, NodesChangedEventArgs args)
{
INodeInformation[] nodes;
int nodeCount;
objectExplorer.GetSelectedNodes(out nodeCount, out nodes);
INodeInformation node = (nodeCount > 0 ? nodes[0] : null);
if (_databaseMenu == null &&
_databaseRegex.IsMatch(node.Context))
{
_databaseMenu = (HierarchyObject)node.GetService(typeof(IMenuHandler));
_databaseMenu.AddChild(string.Empty, new MenuItem());
}
}
BUT the problem is: if I do left click on database and then right click - I see my menu, ok. If I expand the object tree via (+) and then immediately right click on database - I do not see my menu.
I understand why it is but how to solve this problem?
I spent a considerable amount of time working on this same issue for my own SSMS add-in. What I came up with is a dirty hack, but it was the only way I could find to get it working reliably.
You use SendKeys.SendWait to issue SHIFT + F10, which is the shortcut to open the context menu, and you do it twice, since a single issuance will toggle the menu's state (visible to not or vice versa). The UI will stop responding and eventually throw if you use Send, so be sure to use SendWait.
There will be a slight delay on left click or flicker of the menu on right click. And, of course, this won't work if the user has altered that shortcut (or has defined external, superseding macros), but a quick glance through the SSMS options doesn't reveal any way to change the context menu shortcut.
private void Provider_SelectionChanged(object sender, NodesChangedEventArgs args)
{
INodeInformation[] nodes;
int nodeCount;
objectExplorer.GetSelectedNodes(out nodeCount, out nodes);
INodeInformation node = (nodeCount > 0 ? nodes[0] : null);
if (_databaseMenu == null &&
_databaseRegex.IsMatch(node.Context))
{
_databaseMenu = (HierarchyObject)node.GetService(typeof(IMenuHandler));
_databaseMenu.AddChild(string.Empty, new MenuItem());
SendKeys.SendWait("+({F10})")
SendKeys.SendWait("+({F10})")
}
}

Resources