Load textBox Form title on load - c#

I want to show current form Title inside the textBox
But the code below doesnt do it,it only works if I set it on Button1_click.
And After clicking the button it will change to the form title
But I need it to set the form title in the textbox instantly on load
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;
using System.Diagnostics;
namespace ShowTitle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
textBox1.Text = currentp.MainWindowTitle;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}

This simple code demonstrate that when your event handler for Form.Load event is called there is no MainWindowTitle to read from the currentp, while if you execute the same code in the Form.Shown event handler there is a MainWindowTitle in the currentp variable
Form f;
TextBox t;
void Main()
{
f = new Form();
f.Text = "This is a test";
t = new TextBox();
f.Controls.Add(t);
f.Load += onLoad;
f.Shown += onShow;
f.Show();
}
void onLoad(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
t.Text = currentp.MainWindowTitle;
else
t.Text = "NO TITLE";
}
void onShow(object sender, EventArgs e)
{
// Uncomment these line to see the differences
// if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
// t.Text = currentp.MainWindowTitle;
// else
// t.Text = "NO TITLE";
}

Related

how to read excel data without manually telling the cells

My code when ran, opens a form and when that form opens, it opens and reads data from a excel spreadsheet. During the initial load, it reads the specific data from it's current cells. To test the theory of reading extra data, I manually re-read the data using a button and calling specific cells. Here is my code below:
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_read_data_from_excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OpenFile();
}
public void OpenFile()
{
Excel excel = new Excel(#"Test.xlsx", 1);
textBox1.Text = excel.ReadCell(1, 0);
textBox2.Text = excel.ReadCell(1, 1);
textBox3.Text = excel.ReadCell(3, 2);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Excel excel = new Excel(#"Test.xlsx", 1);
textBox1.Text = excel.ReadCell(2, 0);
textBox2.Text = excel.ReadCell(2, 1);
textBox3.Text = excel.ReadCell(4 , 2);
}
}
}
How can I get the form to automatically read the data and then every 10 seconds display the next line or cells of data? P.S. I used a button to check to see if the reading of the next line of data or cells works correctly.
For the timer functionality, try something like this:
private void InitializeTimer()
{
// Call this procedure when the application starts.
// Set to 10 seconds.
Timer1.Interval = 10000; // .Interval is in milliseconds
Timer1.Tick += new EventHandler(Timer1_Tick);
// Enable timer.
Timer1.Enabled = true;
}
private void Timer1_Tick(object Sender, EventArgs e)
{
//Call any excel code you want to run every 10 seconds here
}

how can use sender and tags together in c#?

I want develop a calculator by using c#, and I do not use method click for all button from 0 to 9 I want I have just one method and if I click the each button wrote in textbox by using sender and tags.
best regards
enter code here
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 Final
{
public partial class Form1 : Form
{
bool names;
int counter;
string name;
double ans, num;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
counter++;
again();
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
counter++;
again();
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
counter++;
again();
}
You can have just one handler for all digit buttons, and then you can extract its value like this:
int num = int.Parse(((Button)sender).Text);
This assumes that you set the Text property of the buttons to: 0,1,2..9
You can access the Tag property just like the Text:
var txt = ((Button)sender).Tag).ToString();
textBox1.Text += txt;
Set .Tag to correspondent value and then retrieve it from sender by casting it to Button type.
private void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
textBox1.Text += button.Tag.ToString();
counter++;
again();
}
public Form1()
{
InitializeComponent();
button1.Tag = 1;
button1.Click += button_Click;
button2.Tag = 2;
button2.Click += button_Click;
// and so on for other buttons
}

Writing to CSV File in C#

I am creating a clock for clocking into a business. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
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;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
String Code;
String Name;
String InOut;
Boolean Luke = true;
String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
StringBuilder Header = new StringBuilder();
StringBuilder csvData = new StringBuilder();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
Header.AppendLine("Timestamp, Name");
File.AppendAllText(csvPath, Header.ToString());
textBox1.Font = new Font("Arial", 30, FontStyle.Bold);
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Code = Code + button.Text;
textBox1.Text = Code;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
//clear
Code = null;
textBox1.Text = Code;
}
private void button10_Click(object sender, EventArgs e)
{
//in or out
DateTime timeStamp = DateTime.Now;
if (Code == "123")
{
Name = "Luke";
}
Button button = (Button)sender;
csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
File.AppendAllText(csvPath, csvData.ToString());
Code = null;
textBox1.Text = Code;
}
private void button14_Click(object sender, EventArgs e)
{
}
}
}
My layout consists of a number pad, in button, and out button. When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. When I tested the code by clocking in, the program writes one row correctly. When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. I was wondering if anyone could help me find what is going wrong in the code. Thanks.
You need to empty csvData after writing it to the file.

