Saving values from datagridview - c#

I am new to c#. I have been trying to add values from my datagrid to mysql but in vain and i really need your help. The values are automatically generated after a time interval of 1 sec and i need to insert them to the mysql in the same time interval. Here are the codes.
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;
using System.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace BLIND_SHOPPING_SYSTEM
{
public partial class Form2 : Form
{
private DataTable m_TagDataTable = new DataTable("Tag List");
long passiveID = 0;
public Form2()
{
InitializeComponent();
this.m_TagDataTable.Columns.Add("TagID");
}
private void btn_GenerateID_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
public void timer1_Tick(object sender, EventArgs e)
{
if (passiveID < 10)
{
passiveID = passiveID + 1;
//listBox1.Items.Add(passiveID);
//this.m_TagDataTable.Columns.Add("TagID");
this.dataGridView1.DataSource = m_TagDataTable;
DataRow dr = m_TagDataTable.NewRow();
dr["TagID"] = passiveID;
m_TagDataTable.Rows.Add(dr);
}
else
// Application.Exit();
Console.ReadLine();
}
private void btn_Stop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
}
}
Please kindly help me.

You probably want to use a DataTableAdapter and specify the Update command, see here for some examples ;) http://msdn.microsoft.com/en-us/library/ms233819(v=vs.80).aspx

Related

I currently have this code to print labels, but now I want to add a loop or function to control how many copies the label prints

For this I have a Text Box, that would be the amount of copies that are going to be printed, that is to say if the user puts 2, 2 would be printed and so on.
But I can't figure out how to take the number typed by the user in the textbox and use it in the for condition, I tried to put For(int i =1;i>1;i++) before the If but it generates an infinite loop of the print box and still only prints one copy.
I use this code, and the class RawPrinterHelper
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Código_zpl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string x = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName,x);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
I do not have much reputation to comment on the conversation between you and Trix. But I would have suggested you created a method that accepts a parameter. that when calling, you can pass the text.
void printcopies(int numberofcopies){
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
pd.PrinterSettings.Copies = numberofcopies;
....
}
}
....

Adding Page Numbering in a Word document C# starting from given Page number

It's been 8 hours since i'm stuck here. I want ask user to input a page number and starting from the given page it should add page numbering till the .docx file.
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;
using Word = Microsoft.Office.Interop.Word;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string fileName;
public Form1()
{
InitializeComponent();
}
private void minimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void Close_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Word Documents|*.docx";
dlg.ShowDialog();
fileName = dlg.FileName;
textBox1.Text = fileName;
textBox1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
On button2_click i want to update the file by using data from a textBox2, can anybody complete it?
I won't complete it, but I will point you in the right direction. You have to have a reference to Microsoft Word. What you are trying to do is typically referred to as automation or more specifically Microsoft Word Automation.
You can google that and find examples or try this location for starting point.
https://learn.microsoft.com/en-us/previous-versions/office/troubleshoot/office-developer/automate-word-create-file-using-visual-c

How can I save settings to a text file and read them back?

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;
using System.IO;
namespace StopwatchTimer
{
public partial class Form1 : Form
{
public string settingsPath = "Settings";
private string settingsFileName = "settings.txt";
private static readonly Stopwatch watch = new Stopwatch();
private long diff = 0, previousTicks = 0, ticksDisplayed = 0;
public Form1()
{
InitializeComponent();
richTextBox1.TabStop = false;
richTextBox1.ReadOnly = true;
richTextBox1.BackColor = Color.White;
richTextBox1.Cursor = Cursors.Arrow;
richTextBox1.Enter += RichTextBox1_Enter;
settingsPath = Path.Combine(Path.GetDirectoryName(Application.LocalUserAppDataPath), settingsPath);
if (!Directory.Exists(settingsPath))
Directory.CreateDirectory(settingsPath);
settingsFileName = Path.Combine(settingsPath, settingsFileName);
if (!File.Exists(settingsFileName))
File.Create(settingsFileName);
string[] settings = File.ReadAllText(settingsFileName).Split(',');
if(settings.Length > 0)
{
}
} radioButton1.Checked = true;
}
I didn't write yet to the text file. I want for example to write to the text file the radioButton1 and radioButton2 states somewhere else in the code and then reading the states back when running the application.
So I'm using Split with ',' but how do I check if the value when reading back is belong to the radioButton1 or radioButton2 or maybe a textBox or a button1 ?
You can use the built in winforms settings:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Username = txtUsername.Text;
Properties.Settings.Default.Password = txtPassword.Text;
Properties.Settings.Default.Save();
}
C# Setting load and save
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp

