I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character
Related
I have a function that displays numbers using a while loop but I want to stop execution of a while loop at random variable value using c# by clicking a button.
For Example:
private void FrqSent()
{
int i = 1;
while (i <= 5)
{
i = i + 1;
}
}
Here is a quick example on how you can use a Backgroundworker to accomplish your task:
public partial class Form1 : Form
{
private int i = 1;
public Form1()
{
InitializeComponent();
}
private void FrqSent()
{
while (i <= 500000000)
{
if (backgroundWorker1.CancellationPending)
{
break;
}
else
{
i = i + 1;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FrqSent();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(i.ToString());
}
}
Just create a new Windows-Forms Project and add a backgroundworker object aswell as 2 buttons. You have to set the DoWork, RunWorkerCompleted and Click events manually.
Edit: Do not forget to set the BackgroundWorker`s WorkerSupportsCancellation property to true.
not very elegant but simple
public partial class Form1 : Form
{
private bool _stop;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
_stop = false;
FrqSent();
}
private void buttonStop_Click(object sender, EventArgs e)
{
_stop = true;
}
private void FrqSent()
{
int i = 1;
while (i <= 5000000 && !_stop)
{
i = i + 1;
Application.DoEvents();
}
}
}
I am creating a To Do List using Windows Forms.
This is what I have so far.
The code for this form is as follows
namespace To_Do_List
{
public partial class To_Do_List : Form
{
public To_Do_List()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) //this is the exit button on the toolstrip
{
Application.Exit(); //exits the program when the Exit button is clicked in dropdown menu
}
private void button4_Click(object sender, EventArgs e)//This creates the button to open the about form
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form
}
private void button3_Click(object sender, EventArgs e) //This creates the button to open the form which ammends tasks
{
AmmendItem CreateForm = new AmmendItem(this);
CreateForm.Show();
}
private void button1_Click(object sender, EventArgs e) // This creates the button to open the form which creates new tasks
{
AddItem CreateForm = new AddItem(this);
CreateForm.Show();
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e) // This creates the table
{
}
private void button2_Click(object sender, EventArgs e) //This Allows the user to delete entries
{
if (listView1.SelectedItems != null)
{
var confirmation = MessageBox.Show(
"Are you sure you want to delete this?",
"WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Question
);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.Items.Count - 1; i >= 0; i--)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
}
}
}
}
}
To add a task, the form looks like this
And again, the code is as follows
namespace To_Do_List
{
public partial class AddItem : Form
{
To_Do_List home;
public AddItem(To_Do_List parent)
{
InitializeComponent();
home = parent;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void openListToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
{
}
public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)//Title Box
{
}
public void /*Description of Task*/richTextBox1_TextChanged(object sender, EventArgs e)
{
}
public void /*Priority Box*/comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close(); //causes the window to close but keeps the application running
}
private void aboutToDoListToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form displaying copyright information
}
public void button1_Click(object sender, EventArgs e) //create task button
{
ListViewItem item1 = new ListViewItem(Title.Text);
item1.SubItems.Add(Description.Text);
item1.SubItems.Add(Priority.Text);
item1.SubItems.Add(Date.Text);
string value = "";
bool isChecked = Completed.Checked;
if (isChecked)
item1.SubItems.Add(Completed.Text);
else
value = null;
home.listView1.Items.Add(item1);
this.Close();
}
private void Date_ValueChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Closes the window
{
this.Close();
}
}
}
The Amend form is exactly the same as the New Task form. How would I go about being able to select a record, pressing the amend button and actually changing?
I'm pretty stumped.
Also, this isn't really part of this question but I'll ask it just in case someone knows.
How would I go about being able to actually save these records so that they are there when I open the application back up again?
Thank you very much for reading, I know that I have just dumped everything but I'm really new to Windows Forms so I didn't want to accidentally miss something important out.
Edit
Am I on the right road with something like this?
public void button1_Click(object sender, EventArgs e)
{
To_Do_List editform = new To_Do_List(Title.Text);
editform.Description.Text = listView1.SelectedItems[0].SubItems[0].Text;
Still can't get it to work :/
This code for popup window.
public partial class frmToDoDetails : Form
{
public string TaskTitle { get; set; }
public string Description { get; set; }
public bool EditMode { get; set; }
public frmToDoDetails()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
TaskTitle = txtTitle.Text;
Description = txtDesc.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void frmToDoDetails_Load(object sender, EventArgs e)
{
if (EditMode)
{
txtTitle.Text = TaskTitle;
txtDesc.Text = Description;
}
else {
txtTitle.Text = string.Empty;
txtDesc.Text = string.Empty;
}
txtTitle.Focus();
}
}
And this for list
public partial class frmToDo : Form
{
public frmToDo()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
frmToDoDetails frm = new frmToDoDetails();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] arr = new string[2];
ListViewItem itm;
arr[0] = frm.TaskTitle;
arr[1] = frm.Description;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
private void frmToDo_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("Title", 100);
listView1.Columns.Add("Desc", 70);
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
ListViewItem currentItem= listView1.SelectedItems[0];
frmToDoDetails frm = new frmToDoDetails();
frm.TaskTitle = currentItem.SubItems[0].Text;
frm.Description = currentItem.SubItems[1].Text;
frm.EditMode = true;
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentItem.SubItems[0].Text=frm.TaskTitle;
currentItem.SubItems[1].Text=frm.Description;
}
}
}
I am not added your complete fields(priority, date,etc..).
I hope it will help you.
I have used Backgroundworkerclass to update my user interface element (Label). Please! Check my code below. It is working fine. But I am trying to replace Backgroundworkerclass and use simple worker thread to achieve the same goal. How can I achieve that? Any suggestion will be appreciated. Thanks
public void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
backgroundWorker1.RunWorkerAsync();
else
label1.Text = "Busy Processing";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
delegate void DelegateDoWork(int number);
private void button1_Click(object sender, EventArgs e)
{
new Thread(DoWork).Start();
}
public void ProgressBar(int i)
{
if (label1.InvokeRequired)
{
var d = new DelegateDoWork(ProgressBar);
this.Invoke(d, i);
}
else
label1.Text = i.ToString();
}
public void DoWork()
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
ProgressBar(i);
}
}
What is happening is, even though the questionNr is set to 1, it's not changing the .Text properties of ans1-4, as well as the questionLabel. Any help would be appreciated. Also as a sub-question, is it possible to do something along the lines of if(ans1.Clicked = true)?
public partial class Form1 : Form
{
int pointCounter = 0;
private SoundPlayer _soundPlayer;
int questionNr = 1;
public Form1()
{
InitializeComponent();
_soundPlayer = new SoundPlayer("song.wav");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.amazon.com/Chuck-Seasons-One-Five-Blu-ray/dp/B007AFS0N2");
}
private void Form1_Load(object sender, EventArgs e)
{
_soundPlayer.PlayLooping();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void muteButton_Click(object sender, EventArgs e)
{
if (muteButton.Text == "Mute")
{
muteButton.Text = "Unmute";
_soundPlayer.Stop();
}
else
{
muteButton.Text = "Mute";
_soundPlayer.PlayLooping();
}
}
private void playButton_Click(object sender, EventArgs e)
{
ans1.Visible = true;
ans2.Visible = true;
ans3.Visible = true;
ans4.Visible = true;
playButton.Visible = false;
}
public void question()
{
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "Charles Irving Bartowski";
ans2.Text = "Charles Richard Bartowski";
ans3.Text = "Charles Luke Bartowski";
ans4.Text = "Zachary Strahovski";
}
}
private void ans1_Click(object sender, EventArgs e)
{
}
private void ans2_Click(object sender, EventArgs e)
{
}
private void ans3_Click(object sender, EventArgs e)
{
}
private void ans4_Click(object sender, EventArgs e)
{
}
}
}
Form where you invoke the question() method. First call that method from where you need.
eg: FormLoad/Button click etc..Then try
public Form1()
{
InitializeComponent();
_soundPlayer = new SoundPlayer("song.wav");
question();
}
It's good if you put a break point in your Form Load event and see how your code executed.Then you'll get an idea about the flow of your code.
I would like to have a progress bar, where the value of the bar would rise if the button was being pressed down(MouseDown Event), the bar needs to rise simultaneousely.
Any ideas? I tried with a timer but this is all I have currently
private void button1_Click(object sender, EventArgs e)
{
progressBar1.PerformStep();
progressBar1.UseWaitCursor = true;
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
}
public void button1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
public void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
It should look like this:
void timer_Tick(object sender, EventArgs e) {
progressBar1.PerformStep();
}
void button1_MouseDown(object sender, MouseEventArgs e) {
timer.Start();
}
void button1_MouseUp(object sender, MouseEventArgs e) {
timer.Stop();
}
The following program will update the progress bar each 20 ms the mouse is held down and get 10 100% after 2 seconds
public partial class Form1 : Form
{
private Timer pbTimer;
private int pbProgress = 0;
public Form1()
{
InitializeComponent();
pbTimer = new Timer();
pbTimer.Tick += new EventHandler(ProgressUpdate);
pbTimer.Interval = 20;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
}
private void ProgressUpdate(object sender, EventArgs e)
{
if (pbProgress < 100)
{
progressBar1.Value = ++pbProgress;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
pbTimer.Start();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
pbTimer.Stop();
progressBar1.Value = 0;
pbProgress = 0;
}