I have coded an app in WPF where i have Keydown event functioning with the code below:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(Form1_KeyDown);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(Form1_KeyUp);
}
//Declare the comands for Rover control//
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) // Holding Keyboard Character "W" //
{
serialPort1.Write("F"); // Passing command "Forward" thorugh letter "F" in arduino code //
}
I am trying to replicate this in UWP and am not sure what I am doing wrong.
Based on research so far, I understand that i have to place KeyDown="Grid_KeyDown" within Grid in XAML part, and i have to write something like:
private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
//handling code here
if (e.Key == Key.F)
{
string sendData = "F";
if (string.IsNullOrEmpty(sendData))
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Please specify the string you are going to send";
}
else
{
DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
UInt32 len = dwriter.MeasureString(sendData);
dwriter.WriteUInt32(len);
dwriter.WriteString(sendData);
await dwriter.StoreAsync();
await dwriter.FlushAsync();
}
}
However this is not working. Does anyone have any suggestion how do I make this Keydown even work so when i press F key i want to pass that to serial port and send it as string "F" to Bluetooth device?
Ok so when i involve the if (e.Key == Windows.System.VirtualKey.F) the code works as below:
private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
//handling code here
if (e.Key == Windows.System.VirtualKey.F)
{
string sendData = "F";
if (string.IsNullOrEmpty(sendData))
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Please specify the string you are going to send";
}
else
{
DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
UInt32 len = dwriter.MeasureString(sendData);
dwriter.WriteUInt32(len);
dwriter.WriteString(sendData);
await dwriter.StoreAsync();
await dwriter.FlushAsync();
}
}
}
However, i am having like the delay. As i mentioned in the first part of the code where I was using serialPort1.Write, the Bluetooth will send the command to the robot instantaneously when i press button and will stop right away when i release the button. So i have to incorporate something similar to UWP as i did in the WPF code below:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(Form1_KeyDown);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(Form1_KeyUp);
}
//Declare the comands for Rover control//
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) // Holding Keyboard Character "W" //
{
serialPort1.Write("F"); // Passing command "Forward" thorugh letter "F" in arduino code //
}
Thank you
Have you tried using key up event as below:
<Grid KeyUp="Grid_KeyUp">
...
</Grid>
void Grid_KeyUp(object sender, KeyRoutedEventArgs e)
{
//handling code here
}
For more details read the documentation here.
Maybe this will help! This is how I used KeyUP ...
`private void Value1_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
string value = Value1.Text;
PivotItem pi = MainPivot.SelectedItem;
((WebView)pi.Content).Navigate(new Uri(value,
UriKind.Absolute));
MainPivot.SelectedItem = pi;
TagTextBlock.Text = Value1.Text;
pi.Header = ((WebView)pi.Content).DocumentTitle;
}
}
`
Maybe you need to flush the DataWriter:
await dwriter.FlushAsync();
Related
Here's what H have so far:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}
private void button3_Click(object sender, EventArgs e)
{
audio.Stop();
if (button1.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
if (button2.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
}
}
this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.
Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.
also:if (button2.Enabled == true)
is nested in the first conditional, I'm not sure if that's what you want.
You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)
More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
button3.Click += new EventHandler(this.Button3_Click_First);
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
button3.Click += new EventHandler(this.Button3_Click_Second);
}
void Button3_Click_First(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
}
void Button3_Click_Second(Object sender,
EventArgs e)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers
You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.
private string _address = null;
private void button1_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}
private void button2_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}
private void button3_Click(object sender, EventArgs e)
{
if (_address != null)
{
audio.Stop();
if (button1.Enabled || button2.Enabled)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start(_address);
}
}
}
I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.
button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((bool)ViewState["Button1Clicked"])
{
//open webpage2 code comes here
}
else
{
//open webpage2 code comes here
}
}
I'm doing a simple login screen and I have already implemented a simple method in the password textbox to simulate the 'OK" button being clicked:
private void textpwd_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = btnLogin;
}
can I use the same method in the username textbox to move to the password textbox?
private void textusername_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = textpassword.Focus();
}
private void textusername_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
textpassword.Focus(); // or SendKeys.Send("{Tab}");
}
Not to write a specific code for each of your TextBox(es) it's better to simulate the Tab key press behavior.
Edit the Tab order via View -> TabOrder based on your needs.
Set the following method for all of your form controls KeyDown event
private void AllControls_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
e.SuppressKeyPress = true;
}
}
I want my checkedlistbox to expand to a certain size when the mouse enters and then go back to a its original size after mouse leaves. Below is the code is have. However, I receive an error when i have another program selected and my mouse goes over the checkedlistbox while the application is not active.
Any suggestions on how to fix?
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;}
Error Code - Object Reference not set to an instance of an object.
Try this
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
checkedListBox1.Size = new Size(Width,Height);
}
This of course would work so that no exception is thrown, but I hope it's also what you want:
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;
}
Hello Everyone. This is my first Program and within in 5 minutes I have a error. I've only started to today using C#, so I know I should be really looking around, but I didn't think there was a problem with what I was doing.
My Program is a Generator
depending on what a user picks or types in all the textboxes depends on the outlook of the generated code.
I have two text boxes named: textBox1, and GeneratedCode
When I press checkBox1 it allows textbox1 to be used.
When I press my button it created a string "Testing" (which was to make sure I did it right).
When I pressed F5 to test my build it came back with this error:
No overload for 'textBox1_TextChanged' matches delegate 'System.EventHandler'
I do not know what this means.
Here's my code:
public void checkBox1_CheckedChanged(object sender, EventArgs e)
{
switch (checkBox1.Checked)
{
case true:
{
textBox1.Enabled = true;
break;
}
case false:
{
textBox1.Enabled = false;
break;
}
}
}
private void textBox1_TextChanged()
{
}
public void button1_Click(object sender, EventArgs e)
{
GenerateBox.Text += "Testing";
}
private void GenerateBox_Generated(object sender, EventArgs e)
{
}
This is form1.designer which is in C++:
//
// textBox1
//
this.textBox1.Enabled = false;
this.textBox1.Location = new System.Drawing.Point(127, 3);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(336, 20);
this.textBox1.TabIndex = 1;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); //Error
//
// GenerateBox
//
this.GenerateBox.Enabled = false;
this.GenerateBox.Location = new System.Drawing.Point(84, 6);
this.GenerateBox.MaxLength = 1000000;
this.GenerateBox.Multiline = true;
this.GenerateBox.Name = "GenerateBox";
this.GenerateBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.GenerateBox.Size = new System.Drawing.Size(382, 280);
this.GenerateBox.TabIndex = 1;
this.GenerateBox.TextChanged += new System.EventHandler(this.GenerateBox_Generated);
The function textbox1_textChanged should have two arguments as below to be accepted by EventHandler in this case
textBox1_TextChanged(object sender, EventArgs e)
Your textbox1_TextChanged method does not match what is expected of the System.EventHandler delegate. It should be
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
The compiler is telling you exactly what is wrong, you don't have an EventHandler called textBox1_TextChanged.
Change your textBox1_TextChanged method to read:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//Why are you handling this event if you aren't actually doing anything here???
}
For the rest of my concern with this question, please refer to the commented portion of my code example.
If you didn't mean to add a handler for this event, just remove the following from your designer code:
textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
I am making a program that should just continue if 2 conditions are given.
The first one, 2 TextBoxs have the same word in and a Button was clicked, which opens a new Form. Now I have the event for the "complete" button.
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == textBox3.Text && ???)
{
StreamWriter myWriter = File.CreateText(#"c:\Program Files\text.txt");
myWriter.WriteLine(textBox1.Text);
myWriter.WriteLine(textBox2.Text);
}
]
My problem is, I can't find a method that gives something like `button1.Clicked or something similar.
I hope someone can help me here..
Click is an event that fires immediately after you release the mouse button. So if you want to check in the handler for button2.Click if button1 was clicked before, all you could do is have a handler for button1.Click which sets a bool flag of your own making to true.
private bool button1WasClicked = false;
private void button1_Click(object sender, EventArgs e)
{
button1WasClicked = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == textBox3.Text && button1WasClicked)
{
StreamWriter myWriter = File.CreateText(#"c:\Program Files\text.txt");
myWriter.WriteLine(textBox1.Text);
myWriter.WriteLine(textBox2.Text);
button1WasClicked = false;
}
}
These helped me a lot: I wanted to save values from my gridview, and it was reloading my gridview /overriding my new values, as i have IsPostBack inside my PageLoad.
if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null)
{
//Do not reload the gridview.
}
else
{
reload my gridview.
}
SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked
button1, button2 and button3 have same even handler
private void button1_Click(Object sender, EventArgs e)
{
Button btnSender = (Button)sender;
if (btnSender == button1 || btnSender == button2)
{
//some code here
}
else if (btnSender == button3)
//some code here
}
i am very new to this website. I am an undergraduate student, doing my Bachelor Of Computer Application.
I am doing a simple program in Visual Studio using C# and I came across the same problem, how to check whether a button is clicked?
I wanted to do this,
if(-button1 is clicked-) then
{
this should happen;
}
if(-button2 is clicked-) then
{
this should happen;
}
I didn't know what to do, so I tried searching for the solution in the internet. I got many solutions which didn't help me. So, I tried something on my own and did this,
int i;
private void button1_Click(object sender, EventArgs e)
{
i = 1;
label3.Text = "Principle";
label4.Text = "Rate";
label5.Text = "Time";
label6.Text = "Simple Interest";
}
private void button2_Click(object sender, EventArgs e)
{
i = 2;
label3.Text = "SI";
label4.Text = "Rate";
label5.Text = "Time";
label6.Text = "Principle";
}
private void button5_Click(object sender, EventArgs e)
{
try
{
if (i == 1)
{
si = (Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text)) / 100;
textBox4.Text = Convert.ToString(si);
}
if (i == 2)
{
p = (Convert.ToInt32(textBox1.Text) * 100) / (Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text));
textBox4.Text = Convert.ToString(p);
}
I declared a variable "i" and assigned it with different values in different buttons and checked the value of i in the if function.
It worked. Give your suggestions if any. Thank you.