c# prevent duplicating forms [closed] - c#

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.

Related

Dissapear text in textbox 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 made a textbox of name tbFirstNumber
When i enter any value in that textbox , i want that Value to dissapear
I am using C# window form application in visual studio 2008
private void tbFirstNumber_TextChanged(object sender, EventArgs e)
{
}
tbFirstNumber.Text = "";
Just set it to empty on text changed event.
If you want that user is not allowed to enter any text, you can make the textbox as read only.
Set the TextBox control's ReadOnly property to true.
tbFirstNumber.ReadOnly = true;
Another way would be to hook in to the KeyPress event.
private void tbFirstNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Stop the character from being entered into the control
e.Handled = true;
}
private void tbFirstNumber_TextChanged(object sender, EventArgs e)
{
tbFirstNumber.Text = "";
}
Clear the value of the text box
this.tbFirstNumber.Text = ""

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

Can you change an inactive Form's text? [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 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 = "????";

Passing data into a separate form [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 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();
}
}

Making text box visible/unvisible c# [closed]

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 8 years ago.
Improve this question
I'm working on a windows form application and I have a button and a textbox in it.
When the button is pressed, it should make the textbox visible and hidden.
myTextbox.Visible = !myTextbox.Visible;
Did you try Google?
textBox1.Visible = false;
You can toggle the visibility by doing:
if(textBox1.Visible == true)
textBox1.Visible = false;
else
textBox1.Visible = true;
WinForm:
private void button1_Click(object sender, System.EventArgs e)
{
textBox.Visible = !textBox.Visible;
}
WPF:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox.Visibility != System.Windows.Visibility.Hidden)
textBox.Visibility = System.Windows.Visibility.Hidden;
else
textBox.Visibility = System.Windows.Visibility.Visible;
}
You can find an example here
private void button1_Click(object sender, System.EventArgs e)
{
/* If the CTRL key is pressed when the
* control is clicked, hide the control. */
if(Control.ModifierKeys == Keys.Control)
{
((Control)sender).Hide();
}
}
textbox.visible=true;
you should try this on buttonClick event

Categories