File is created but information wont save in it - c#

Ok, so I'm new to C# and don't know what I'm doing wrong.
At the top of the button click event I have this variable:
InformationDump infodump;
infodump = new InformationDump();
After that I have the rest of the code which is meant to path to the user desktop and save whatever is filled out in the textbox's into a separate window (part of the same program):
infodump.richTextBox1.Text = textBox1.Text + ", " + textBox2.Text + ", " + textBox3.Text + ", " + textBox4.Text + ", " + comboBox1.SelectedItem;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = path + "\\" + DateTime.Now.ToString("HH.mm.ss") + System.Environment.UserName + ".txt";
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
Thanks in advance.

This is happening because you are creating an instance of the form (InformationDump) then try to use the RichTextBox instance but the form then will not go through the standard loading and initialization process therefore as I reproduced, the file will be created but will be empty.
It is very interesting, but if you do repeated save it works!
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
// zero bytes
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
// works!

Related

Output showing System.Windows.Form.Label after every word

I am having a problem where the user enters his inputs into a text box (like a name). If the name is Jim Bob it will output like Jim System.Windows.Form.Label Bob System.Windows.Form.Label. I searched the properties tab and I am following along from the book but they do not have the same problem I have. I added some code below but I don't think it has anything to do with the problem.
string output;
output = textBoxPrefT.Text + ". " + textBoxFirstN + " " + textBoxMiddleN + " " + textBoxLastN;
labelOutput.Text = output;
You have to include .Text for all textboxes so it should look like this
string output;
output = textBoxPrefT.Text + ". " + textBoxFirstN.Text + " " + textBoxMiddleN.Text + " " + textBoxLastN.Text;
labelOutput.Text = output;

Create a QR code from multiple textboxes and decode it back into the textboxes

I have created an application. This app contains the five textboxes id, name, surname, age and score.
When a user clicks the "okay button", these values are stores in an sql database.
Additionally, I want to store all of these information in an QR code. And when I decode it, the information should be shown in the textboxes respectively.
These are the references I am using so far.
using AForge.Video.DirectShow;
using Zen.Barcode;
using ZXing.QrCode;
using ZXing;
I can encode an ID number into a picture box, like so:
CodeQrBarcodeDraw qrcode = BarcodeDrawFactory.CodeQr;
pictureBox1.Image = qrcode.Draw(textBox1.Text, 50);
But I want all of the values in the textboxes to be storee in this QR code.
How can i do that?
The essence of the solution is, that you have to combine all the values from the textboxes into one string. To seperate them after decoding the QR code, you have to add a special character between the data values, that does not exist insinde the user input. After decoding the QR code, you can seperate the values by splitting the string at each occurance of the special character.
This is the quick and dirty way of doing that. If you want the QR code to be conformant to any specific format (like vcard), you have to reserach what it takes to compose the data for this format.
I expect your users cannot enter more than one line into the textboxes, so the newline character can be used as seperator character.
Encode all the information into one QR code.
var qrText = textBox1.Text + "\n" +
textBox2.Text + "\n" +
textBox3.Text + "\n" +
textBox4.Text + "\n" +
textBox5.Text;
pictureBox1.Image = qrcode.Draw(qrText, 50);
You can decode the QR code and assigning the data to the different textboxes again.
var bitmap = new Bitmap(pictureBox1.Image);
var lumianceSsource = new BitmapLuminanceSource(bitmap);
var binBitmap = new BinaryBitmap(new HybridBinarizer(source));
var reader = new MultiFormatReader();
Result result = null;
try
{
result = reader.Decode(binBitmap);
}
catch (Exception err)
{
// Handle the exceptions, in a way that fits to your application.
}
var resultDataArray = result.Text.Split(new char[] {'\n'});
// Only if there were 5 linebreaks to split the result string, it was a valid QR code.
if (resultDataArray.length == 5)
{
textBox1.Text = resultDataArray[0];
textBox2.Text = resultDataArray[1];
textBox3.Text = resultDataArray[2];
textBox4.Text = resultDataArray[3];
textBox5.Text = resultDataArray[4];
}
You can get this done by implementing below code :
"{" + '"' + "name" + '"' + ":" + '"' + txtName.Text + '"' + "," + '"' + "lname" + '"' + ":" + '"' + txtLname.Text + '"' + "," + '"' + "Roll" + '"' + ":" + '"' + txtRoll.Text + '"' + '"' + "class" + '"' + ":" + '"' + txtClass.Text + '"' + "}"
Result will be:
{"name":"Diljit","lname":"Dosanjh","Roll","2071","class":"BCA"}
Such that your QR scanner will recognize the data belong to its specific filed.

C# saving mulitple filenames from OpenFileDialog

