How can i make a hotkey in visual c#? - c#

So what I'm trying to do is when I press a certain key, it will send keys. This is what I have so far. When I click F1 it doesn't do anything, and I'm not sure why.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
SendKeys.Send("Example Text");
}
}

Try :
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F1")
{
MessageBox.Show("F1 is pressed");
}
}

Related

How do I specify the location where my User Control shows?

I am extremely new to C# and any help would be appreciated. I am trying to create a simple multiple option menu with User Controls and I have got it in a semi functional state. I am trying to get the user controls to show in a certain location because as of now they are only showing from the top left down and that is not where I would like them. Any help would be appreciated. My Form1 code is below:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Menu_V2
{
public partial class Form1 : Form
{
StartScreen u1;
Softaim u2;
Triggerbot u3;
Macro u4;
Misc u5;
public static bool flag = true;
public Form1()
{
u1 = new StartScreen();
u2 = new Softaim();
u3 = new Triggerbot();
u4 = new Macro();
u5 = new Misc();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label4_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Hide();
u4.Hide();
u5.Show();
u5.Dock = DockStyle.Fill;
this.Controls.Add(u5);
}
private void insert(object sender, KeyEventArgs e)
{
}
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F8)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
this.Hide();
}
}
private void bunifuThinButton21_KeyUp(object sender, KeyEventArgs e)
{
}
private void bunifuThinButton22_Click(object sender, EventArgs e)
{
}
private void bunifuThinButton22_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F8)
{
this.Show();
}
}
private void misc1_Load(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Hide();
u4.Show();
u5.Hide();
u4.Dock = DockStyle.Fill;
this.Controls.Add(u4);
}
private void button1_Click_1(object sender, EventArgs e)
{
}
private void startScreen1_Load(object sender, EventArgs e)
{
}
private void label2_Click_1(object sender, EventArgs e)
{
u1.Hide();
u2.Show();
u3.Hide();
u4.Hide();
u5.Hide();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void label3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Show();
u4.Hide();
u5.Hide();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
}
}```

Dynamic method in c#

I have many labels on the form, and every label invokes same method with different argument(which belongs to label text/name). Here is the code:
//"res" is an array
private void label1_Click(object sender, EventArgs e)
{
checkresult(res[0]);
}
private void label2_Click(object sender, EventArgs e)
{
checkresult(res[1]);
}
private void label3_Click(object sender, EventArgs e)
{
checkresult(res[2]);
}
private void label4_Click(object sender, EventArgs e)
{
checkresult(res[3]);
}
private void label5_Click(object sender, EventArgs e)
{
checkresult(res[4]);
}
private void label6_Click(object sender, EventArgs e)
{
checkresult(res[5]);
}
private void label7_Click(object sender, EventArgs e)
{
checkresult(res[6]);
}
private void label8_Click(object sender, EventArgs e)
{
checkresult(res[7]);
}
private void label9_Click(object sender, EventArgs e)
{
checkresult(res[8]);
}
I just want to precise my code by defining only one method for all labels. How can i do it?
A pseudocode may look like this:
label1.Click += label_Click(object sender, EventArgs e);
label2.Click += label_Click(object sender, EventArgs e);//SAME HANDLER
label3.Click += label_Click(object sender, EventArgs e);//SAME HANDLER
....
and after
private void label_Click(object sender, EventArgs e)
{
if(sender == label1)
checkresult(res[0]);
else if(sender == label2)
checkresult(res[1]);
...
...
}
First let all of your labels use the same Label_Click event.
private void Label_Click(object sender, EventArgs e)
{
Label temp = sender as Label;
if (temp != null)
{
string labelName = temp.Name;
string labelId = labelName.Substring(5, labelName.Length);
int id = int.Parse(labelId) - 1;
checkresult(res[id]);
}
}
You could set anonymous delegates in when you make the event handler
label1.Click += (s,e) => {checkresult(res[0]); };
label2.Click += (s,e) => {checkresult(res[1]); };
label3.Click += (s,e) => {checkresult(res[2]); };
In WinForms, set your Index to Tag of Label and set each OnClick event to same EventHandler
private void lbl_Click(object sender, EventArgs e)
{
checkresult(res[Convert.ToInt32((sender as Label).Tag)]);
}

Update Tab's Name in C# Web Browser

I'm working on a Web browser in Visual Studio 2010, but I can't update the tab's name to the website's name. For example, when you visit a website like CNN.Com, I want the tab to also say, "cnn.com". The project isn't using the default WebBrowser form, by the way. Please explain it in the simplest way possible since I'm new to C#(Just moved from C++ and Java) so I'm not familiar with working with Windows forms. Thanks. Any help is appreciated.
Here's an image of the problem: http://postimage.org/image/5ym4yx0pt/
....
public Form1()
{
InitializeComponent();
}
int i = 1;
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
//Load a tab when loading form
tabControl1.TabPages.Add("Tab");//problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab"); //problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i- 1;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void yahooSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = yahooSearchToolStripMenuItem.Text;
}
private void youtubeSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = youtubeSearchToolStripMenuItem.Text;
}
private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = googleSearchToolStripMenuItem.Text;
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//add KeyUp event for detecting 'Enter' key
//navigate to specified URL withoud pressing the 'Go' button
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
}
}
private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab");
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i - 1;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//Associate PrintPreviewDialog with PrintDocument.
printPreviewDialog1.Document = printDocument1;
// Show PrintPreview Dialog
printPreviewDialog1.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
// Bring up 'Print Dialog'
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
pageSetup.EnableMetric = false;
pageSetup.ShowDialog();
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 about = new Form2();
about.Show();
}
private void calenderToolStripMenuItem_Click(object sender, EventArgs e)
{
calenForm cal = new calenForm();
cal.Show();
}
}
}
...........
Assuming WebBrowser is the built-in WebBrowser, you can fire the OnDocumentTitleChanged event to change the tab text every time the WebBrowser document title is changed.
to do this, in the form load event, after declaring browse, start typing browse.DocumentTitleChanged += and a tooltip should come up saying 'tab to insert this code' or something along those lines. Just tab twice and Visual Studio will insert a new method for you, with a throw new NotImplementedException(); line. Delete that line and replace it with the code changing your tab's text to the browser's DocumentTitle.
If you need any more information, I suggest you check the documentation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx
Though I am confident that using events is the best solution. Events are designed to execute upon certain significant programming 'events' happening, and changing a webpage is one example of such an event. (Events are roughly C#'s equivalent of C++'s function pointers if that helps your understanding at all. Though they are more akin to a std::vector of function pointers.)
Set the HTML title tag for the page text contained within the two tags will show up in the tab that the web page is displayed in.
See the following for more about setting the title in the code behind
How to use Eval in codebehind to set Page.Title
And this link as well
http://www.asprobot.com/ASP.NET/ASPNET-Title-Tag-and-Meta-Tags

C# executing code from one button in another

I've one button - button1click and textbox in which when i type something and press enter, i'd like to run code from button1click.
How can I do it without copying entire code from button1click into EnterPressed?
private void button1click(object sender, EventArgs e)
{
//Some Code
}
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Execute code from button1
}
}
Maybe something like that? But I'm getting errors...
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1click(object sender, EventArgs e)
}
}
Just have button1_click call a method and then you can call that same method from anywhere you want to. So in your example:
private void button1click(object sender, EventArgs e)
{
Foo();
}
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Foo();
}
}
void Foo()
{
//Do Something
}
I personally wouldn't manually call another control's event.
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1click(sender, EventArgs.Empty);
}
}

.NET/C#: How to remove/minimize code clutter while 'triggering' Events

I just wanna find out if there's a way I could minimize code clutter in my application.
I have written code/s similar to this:
private void btnNext_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnNext.Opacity = 1;
}
private void btnNext_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnNext.Opacity = 0.5;
}
private void btnShowAll_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnShowAll.Opacity = 1;
}
private void btnShowAll_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnShowAll.Opacity = 0.5;
}
private void btnPrev_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnPrev.Opacity = 1;
}
private void btnPrev_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnPrev.Opacity = 0.5;
}
private void btnSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearch.Opacity = 1;
}
private void btnSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearch.Opacity = 0.5;
}
private void btnSearchStore_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearchStore.Opacity = 1;
}
private void btnSearchStore_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearchStore.Opacity = 0.5;
}
private void btnCloseSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnCloseSearch.Opacity = 1;
}
private void btnCloseSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnCloseSearch.Opacity = 0.5;
}
private void btnHome_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnHome.Opacity = 1;
}
private void btnHome_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnHome.Opacity = 0.5;
}
and so on and so forth...
Do I need to create a 'function' that will run initially? Or do I have to create another class just so I can 'organize' them?
Any suggestions?
You could rewrite all those functions into 2:
private void FadeBtn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Button btn = (Button)sender;
btn.Opacity = 1;
}
private void FadeBtn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
Button btn = (Button)sender;
btn.Opacity = 0.5;
}
And then point all of the buttons MouseEnter and MouseLeave events to those functions.
You need to have ChangeButtonOpacity method:
private void ChangeButtonOpacity(Button button, double newOpacity)
{
button.Opacity = newOpacity;
}
And you can implement your handlers as:
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
ChangeButtonOpacity((Button)sender, 1);
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
ChangeButtonOpacity((Button)sender, 0.5);
}
In this way you will need only two handlers.
Create a Mouse Enter Event and register all the buttons with it. Inside the method you will notice I cast the sender object as a button. So what ever button calls it you can perform this opacity action on.
private void ButtonMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Button button = (Button) sender;
button.Opacity = 1;
}
As far I can see, in your case you can shorten to:
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 1;
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 0.5;
}
In the designer, you can choose these event handlers then instead of creating new ones for each button.
Perhaps you can use the Tag property of the button if your not using it for anything else, Then you can do the following.
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = (double)((sender as Button).Tag);
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 0.5;
}
This would allow you to setup different values for different buttons using only two handlers.

Categories

Resources