No overload for 'textBox1_TextChanged' matches delegate 'System.EventHandler' - c#

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

Related

UWP KeyDown Event

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

Optimizing a repetiton

I have a small Menu strip item where I have a plethora of buttons which activate different forms.
The code for one button would be this:
Form B1 = new Form1();
private void Button1_Click(object sender, EventArgs e)
{
if (B1.Visible == false)
{
B1 = new Form1();
}
B1.Visible = true;
B1.Activate();
}
I also have a mouse enter- and leave event:
private void Button1_MouseEnter(object sender, EventArgs e)
{
Button1.Text = "Something prdy intriguing";
}
private void Button1_MouseLeave(object sender, EventArgs e)
{
Button1.Text = "Hi";
}
And a tooltip:
private void Tooltips()
{
ToolTip forB1 = new ToolTip();
forB1.SetToolTip(button1, "21.11.17");
}
Now imagine i need about 8 buttons for 8 different forms, that means i have to repeat all of these again and a gain, wasting time AND taking up a LOT of code space.
Is it possible to compress these in anyway?
This is very out of my world, im unsure where to start optimizing.
One option is move all this to one function:
public void AttachMenuStripButtonHandlers(
Button btn,
Form form,
string enterText,
string leaveText,
string tooltip) {
btn.Click += (sender, args) => {
form.Visible = true;
form.Activate();
};
btn.MouseEnter += (sender, args) => {
btn.Text = enterText;
};
btn.MouseLeave += (sender, args) => {
btn.Text = leaveText;
};
new ToolTip().SetToolTip(btn, tooltip);
}
And for each button call like this:
AttachMenuStripButtonHandlers(Button1, B1, "on enter", "on leave", "tooltip");
For second part of your question, You could do something like this
private void Button_MouseEnter(object sender, EventArgs e)
{
((Button)sender).Text = "Something prdy intriguing";
}
private void Button_MouseLeave(object sender, EventArgs e)
{
((Button)sender).Text = "Hi";
}
You need to attach same event handler to all buttons.
So i am kinda stealing #Evk's code here but essentially this works the way I wanted to.
public void ButtonHandlers(Type NewForm)
{
NewButton.Click += (sender, args) =>
{
Form TheNewMain = (Form)Activator.CreateInstance(NewForm);
if (TheNewMain.ShowDialog() != DialogResult.Cancel)
{
TheNewMain.Activate();
}
};
Essentially what i added was instead of getting the Form i have to get the type, since what I want is that when a form is Visible, it won't open it twice, going by Evks code it opens it yes but upon close it's disposed and it cant create a new instance of it.
In code i just have to ask for typeof(formName) in as NewForm
Thanks Evk!

C# Checking if button was clicked

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.

Why Doesn't Regex give a chance to input data?

I have a event which doesn't give a any opportunity to input data in TextBox. When I'm trying to input data in Textbox, then Textbox doesn't give to do it:
private void Login_textbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textbox1.Text, #"^[a-zA-Z]+$"))
e.Handled = true;
}
I just want to input data in TextBox which isn't digit or any symbols.
Try using the following code
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString() , #"^[a-zA-Z]+$"))
e.Handled = true;
}
Thanks!
It seems you are using c#.
Then steps you need to follow :
1) Set causesValidation property of your textbox to true
2) Set event listeners for causes validation
myTextBox1.Validating +=
new System.ComponentModel.CancelEventHandler(myTextBox1_Validating);
myTextBox1.Validated +=
new System.EventHandler(myTextBox1_Validated);
3) Implement these event hadler functions
private void myTextBox1_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
if(!CheckIfTextBoxNumeric(myTextBox1))
{
myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void myTextBox1_Validated(object sender,System.EventArgs e)
{
myLabel.Text = "Validated first control";
}
If you instead want to use maskedTextBox refer http://msdn.microsoft.com/en-us/library/ms234064(v=vs.80).aspx

Dynamically create an ImageButton

I’m trying to dynamically declare an ImageButton.
I declare it and assign an ID and Image to it as follows:
ImageButton btn = new ImageButton();
btn.ImageUrl = "img/Delete.png";
btn.ID = oa1[i] + "_" + i;
btn.OnClick = "someMethod";
But when I try to assign an OnClick handler for the button it throws the following exception:
System.Web.UI.WebControls.ImageButton.OnClick is inaccessible due to protection level
You couldn't assign a value to a method like that, even if it were accessible. You need to subscribe to the event:
btn.Click += ClickHandlingMethod;
Take a look at this answer, it is related with dynamic controls and events
As Jon commented you cannot add a string to the event, in this case you need to add a handler for the event:
protected void Page_Init(object sender, EventArgs e)
{
var i = new ImageButton();
i.Click += new ImageClickEventHandler(i_Click);
this.myPanel.Controls.Add(i);
}
void i_Click(object sender, ImageClickEventArgs e)
{
// do something
}
Alternativeley
protected void Page_Init(object sender, EventArgs e)
{
var i = new ImageButton();
i.Click += (source, args) =>
{
// do something
};
this.myPanel.Controls.Add(i);
}
An example:
private void CreateAButton()
{
var button = new ImageButton();
button.ImageUrl = "yourimage.png";
button.ID = "Button1";
button.Click += ButtonClick;
Page.Form.Controls.Add(button);
}
private void ButtonClick(object sender, ImageClickEventArgs e)
{
// Do stuff here
// ...
}
You can use this code (one significant change) :
private void CreateAButton()
{
var button = new ImageButton();
button.ImageUrl = "yourimage.png";
button.ID = "Button1";
button.PostBackUrl = "http://www.towi.lt";
Page.Form.Controls.Add(button);
}
Trick is in "PostBackUrl". If you write correct link it will redirects to it (as in example). In other cases this will add original server name, '/' and text you entered. For example 'xxx' will be turned to "http://yourservername/xxx". It is very useful, when you working with redirects to same ISS, but different sites and dynamically creating buttons for users.

Categories

Resources