Change file attribute pragmatically

im kinda new to programming and As university project,i have to write a program which changes a file info like a virus and then undo the changes just like anti virus.
i wrote the code for changing attribute on read only,But what about Hidden or system file ?
and what is the way for undoing it !
where im going wrong in coding ??
Here is my main form code :
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog fDialog;
void button1_Click(object sender, EventArgs e) // Browse button
{
fDialog = new OpenFileDialog();
fDialog.Title = "Open a Text File";
fDialog.Filter = "TXT Files|*.txt|doc Files|*.doc";
fDialog.InitialDirectory = #"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(fDialog.FileName.ToString());
}
textBox1.Text = fDialog.FileName;
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)//the path showing text box
{
}
private void button2_Click(object sender, EventArgs e)//read-only button
{
fDialog.ReadOnlyChecked = true;
}
private void button3_Click(object sender, EventArgs e) //Hidden button
{
}
}
}

How can i check for text identical in textBox while typing inside the textBox?

I have this Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GatherLinks
{
public partial class ChangeLink : Form
{
public ChangeLink()
{
InitializeComponent();
}
public string getText()
{
return textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
DialogResult = DialogResult.OK;
}
else
{
}
}
private void ChangeLink_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
}
}
And this code in Form1:
public void KeysValuesUpdate()
{
DialogResult dr = DialogResult.None;
using (var w = new StreamWriter(keywords_path_file))
{
crawlLocaly1 = new CrawlLocaly(this);
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
if (FormIsClosing != true)
{
dr = crawlLocaly1.ShowDialog(this);
}
if (dr == DialogResult.OK)
{
if (LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Clear();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
else
{
LocalyKeyWords[mainUrl] = new List<string>();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
Write(w);
ClearListBox();
}
if (dr == DialogResult.Cancel)
{
Write(w);
}
if (dr == DialogResult.None)
{
Write(w);
}
}
}
This KeysValuesUpdate() function is called here:
private void button2_Click(object sender, EventArgs e)
{
cl = new ChangeLink();
cl.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = cl.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
cl.Close();
}
else if (dr == DialogResult.OK)
{
label4.Text = cl.getText();
mainUrl = cl.getText();
if (!LocalyKeyWords.ContainsKey(mainUrl))
{
newUrl = true;
KeysValuesUpdate();
}
else
{
newUrl = false;
KeysValuesUpdate();
}
OptionsDB.set_changeWebSite(cl.getText());
cl.Close();
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
}
When I click the button2 it's opening the new Form with a textbox and then inside I can type text.
Then I checking if the text inside already exist then newUrl is false or true.
Then when I click OK the OK button in the new Form then it's checking if the text I typed Contain/exist already or not.
I want that when the user type something in the textbox while he is typing if it's Contain/Exist the key then color the text in the textbox in Red one the user is keep typing and the text is not Contain/EXist color it back to Black but each time if the text in the textbox Contain/Exist already color it in Red and only if it's match case not if the text is inside other text:
This is in black:
For example : Danny hello all
But if I type in the textbox only: hello
Then the word hello will be in Red then if I kept typing after the hello then all the text in the textbox is Black if I delete the text and kept only the word hello then it will be Red again.
And that should be according to the code above and in realtime when im typing text in the textbox.
The new Form again with updated code with the textBox1 text changed event:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GatherLinks
{
public partial class ChangeLink : Form
{
Form1 f1;
public ChangeLink(Form1 f)
{
InitializeComponent();
f1 = f;
}
public string getText()
{
return textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
DialogResult = DialogResult.OK;
}
else
{
}
}
private void ChangeLink_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (f1.mainUrl.Contains(textBox1.Text))
{
textBox1.ForeColor = Color.Red;
}
else
textBox1.ForeColor = Color.Black;
}
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
if (Regex.IsMatch(yourtext, #"\b" + textBox.Text + #"\b"))
{
textBox.ForeColor = Color.Red;
}
else
textBox.ForeColor = Color.Black;
}
Place your data containing variable name at the place of yourtext.
I have edited the answer. It is perfectly matching the whole words as you asked to do. To use Regex class, include System.Text.RegularExpressions namesapce.
You can implement textBox1 TextChanged event handler simply by defining a method
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox;
String text = textBox.Text;
if (SomeCheck(text))
{
textBox.ForeColor = Color.Red;
}
else
{
textBox.ForeColor = Color.Black;
}
}
and assigning method textBox1_TextChanged to OnTextChanged property of textBox

Categories

Resources