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!
Related
I'm creating Buttons programmatically with a method and am wanting to attach a Click event handler. However, that data currently comes from a string parameter which can't be used with += RoutedEventHandler.
public Button CreateButton(string Display, string Name, string ClickEventHandler)
{
Button Btn = new Button
{
Content = Display,
Name = "Btn_" + Name
};
Btn.Click += new RoutedEventHandler(ClickEventHandler);
return Btn;
}
void Btn_save_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
// later
Button MyButton = CreateButton("Save", "save", "Btn_save_Click");
Error is RoutedEventHandler expects a Method and not a String. Is there a different approach to programmatically binding events that allows this sort of behaviour?
Thanks
From what I understand you wish to pass the method that should be executed when Click event is triggered. You could do something along the lines of:
Button button = CreateButton("Save", "save", (s, e) => SomeOnClickEvent(s, e));
Button button2 = CreateButton("Create", "create", (s, e) => SomeOtherOnClickEvent(s, e));
public Button CreateButton(string display, string name, Action<object, EventArgs> click)
{
Button b = new Button()
{
Content = display,
Name = $"Btn_{name}"
};
b.Click += new EventHandler(click);
return b;
}
void SomeOnClickEvent(object sender, EventArgs e)
{
}
void SomeOtherOnClickEvent(object sender, EventArgs e)
{
}
I am not entirely sure what you are trying to accomplish with this.
Here is an example of how to create an event at run time.
public void CreateButton()
{
Button Btn = new Button();
Btn.Click += new EventHandler(btn_Clicked);
}
private void btn_Clicked(object sender, EventArgs e)
{
// Your Logic here
}
I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.
I have a form with a tabControl and inside of each tab is a flowLayoutPanel where I can drag and drop files and a button is created for each dropped file. Afterwards when I click on a button, the file that i dropped should open. I have managed to do this for one file only.. My problem is how can I tell which button was clicked and to open the file/app stored in the path for each button.. How can I differentiate in the button_click event the clicked button and the path of the app to open?
Code for this part so far:
Process myProcess = new Process();
string path_app;
public Form1()
{
InitializeComponent();
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
}
}
private void button_Click(object sender, System.EventArgs e)
{
myProcess.StartInfo.FileName =path_app;
myProcess.Start();
}
Also my tabControl has the possibility to add new tabs but how can I get the selected tab and the inside flowLayoutPanel to know where to create the button?
And by the way, is there a problem of how I open the files? I understood that i have to take into consideration the working directory..
Thank you for your help!
You can utilize Tag property of the Button:
void Form1_DragDrop(object sender, DragEventArgs e)
{
foreach (String s e.Data.GetData(DataFormats.FileDrop))
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
// Add to Tag any data you want to pin to the button
button.Tag = path_app;
}
}
private void button_Click(object sender, System.EventArgs e)
{
// Obtain via Tag
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
You could use button.Tag = "theFancyPath" and in the EventHandler cast the object sender as Button to access the Tag property.
If you need more then you could inherit from Button:
public class ButtonWithPathProperty : Button
{
public FileInfo PathToOpen { get; private set; }
public ButtonWithPathProperty(FileInfo path)
{
PathToOpen = path;
this.Click += new EventHandler(this.button_Click);
}
private void button_Click(object sender, System.EventArgs e)
{
var yourPath = this.PathToOpen;
}
}
This is not tested btw :)
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 have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}