This is for a project I'm doing for class, I'm trying to create a win form that will have 2 buttons one that will increment in the text box when the button is pushed and one that will decrement when a different button is pushed. I'm having trouble finding the right line that will do what I want. Is there someone that could help me out?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project10TC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i++);
}
private void button2_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i--);
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Since its a class project, I can only give you a hint.
You need to define the variable i outside of your button click events. Use the same variable in both events.
Also look at the difference between i++ and ++i
Declare the i variable as a field. Besides I would use ++i instead of i++. Otherwise you have different values in the textbox and in the variable. Also, no need to use Convert.ToString().
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
i = 0;
}
//...
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString;
}
}
Related
I have a dotnet 2.0 app in C# that I have developed in Visual Studio 2008, it's pretty simple:
I would like to change the label name currently called "label1" to "Running" or "Stopped" when my process loop.exe is running or stopped.
When I press start, it will run loop.exe and the stop button will obviously stop it.
I've read a lot of topic on C# Winforms but I cannot get this working and I have no idea what do to now. I think that I need to add a backgroundworker but I don't know how to check the process and update the label programmatically.
Here's my clean/current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace APP_NAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("taskkill", "/F /IM loop.exe");
Process.Start("taskkill", "/F /IM azcopy.exe");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe", #"C:\ProgramData\APP_NAME\settings\settings.xml");
}
private void button4_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", #"C:\ProgramData\APP_NAME\logs");
}
}
}
I hope I made myself clear, thank you.
You can update the text shown by a label by accessing it's Text property. So, change your method like:
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
}
//hi
//Do this to see the label value changes in real time in the window
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
lable1.Refresh();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Apostolh_Texnikou
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void GN_TextChanged(object sender, EventArgs e)
{
}
private void CP_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
I want the user to click in the ready button and a window to pop out.
In the window the user must be able to copy the text.
I found how to make a pop window but its just a window which doesn't allow
copy paste. thank you in advance
You might want to look into a more complex type of dialog box. Perhaps one with a textbox? This will require launching another form. (see how to display textBox control in MessageBox?)
In Form1
private void button4_Click(object sender, EventArgs e)
{
AddText at = new AddText();
at.Show();
richTextBox2.Text = at.text;
}
In new Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class AddText : Form
{
public string text = "";
public AddText()
{
InitializeComponent();
}
private void AddText_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
text = textBox1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
When I click button1 in the new form it does assign the text from textBox1 to the variable text.
But it's not passing it to the Form1.richTextBox2.
I guess the problem is that I try to assign the text in Form1 in the button click event:
richTextBox2.Text = at.text;
But this happen before the button Click event in the new form.
Where/how in Form1 should I assign the text to richTextBox2 ?
I used ShowDialog() it's working only when I close the new form window. Only when I close it I see the text in the richTextBox2. But I want to see the text in richTextBox2 when I click the Ok(button1) button without closing the form.
Form1
private void button4_Click(object sender, EventArgs e)
{
AddText at = new AddText(this);
at.Show();
richTextBox2.Text = at.text;
}
public void SetText(string text)
{
richTextBox2.Text = text;
}
New form
public partial class AddText : Form
{
private Form1 _form1;
public AddText(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void AddText_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
_form1.SetText(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
This question already has answers here:
Send values from one form to another form
(20 answers)
Closed 7 years ago.
I am having a problem adding text from my Form 2, to the List item in Form 1. Could anyone help please, Sorry if it's a bit jumbled, I'm new with coding :) What I am trying to achieve is a to-do list application, in the 2nd form, text should be entered in a text box - when the user clicks enter, this text should be entered into the list item.
Another question is, in this case would I be better using a list item or a list box?
Here is the code:
FORM 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ToDoList
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void button2_Click(object sender, EventArgs e, string tbtext)
{
//Form1 form1 = new Form1();
//form1.listBox1.Items.Add(tbtext);
}
public void button1_Click(object sender, EventArgs e)
{
this.Close();
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
ListViewItem item = new ListViewItem(textBox1.Text);
Form1 f1 = new Form1();
f1.listView1.Items.Add(item);
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
}
}
FORM 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ToDoList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void filesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
}
public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void F1Button1_Click(object sender, EventArgs e)
{
}
private void tB1_TextChanged(object sender, EventArgs e)
{
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
You create different object Form1, and the object is destroyd after you quit button2_Click. Is it local variable.
You need to create the datasource for your TODO list and propaget it to both forms, for example by method Set DataSource. Then you have one object accesible by
both forms.
I have a windows Form Project that runs smoothly and fine on framework 4.5.1
But yet I have a project on framework 2.0 that is so laggy even after i changed the target framework to 4.5.1
so why is one project slow and the other is normal?
Update :
here is the Main From
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(" ", "About US");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Add AddForm = new Add();
AddForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Search SearchForm = new Search();
SearchForm.Show();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
and here is its design
Update 2 :
Form "Add.cs" that dose not lag .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
AddBook AddBookForm = new AddBook();
AddBookForm.Show();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
MainForm mainForm = new MainForm();
mainForm.Show();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void publisherButt_Click(object sender, EventArgs e)
{
this.Close();
AddPublishercs AddPublisherForm = new AddPublishercs();
AddPublisherForm.Show();
}
private void authorButt_Click(object sender, EventArgs e)
{
//this.Close();
//AddAuthor AddAuthorForm = new AddAuthor();
//AddAuthorForm.Show();
}
private void Add_Load(object sender, EventArgs e)
{
}
}
}
Try removing the panel1 paint eventhandler. This will get called (even tho its empty) ever time the panel has to draw itself, which is all the time in a designer environment.
Remove this code:
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
There will also be code inside of InitializeComponent() similar to this
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1);
Remove it as well. Clean and rebuild solution. Close all designer windows. Try again.