I have a form with an add button. When clicked, a user selects a file or files from the dialog.
My Goal:
Retrieve the names of all the files that a user selects (from whichever directory their file(s) are in) , copy those files in a specified folder that the user doesn't choose using File.Copy (I hard-code a filepath and filename).
My Issue:
If the user only selects one, this works fine. For example:
string name = System.IO.Path.GetFileName(sfd.FileName);
This grabs the file. Then:
DialogResult dialogResult = MessageBox.Show("Is this published?", "", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Directory.CreateDirectory("c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\Published");
File.Copy(sfd.FileName, "c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\Published\\" + name);
}
else if (dialogResult == DialogResult.No)
{
Directory.CreateDirectory("c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\NonPublished");
File.Copy(sfd.FileName, "c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\NonPublished\\" + name);
}
I ask the user if the document is published. Based on the answer, it will create a directory and put the file in that directory.
Is it possible to loop through multiple filenames in the openFileDialog and put them all in a folder , rather than just one?
Set the Multiselect to true.
myFileDialog.Multiselect = true;
When the user accepts the selection, you can get them with FileNames property. It returns a string[]. Note the difference with FileName which returns string. You can use a for or foreach to get all the results.
foreach (string file in myFileDialog.FileNames)
{
//do work
}

c# replace timestamp value string of text in textfield

i currently have a textbox field, everytime a change is made, i add who it was updated by and what time
right now it keep appending that text
how can i find the line that says "LastEdited: ", and only replace the time stamp value?
this is what i do now
maybe someone can post me example of code how to grab the timestamp and replace it?
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.AppendText("LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
i am not an expert, so an example of code would bevery useful
i do use this textbox to save other notes as well (which i wouldn't wanna loose)
can i do this
foreach (var line in txtMemberNotes.Lines) {
if (line.StartsWith("Last Edited: "))
{
txtMemberNotes.Text = txtMemberNotes.Text.Replace(line, "Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
else
{
txtMemberNotes.AppendText("Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
}
The easiest way to solve your problem seems to stop appending text and just set the text value. No parsing is necessary, just overwrite the exiting values with your newer value.
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.Text = "LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine;
I guess theres always Regex if you really wanted:
string s = #"Some notes here etc etc..
Last edited: 12/12/2012 12:00:00 AM";
Console.Write(Regex.Replace(s, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString()));
Or more specifically using your code:
txtMemberNotes.Text = Regex.Replace(txtMemberNotes.Text, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString());

window.focus() of JavaScript not working on IE9

This is my code
protected void LoginButton_Click(object sender, EventArgs e)
{
if (DAL.DAOKullanici.Login(KullaniciTextBox.Text,SifreTextBox.Text))
{
VeriyazPROTicari.Sessionlar.Variables.loginkontrol = true;
Session["kullaniciAdi"] = KullaniciTextBox.Text;
Session["kullaniciId"] = DAL.DAOKullanici.GetEntity(DAL.DAOKullanici.KullaniciAdiIleKullaniciIdCek(KullaniciTextBox.Text)).ID;
bool main_window_open = false;
if (!main_window_open)
{
Page.RegisterClientScriptBlock("Main_Window", "<script>" +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} "
+ "</script>");
main_window_open = true;
}
HataLabel.Text = "";
}
else
{
HataLabel.Text="Hatalı Giriş";
}
}
I have no problem with it except the JavaScript part.
What I am trying to is after LoginButton is clicked opening dashboard.aspx and setting focus on it.And this code opens dashboard.aspx and sets focus in Google Chrome and Mozilla Firefox 4.However,when I try it on IE9 dashboard.aspx is opened but focus does not work and dashboard.aspx remains under the login page.
How can I set focus on a new window on IE9?
I have had a similar problem to this and it seemed to happen because in IE9 (and any IE) that the focus() method is run before the window is rendered.
To get round this there are two ways that I can see that will fix this:
Set a timer to load the focus after a small amount of time.
Defer the JavaScript from being read until the window is fully rendered.
The timer method to me is not my preferred choice as in my personal opinion it is messy and if the page takes longer to load than the timer you are stuck with the same problem. To implement the timer you could use something like:
Page.RegisterClientScriptBlock("Main_Window", "<script>" +
"setTimeout(function() { " +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} " +
"}, 5);" +
"</script>");
I have set a delay of 5 seconds, which may be overkill.
The defer method is my preferred choice as I feel it is cleaner and simpler, but it may or may not work:
Page.RegisterClientScriptBlock("Main_Window", "<script type="text/javascript" defer="defer">" +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} "
+ "</script>");
After what seemed like an eternity, my colleague figured out a way to make it work for us.
There are other references to the issue itself, http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/a250c431-9f09-441c-9b78-af067233ed78 http://support.microsoft.com/kb/979954
That said, we simply put the window.focus() in the body tag of the new (pop-up) page.
<body onload="FocusWindow()">
Defined FocusWindow() in a separate .js file as follows,
function FocusWindow() {
window.focus();
}

Categories

Resources