ContextMenuStrip disappears after a quarter second display - treeview

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.

Related

Unity show pop up if the player doesn't move for 5 seconds

I have a task on Unity 3d that if the player doesn't move for 5 seconds, a pop-up shows on the center of the screen and if the player moves, the pop-up disappears. How can I write the logic for this task please ?
Thanks
Here is code that will check the mouse position of the user and see if it has moved in the last 5 seconds. If it has not, then the popup window will show up. If it's hard to read here with the comments (I kind of think it is) copy and paste this code into Visual Studio so the colors will help distinguish code from comments.
[SerializeField] GameObject popupWindow = null;
float totTime;
float timeBeforePause = 5f;
Vector3 updatedMousePosition;
private void Update()
{
// Add the time delta between frames to the totTime var
totTime += Time.deltaTime;
// Check to see if the current mouse position input is equivalent to updateMousePosition from the previous update
// If they are equivalent, this means that the user hasn't moved the mouse
if (Input.mousePosition == updatedMousePosition)
{
// Since the user hasn't moved the mouse, check to see if the total Time is greater than the timeBeforePause
if (totTime >= timeBeforePause)
{
// Set the popup window to true in order to show the window (instantiate instead it if if doesn't exist already)
popupWindow.SetActive(true);
}
}
// If the user has moved the mouse, set the totTime back to 0 in order to restart the totTime tracking variable
else
{
totTime = 0;
}
// Check to see if the popup window is visible (active)
if (popupWindow.activeSelf == true)
{
// Check to see if the user has pressed the Esc button
if (Input.GetKeyDown(KeyCode.Escape))
{
// Hide the window
popupWindow.SetActive(false);
}
}
// Update the updatedMousePosition before the next frame/update loop executes
updatedMousePosition = Input.mousePosition;
}
If you want to track different user input (key presses) you can use a similar method. Also you will have to implement some sort of button on the popup window that will allow the user to exit out from the popup window once they return. Hope this helps!

How to write KeyBindings

I am developing a Java 2D Game, in which I have used a KeyListener, but as you probably can guess, it has focusing issues, mainly when the Player is running and you keep the same key pressed for long, for example pressing "W" to run forward, but after some seconds of keep pressing W, the KeyListener dies and no key works, I want to use KeyBindings, as most people suggest it for game development, but I cannot find any usefull tutorials, most of them use some form of Buttons, and other useless features for my game, so how can i avoid the KeyListener from losing focus, or how can I write a simple KeyBinding code, that only moves the player, and other simple stuff used in a game.
This is the kind of Key Binding that I want, I KNOW IT DOES NOT WORK, it is an example:
component.getInputMap().put(KeyStroke.getKeyStroke(VK_W),
"move forward")
component.getActionMap().put("released",
releasedAction);
if(releasedAction == true){
Player.playerSpeedY = 7;
} else{
Player.playerSpeedY = 0;
}
FWI: this is the current KeyListener code:
if(HUD.PlayerHealth > 0){
if(key == KeyEvent.VK_W) {Player.playerSpeedY = -5; keyDown[0]= true;}
if(key == KeyEvent.VK_S) {Player.playerSpeedY = 5; keyDown [1]= true;}
if(key == KeyEvent.VK_A) {Player.playerSpeedX = -5; keyDown [2]= true;}
if(key == KeyEvent.VK_D) {Player.playerSpeedX = 5; keyDown [3]= true;}
}
I keep looking for Actions and Input maps tutorials, or KeyBinding ones, and I just don't find anything useful, another dough, the component in action and input map, what is it for?, should my entire code be based on that, and is there any way to only make the action map move the player, and only that?
Try something like this:
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right");
this.getActionMap().put("right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//Do Something Here
Player.playerSpeedX = 5;
}
});
How to use KeyBindings

libGDX lags are causing shifts

I made a games where different items(rectangles with textures) should be created regularly. These Items are always moving left (like this) so it should look like this:
But if it's lagging or if I click on the menu bar (where the close button etc. is)
and the game "pauses" the items aren't spawning regularly so it looks like this:
This is obstructing the gameplay so I want to fix this.
This is the part where I spawn the items:
public void renderMain(){
timeState += delta;
if (timeState >= 0.32f) {
timeState = 0f;
Item.spawnitem();
}
...
How do I fix this?

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})")
}
}

C++/CLI multiple buttons maintaining variable value

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();
}
}

Resources