Can you change an inactive Form's text? [closed] - c#

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 = "????";

Related

Set a printer for app using a Dialog [closed]

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.

creating a new button or making it appear in an if statement in C# [closed]

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'm using c# 2008 and i want to know if its possible to create(or display) a new button in an if statement after something happened. eg. if a certain label displays text, then a button must be created. If anyone can help, it will greatly be appreciated.
This code show you how to create and display a new button when double click mouse on the form:
public partial class Form1 : Form
{
private Button button1 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (button1 == null)
{
button1 = new Button();
button1.Text = "New Button";
button1.Location = new System.Drawing.Point(10, 10);
button1.Size = new System.Drawing.Size(150, 30);
button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked.");
}
}
Note: this.Controls.Add(button1); add the button1 to the Form1. You also using this Controls property of other control to add a control to another control.
See more details:
http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.control.controls(v=vs.100).aspx

c# prevent duplicating forms [closed]

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.

Navigation and memorising text box data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The program has a panel which holds a text box and the panel has two buttons on each side.
Each button acts as a 'next' (>>) and 'previous' (<<) navigation. I want to be able to navigate to the next panel by clicking '>>' this will clear the text box. Then when I click '<<' I want to go back to the previous panel with the text box containing the data previously added. However I want to do this without having to create two panels on top of each other and setting the visibility to true or false (which I am able to do). I want to achieve this by using only the one panel so the process can be done an infinite number of times. I hope this is clear to understand if you require more information please let me know.
Here is an image of my interface to clarify things:
since you have the page number, why not just create a list (or use a dictionary with the page number as a key), then in the button handler for >> and << collect the text for the current page (and put it in the list or dictionary) and replace it with the text for the previous page (from the list or dictionary).
code could look something like this:
public partial class Form1 : Form
{
Dictionary<Decimal, String> TextInfo;
public Form1()
{
InitializeComponent();
TextInfo= new Dictionary<Decimal, String>();
}
private void Form1_Load(object sender, EventArgs e)
{
numPage.Value = 1;
}
private void bnForward_Click(object sender, EventArgs e)
{
if (TextInfo.ContainsKey(numPage.Value))
{
TextInfo[numPage.Value] = textBox1.Text;
}
else
{
TextInfo.Add(numPage.Value, textBox1.Text);
}
numPage.Value++;
if (TextInfo.ContainsKey(numPage.Value))
{
textBox1.Text = TextInfo[numPage.Value];
}
else
{
textBox1.Text = "";
}
}
private void bnBack_Click(object sender, EventArgs e)
{
if (numPage.Value == 1)
return;
if (TextInfo.ContainsKey(numPage.Value))
{
TextInfo[numPage.Value] = textBox1.Text;
}
else
{
TextInfo.Add(numPage.Value, textBox1.Text);
}
numPage.Value--;
if (TextInfo.ContainsKey(numPage.Value))
{
textBox1.Text = TextInfo[numPage.Value];
}
else
{
textBox1.Text = "";
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
}

How to hide a form from the main form

I have been tweaking my program all day and I am having a problem hiding a form which will pop up saying "Please wait"
For example:
private void button12_Click(object sender, EventArgs e)
{
form2 wait = new form2();
pw.Show();
}
private void button13_Click(object sender, EventArgs e)
{
form2 wait = new form2();
pw.Hide();
}
This will not work, although I am sure this isn't news to the casual C# programmer. Is there a simple way to do what I am attempting? I have tried searching online and I did find something although I wasn't 100% sure what they were trying to do. I was going to find an example to show you but I closed page - Typical. However I think they were trying to overide the show and give you control over the .show with a bool?
The code isn't working as you expect it to because the form2 inside of button12_Click is different from the form2 inside of button13_click. Notice that you are using the new keyword twice. So in button13_click, you are creating a new form2, and then hiding it, even though you haven't even shown it yet!
Instead you can create a single form2 instance to share between your two methods:
//define this code outside both of the methods below
form2 _waitForm = new form2();
private void button12_Click(object sender, EventArgs e)
{
_waitForm.Show();
}
private void button13_Click(object sender, EventArgs e)
{
//this will hide the same form2 that was shown in button12_Click
_waitForm.Hide();
}

Categories