How to make a customized progressbar in c#

I would like to learn how to make a progressbar like the one shown in this video.
I tried to replicate it in VS C#, but I get the error:
C# Property or indexer cannot be assigned to -- it is read only
If I try using if (txProgressBar.Text.Length == 85), I will get this in the TextBox (txProgressBar)
System.Windows.Forms.TextBox, Text: System.Windows.Forms.TextBox, Text: Syst...██
Textbox Progressbar Tutorial VB 2010
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 CustomizedProgressBar
{
public partial class Form1 : Form
{
int last = 1;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (txProgressBar.Text.Length = "85")
{
timer1.Stop();
MessageBox.Show("Counted!");
}else
{
if (last == 1)
{
txProgressBar.Text = txProgressBar + "█";
last = 2;
}
else
{
txProgressBar.Text = txProgressBar.Text + "█";
last = 1;
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txProgressBar.Text = "";
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
}
}
Your line:
txProgressBar.Text = txProgressBar + "█";
should be
txProgressBar.Text = txProgressBar.Text + "█"; or txProgressBar.Text &= "█";
I realized what was the problem. The txProgressBar.Text = txProgressBar + "█"; was missing the *.Text. That solved the problem and the message is shown as well.

Replace characters in string with another character and switch back

Quick note - I am very new to c# so I apologize if this is stupid simple.
I am having a hard time trying to complete a simple c# task in a book.
Pseudocode-
Text box text = user input
if button one is clicked
replace all capital letters in the text box with an asterisk
else if button two is clicked
replace the asterisks with their original characters (back to normal)
Here is what I have so far
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.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new System.EventHandler(ClickedButton);
button2.Click += new System.EventHandler(ClickedButton);
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void ClickedButton(object sender, EventArgs e)
{
string orignalText = textBox1.Text;
if (sender == button1)
{
string replaced = Regex.Replace(orignalText, #"[A-Z]", "*");
textBox1.Text = (replaced);
}
else if (sender == button2)
{
textBox1.Text = (orignalText);
}
}
}
}
The problem is that button2 is showing the text with the asterisks. It should be showing (I want it to show) the original characters.
The originalText should be a class field instead of a local variable. Also you should not store a textbox's value in case if someone clicked on the button2. Try to replace your ClickedButton method with this:
string orignalText;
public void ClickedButton(object sender, EventArgs e)
{
if (sender == button1)
{
orignalText = textBox1.Text;
string replaced = Regex.Replace(orignalText, #"[A-Z]", "*");
textBox1.Text = replaced;
}
else if (sender == button2)
{
textBox1.Text = orignalText;
}
}
You have 2 issues. First, you're setting the originalText before knowing which button was pressed. Second, originalText is a local variable, so when you want to replace it back in, it won't contain the original text anymore.
You just need to globalize originaltext variable and move the line
string orignalText = textBox1.Text;
into the first if check.
try this:
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.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new System.EventHandler(ClickedButton);
button2.Click += new System.EventHandler(ClickedButton);
}
private void Form1_Load(object sender, EventArgs e)
{
}
string orignalText = null; //you should define this var outside of ClickedButton method to keep its value during button1/2 clicks
public void ClickedButton(object sender, EventArgs e)
{
if (sender == button1)
{
orignalText = textBox1.Text;
string replaced = Regex.Replace(orignalText, #"[A-Z]", "*");
textBox1.Text = replaced;
}
else if (sender == button2)
{
if (orignalText != null) textBox1.Text = orignalText;
}
}
}
}

Categories