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.
Related
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!
I'm trying to find a way to get the string name of the method call which pops up a new window. I have three button click event handlers which will open the new window but I need to know which called the .Show();
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
I don't want to have to have three separate windows! is there an opening event handler parameter which I can fetch the caller from?
well, you can simply add a public variable at MobilityPortfolioSettings class and set its value in each method, ex: in buttonSettingsPortfolio1_Click add MobilityPortfolioSettings.Variable = 1 and so on.
Here
Console.write(triggeredBy); you can output the value by logging to file or some other way . This value will indicate which path your code took.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio1_Click");
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio2_Click");
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio3_Click");
}
private Open(string triggeredBy){
Console.write(triggeredBy); // You can write to file or output in some different way here.
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
Try this:
Cast sender as button and then get it's name.
Change the MobilityPortfolioSettings constructor so that it needs a string parameter.
Pass the button name to the constructor.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
string buttonName = "";
if (sender is Button)
buttonName = ((Button)sender).Name;
Window settingsWindow = new MobilityPortfolioSettings(buttonName);
settingsWindow.Show();
}
BTW use Window as variable type instead of var.
Cheers
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 :)
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);
}
I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}