Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a an app. Form 1 has a button which was set to open an instance of form2. I got some funny behaviour with the form reopening itself. So i deleted the code from that button on form1. Still when i click on that button, its opening the form2. Any idea??
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
Try Clean, then rebuild the project.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My application prints without showing the PrintDialog (and it need to do so) but I'd like to be able to set to which printer it should print so I added an appsetting in the app.config where it stores the printer name. what I want is a dialog where it shows all printer and the user should be able to choose a printer and it will save in the app.config (I could actually do it from the PrintDialog and get the chosen printer but the button says Print and I don't want to confuse the User...)
Thanks
edit
#methodMan Asked for code so I added My code
System.Windows.Forms.PrintDialog ps = new System.Windows.Forms.PrintDialog();
//set the selected printer in the dialog to the current printer
ps.PrinterSettings.PrinterName = MyApp.Properties.Settings.Default.ContinuesLabelPrinter;
var result = ps.ShowDialog();
if(result == System.Windows.Forms.DialogResult.OK)
{
MyApp.Properties.Settings.Default.ContinuesLabelPrinter = ps.PrinterSettings.PrinterName;
}
OK here is something that will work for you I think...Create a windows Form with a listbox and two buttons.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
listBox1.Items.Add(printer);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
In your code do this:
Form2 form2 = new Form2();
if(form2.Show() == System.Windows.Forms.DialogResult.OK)
{
MyApp.Properties.Settings.Default.ContinuesLabelPrinter = form2.listBox1.SelectedItem.ToString();
}
form2.Dispose(); // <-- this might not be necessary
You will need to make the listBox1 public for this to work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a massive problem with a project im working on. I am trying to make a WFA which will take input from user and then the user will choose to either add what they typed using a hashtable or delete something out of that hashtable by using the add and remove buttons...
Im really struggling on how to add the user input to the hashtable??
someone please help!!!
namespace Lab6_Library2 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
Hashtable books = new Hashtable();
books = textBoxInput.Text;
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void buttonView_Click(object sender, EventArgs e)
{
MessageBox.Show("All The Books Added are: \n" + textBoxInput);//+ );
}
If you'd like to keep the collection content, you have to move it to class level:
public partial class Form1 : Form
{
HashSet<string> books = new HashSet<string>();
// (...)
}
I used generic HashSet<string> here, because it's the right one to use in your case.
To add item to HashSet instance, use Add method:
private void buttonAdd_Click(object sender, EventArgs e)
{
books.Add(textBoxInput.Text);
}
To remove items, use Remove method:
books.Remove(textBoxInput.Text);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wondering if it is possible to change an inactive Form's text? The Form is Inactive because I have a MessageBox shown.
I have tried to do something like this:
private void ChangeFormText()
{
Form1 f = new Form1();
f.Text = "This doesn't work...";
}
But that doesn't work. I tried this:
private void ChangeFormText()
{
this.Text = "This still doesn't work...";
}
And this doesn't work also. I have also tried this:
Form1 form = null;
public void ChangeFormText()
{
form.Text = "And this won't work!";
}
But that throws an error.
This still doesn't work:
this.Text = "NOTHING WORKS";
Is there any way to change the Inactive Form's Text?
When the MessageBox is shown - any code in the Form won't run. And you can't put code into a MessageBox (as far as I know).
But what you can do is use a BackgroundWorker which works asynchronously.
This works:
public partial class Form1 : Form
{
BackgroundWorker w = new BackgroundWorker();
public Form1()
{
InitializeComponent();
w.DoWork += new DoWorkEventHandler(w_DoWork);
}
void w_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000);
Invoke(new Action(doit));
}
void doit()
{
Text = "Changed";
}
private void button1_Click(object sender, EventArgs e)
{
w.RunWorkerAsync();
MessageBox.Show("Random Text");
}
}
This is something you don't have to take care of. The title bar of a window already paints with distinctive colors, any Windows user is familiar with it. But you can, you have events for this:
private void Form1_Deactivate(object sender, EventArgs e) {
this.Text = "I miss you, come back soon";
}
private void Form1_Activated(object sender, EventArgs e) {
this.Text = "I'm back! What can I do to help you today?";
}
Unfortunately, not even the best intentions is going to stop that from being repetitive and annoying. Don't tell the user what he already knows and expects. Only tell him about the surprises.
If you want to change Text before calling MessageBox.Show(...), just do it normally. If you want to change Text after calling MessageBox.Show(...) you can use BeginInvoke to show the message box like this:
BeginInvoke((Action)(() => { MessageBox.Show("OK"); }));
Text = "????";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a button that when pressed is currently outputting messages to a messagebox. Its not very tidy so I created a new form and placed a text box in there. While the 2nd form picks up the data it presents it in another messagebox rather then the textbox like intended. Help is appreciated.
Form2
public Form2(string strTextBox)
{
InitializeComponent();
textBox1.Text = strTextBox;
}
Form 1
private void SaveButton_Click(object sender, EventArgs e)
{
foreach (string error in errorSet)
{
Form2 frm = new Form2(error);
frm.Show();
}
}
There is some more logic in the button_click if it looks a little strange but its pretty redundant for the problem im having.
Thank you
You should loop over your errors collection and store them in a StringBuilder, then, show the Form2 only one time outside the loop (if there is at least an error).
Do not forget to make your textbox MultiLine=true, give it enough height and a vertical scroll bar.
private void SaveButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string error in errorSet)
sb.AppendLine(error);
if(sb.Lenght> 0)
{
Form2 frm = new Form2(sb.ToString());
frm.Show();
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have 2 forms .. form1 and form2 .. I have button1 on form1 that loads form2 but every time I click button1 it loads new instance of form2
I want button1 to bring to front form2 if it was open and restore it if it was minimized
This should work (haven't tested it though)
public static bool _Invoked;
Form2 f2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
if (!_Invoked)
{
_Invoked = true;
f2.Show();
}
else if (_Invoked)
{
f2.BringToFront();
_Invoked = false;
}
}
Add a comment for further clarification
EDIT:
Just tested this and its working
Form2 f2 = new Form2();
bool _Clickone = false;
private void button1_Click(object sender, EventArgs e)
{
if (!_Clickone)
{
_Clickone = true;
f2.Show();
}
else
{
f2.WindowState = FormWindowState.Normal;
f2.ShowInTaskbar = true;
f2.BringToFront();
}
}
Then handle the Form Closing event of the second for m
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
You can do this by creating an onClose event in your form2, where you cancel the close, and set the visibility to hidden.
Then instead of creating a new instance, set the visibility to visible again.