C# Windows form delete key not responding - c#

in my C# windows form application I want the user to be able to remove a node by pressing the delete key on their keyboard and have implemented the seemingly correct code but when the delete key is pressed nothing happens.
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys .Delete)
{
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Remove();
}
}
This is the code I am attempting to use to implement the desired function.

The keyboard keycode check looks fine to me, which says to me that treeView1.SelectedNode is probably returning null.

I met the problem today and have tried many ways to address it. MSDN gives the answer actually. Here are the two codes that I put in public Form1()
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
I've just study C# this week, so I am trying to figure out the meaning of the codes. Just hope this approach will work for the people who needs.
If anyone can explain the code above, I will be very grateful.

Related

How do I open a window that I have created with Winforms in my code

I have a Winforms window design that I made and I want to test it. I want to open it via a right click. What do I need to call in order to open this new window?
Something like this:
private void openBlackHoleSingularity_Click(object sender EventArgs e)
{
bool failed = false;
//Open StartSingularity.cs???
if(failed)
{ MessageBox.Show(this, "Need more Antimatter!"); return;}
}
I know this is a very Googleable question, but I am not very good at GoogleFu yet, and I am not really getting a definitive answer. I apologize in advance for the stupid question.
Okay so it's just creating an object and for some reason I just didn't even think to do that.
For fellow lads and lasses who are learning it is:
StartSingularityForm createBlackHole = new StartSingularityForm();
createBlackHole.Show();

How do I set a Timer that executes my code every 60sec?

I am working in Visual Studio 2017 in C# WinForms- The project is based on a conversion from VB6 to C#. What it does right now is that I press a GetQue-button which gather a key that resembles a document, thereafter that document is exported through mail to another mail.
Now for the last point I need to create a Timer where it executes all of this code every 60sec.
I really appreciate all types solutions and I really do not know a lot about timers, but I would really like some simple explanation as well.
Note that I have only used a timer in an simple console app once.
As this code below, the timer will execute the button click which starts the process and it is done within a 60sec interval.
private void StartTimer_Tick(object sender, EventArgs e)
{
if (bRunning == false)
{
bRunning = true;
BtnGetQue_Click(sender, e);
bRunning = false;
}
}

Capture x and y of user touch, iOS Xamarin Forms

I am using Xamarin Forms (Xamarin Studio Community) to write an iOS app. I just need to capture the coordinates of user touch and release on a button.
I have searched all over Google, and I only found responses that simply did not work. They were either for Android, or Swift, or XCode, or WAY too much to make practical use of.
The closest I've seen are answers that say "There is currently no way to do that in Xamarin Forms." Which seems absurd to me because it seems like one of the most basic, fundamental things a programmer would need.
All I need is to capture the x,y from where the user touches and releases. Here are my handlers all ready to go.
thisHereButton.TouchDown += (object sender, System.EventArgs e) =>
{
//pressed
touchX = [x];
touchY=[y];
};
thisHereButton.TouchUpInside += (object sender, System.EventArgs e) =>
{
//released
releaseX = [x];
releaseY=[y];
};
That's it. If someone could provide a simple, practical answer, that would be great!
EDIT: With a bounty goes additional research, in hopes that someone can help.
I followed the advice in the comments and after some analysis, have determined that this is the chunk of code that is most relevant.
private bool touchStartedInside;
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
// get the touch
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
//==== IMAGE TOUCH
if (backdrop.Frame.Contains(touch.LocationInView(View)))
{
//TouchStatus.Text = "Touches Moved";
}
//==== IMAGE DRAG
// check to see if the touch started in the drag me image
if (touchStartedInside)
{
// move the shape
float offsetX = (float)(touch.PreviousLocationInView(View).X - touch.LocationInView(View).X);
float offsetY = (float)(touch.PreviousLocationInView(View).Y - touch.LocationInView(View).Y);
txt_sizeValText.Text = "" + offsetY;
//DragImage.Frame = new RectangleF(new PointF(DragImage.Frame.X - offsetX, DragImage.Frame.Y - offsetY), DragImage.Frame.Size);
}
}
}
That is taken directly from their example. Right there in the code, it gives me what I'm looking for, as well as the bonus of the "offset" which is what I really need in the end.
So I've got this void in my code and the code still compiles, so there are no errors.
The only thing I cannot figure out is how to make use of this void. How do you call it?
If I have to use a different handler than the ones in my original post, that's fine. Whatever you can tell me about how to make this work...
You don't need to call the TouchesMoved() method. It is automatically called when the Objective C runtime calls it ( it is an override of the method to be called) - It is basically the handler of the native event. You can already get the offsets, and you can raise your own events inside that method to change your button. It is already done.

How do I use the keydown function on a winform through other windows?

I have this piece of code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode== Keys.Z)
{
BarTimer.Value = 0;
timer1.Start();
timer1.Interval = 500;
BarTimer.Minimum = 0;
BarTimer.Maximum = 80;
}
}
And it works just fine, but I want to be able to press it when in a fullscreen application, so I can see the progress bar on my second monitor. Is there a property or something i can change to make it work like that?
If it's a single key you can use RegisterHotKey. There is sample code in this answer: how to use RegisterHotKey and the one linked in it. The Microsoft C++ documentation is here.
If you want to intercept all keyboard input you would need a keyhook. I suggest you google WH_KEYBOARD_LL and SetWindowsHookEx. But I don't think you can do this in c#; I've only ever seen key hooks written in C/C++. They will also cause some firewalls to take an interest - intercepting all keyboard input can be considered potentially hostile.
Hi I know of a nice tutorial I found On code project.
It worked for me when i needed something like this.
http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
Regards,

Disable normal behavior of Alt key

When pressing the Alt key, normally the focus goes to the window's menu. I need to disable it globally. Because my application works with Alt key. When the user presses some key in combination with the Alt key, my application sends another key to active application. So I just need to ignore Alt when it opens the menu.
I'm new to programming and I'm looking for VB.Net or C# code.
My first answer is to NOT use the Alt key for your program and use Ctrl instead. Blocking "normal" things from happening usually leads to pain in the future.
But if you must use the Alt key I would check out this article which uses message filters to try and intercept it at the application level.
If that doesn't do what you're looking for, you might need to look into Global hooks, this link will get you started down the path. Global hooks are generally considered evil so you should only use this if the above two suggestions don't work. You must make sure that you uninstall your hooks otherwise you might find that you need to reboot your computer often to fix weird keyboard problems.
This works for me:
private class AltKeyFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
return m.Msg == 0x0104 && ((int)m.LParam & 0x20000000) != 0;
}
}
And then you add the message filter like so:
Application.AddMessageFilter(new AltKeyFilter());
You can try something like this:
public void HandleKeyDown(object sender, keyEventArgs e)
{
//do whatever you want with or without Alt
if (e.Modifiers == Keys.Alt)
e.SuppressKeyPress = true;
}
This should allow you to use Alt for whatever you want but keep it from activating the menustrip. Note that e.SuppressKeyPress = true also sets e.Handled = true.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keyeventargs.suppresskeypress?view=windowsdesktop-5.0

Categories