this is my code:
public RegForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label10_Click(object sender, EventArgs e)
{
}
private void label28_Click(object sender, EventArgs e)
{
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void label29_Click(object sender, EventArgs e)
{
}
private void label30_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
I would like to remove all the unnecessary lines of code for the labels that just came up out of nowhere for no reason and do absolutely nothing. But when I delete them - it's all errors. I'm a beginner, so please go easy on me. Thank you in advance.
To remove those empty event handlers:
Switch to the designer view,
Select each label, one at a time,
Look at the properties window (press F4 if it's not visible), and check out the events view
Find the click event, right-click and choose "reset".
Be careful when you're selecting your different controls in the designer.
If you double-click by accident, you end up creating an empty event handler for whatever the default event is for a particular control. In the case of a Label, it happens to be the Click event.
This are auto generated events which are created when you double click
on control or some other action is performed on the controls in the
design view.
Remove the lines from code behind and also remove them from *.designer.cs file.
IF you build the application after the removing of the lines and check errors: You can click on every error and it will leads you to the place which should be deleted !
If those are unnecessary then just go ahead and delete them from your code behind *.cs file. You will also have to delete the event registration from the corresponding *.designer.cs file and because of the same thing you are getting error (that you are not removing the corresponding event registration from designer file).
You can as well do the same from Designer window by select the control -> press F4 -> go to the event pane by clicking the lighting bolt icon -> remove the event registration.
Related
So this is a fairly straightforward thing, and I am just curious if there is a better way to do it to save lines of code. For class we are making a teletype machine. Basically there is a textbox, and a series of buttons A-Z and 0-9. When you click the button it adds the corresponding letter/number to the textbox. When you click send, it adds the contents of the textbox to a label and resets the textbox. Everything works and it only took a few minutes to build. However there is a mess of redundant lines and I was curious if there is a way to clean up the code with a method.
This is my current code.
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = box_UserInput.Text + "A";
}
As you can see, it is very simplistic and straight forward. Click A, and "A" gets added to the textbox. However the Text property of the button is also just "A" and I want to know if there is a way to just copy the text property of that button and add it to the textbox string.
Something like this, except with a universal approach where instead of having to specify btn_A it just inherits which button to copy based on the button clicked. That way I can use the same line of code on every button.
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = box_UserInput.Text + btn_A.Text;
}
You can use this which is more universal as the Control class contains the Text property. Also, using the best practice $"".
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
}
You can also assign the same event to each button. Create an event, say addControlTextOnClick and assign the same event to each button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addControlTextOnClick(object sender, EventArgs e)
{
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
}
}
You can even shorten this more using this C# construct:
private void addControlTextOnClick(object sender, EventArgs e) =>
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
I am trying to use windows form application to automate a few browser (internet explorer) tasks.
Here I am trying to select a value from the drop-down list available, which I have achieved by using webbrowser1.set attribute("","") option.
But once I have selected the option by this way the on change event of that drop down text box is not getting triggered whereas when I go back and select options manually its working fine.
Hence, here the on change event is not being triggered if I am using the set attribute option, so can someone suggest how I can select the required value and still trigger the onchange event.
Below is the snippet :
private void button4_Click(object sender, EventArgs e) {
webBrowser1.Document
.GetElementById("ctl00$MainContentPlaceHolder$ddl_Reports")
.SetAttribute("value", "EmployeeInformationReport");
}
Please suggest?
You should attach to the eventhandler with the AttachEventHandler api of the document after the DocumentCompleted event:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.AttachEventHandler("nameoftheevent", eventHandler);
}
private void eventHandler(object sender, EventArgs e)
{
//Do whatever you want
}
If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.
I insert tab-control in my windows application.It has 4 tab-pages.I wanted to show only relevant tab-page when I click the relevant button..my code as follows
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage1.Hide();
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
first every tab-page removed when form load
Here is the code for button click and I coded for 4 buttons...
private void button2_Click(object sender, EventArgs e){
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
I again used ....
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
these code. if another tabpage is opened when I click the button it should be remove and show relevant tabpage.Its working.
My problem is.. if I click the same button again and again same tabpages adding continuously
Can anyone give me a solution for it..
I founded a one way...In button click I modify like this.
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
if (tabControl1.TabPages.Count <= 1)
{
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
}
first code is tabControl1.TabPages.Remove(tabPage1).Then if tab-page is open It removed.If statement responsible for when button click relevant tab page is opened.then always show only one tab-page for button click moment
I'm trying to define MouseEventHandlers such that the application will exit whenever the mouse is clicked or moved or whenever a key is pressed. This is my first time using C#, but based on what I found online, I've written the code as follows:
MouseDown += new MouseEventHandler(mouseClickedResponse);
MouseMove += new MouseEventHandler(mouseMovedResponse);
KeyDown += new KeyEventHandler(keyResponse);
which connects to:
private void keyResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseClickedResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseMovedResponse(object sender, EventArgs e)
{
if (firstCall) //Keeps the application from exiting immediately
firstCall = false;
else Application.Exit();
}
The problem that I'm finding is that while the KeyEventHandler works perfectly, I can move and click the mouse as much as I want to no avail.
This is the sum total of the code that I've written to allow for user control; am I missing something?
On the surface, everything looks good with your code.
One possibility - The MouseEventHandler is defined in both the System.Windows.Input (MSDN) namespace as well as the System.Windows.Forms namespace (MSDN).
I believe the one you want is the one in the Forms namespace. Is it possible that you're using the one from the Input namespace instead?
I fixed my problem--my Form was filled with Panels, and by moving the code for mouse input over to the panels, everything worked instantly.
Change:
private void mouseClickedResponse(object sender, EventArgs e)
to:
private void mouseClickedResponse(object sender, MouseEventArgs e)
It should now work fine.