|
just that ...¿?
BiGjOe
Passez Ce Qu'il Passe
|
|
|
|
|
Wiki, as usual.
This technique can be applied to all programming languages. Do you have any C# specific question?
modified 12-Sep-18 21:01pm.
|
|
|
|
|
hellow to all
i am new to C# i tried to make a Model like in VB.NET where i can put variable there and all the forums can read them , or write a public function but i couldn't , what method in C# replace what i am trying to do ?
where can i find good articls to study moveing from VB.NET to C# ?
thank to all for anyhelp ...
|
|
|
|
|
hi there is no (modul) in c# !! try to implement public static (shared in vb.net) the following likns will help you
1 http://www.kamalpatel.net/ConvertCSharp2VB.aspx
2 http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
Tamimi - Code
|
|
|
|
|
The on-line converters mentioned in the other post have serious shortcomings such as not handling generics, see the following for a full list http://www.tangiblesoftwaresolutions.com/Articles/Language%20Features%20not%20Handled%20by%20On-Line%20Converters.htm[^]
The demo of our Instant C# converter will be more useful as it actually converts VB.NET code (2005 or not).
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
|
|
|
|
|
I would like my treeview to remember if the user had a node expanded or not
previous to a refresh. I have found the method expand(), which I think will
do this, but I'am having problems implementing it.
The code compiles but I get runtime exceptions when I launch the application.
Bottom line question,
How do you properly implement treeview.expand()?
Cheers,
-t-
-- modified at 10:51 Sunday 4th June, 2006
|
|
|
|
|
if you want to add from serverXml to PcXml according to your conditions :-
private void AddRow()
{
DataSet serverXml = new DataSet();
serverXml.ReadXml("ServerXml.xml");
DataSet pcXml = new DataSet();
pcXml.ReadXml("PcXml.xml");
foreach(DataRow newRow in serverXml.Tables[0].Rows)
{
string ID = newRow["ID"].ToString();
DataRow [] TempPcRows = pcXml.Tables[0].Select("id= " + ID);
if(TempPcRows.Length != 0)
{
TempPcRows[0].ItemArray =
}
else
{
pcXml.Tables[0].ImportRow(newRow);
}
}
pcXml.WriteXml("PCXml.xml");
Tamimi - Code
|
|
|
|
|
Hi Tamimi - Code,
I thank you very much for your reply!
I don't see this foreach statement: foreach (DataRow serverRow in serverXML.Tables[0].Rows) , which i had in my previous code. Does that mean i don't need that statement?
Because i can't figure out what i should be putting behind this line:
TempPcRows[0].ItemArray =
Hope you can tell me that Tamimi.
Thanks in advance!
|
|
|
|
|
sorry
here is the complete line
TempPcRows[0].ItemArray = newRow.ItemArray;
Tamimi - Code
|
|
|
|
|
Thank you Tamimi - Code!
You have no idea how greatfull i am!
You're the greatest ! ! !
|
|
|
|
|
Not sure if you understand my question?
Appreciate the effort though...
I'm not doing anything with XML
This is just a simple treeview within a form
It's a continuation from some sample code from
a O'Riely book on c#
I have tried to put it in the expand method where
the code fills the directory with no success?
Don't know how to attach so
I will just paste what I have so far:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace FileCopier
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView tvwSource;
private System.Windows.Forms.TreeView tvwTargetDir;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.CheckBox chkOverwrite;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnCopy;
private System.Windows.Forms.Label lblStatus;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.TextBox txtTargetDir;
private System.Windows.Forms.Label lblSource;
private System.Windows.Forms.Label lblTarget;
private System.ComponentModel.Container components = null;
public class FileComparer : IComparer
{
public int Compare (object f1, object f2)
{
FileInfo file1 = (FileInfo) f1;
FileInfo file2 = (FileInfo) f2;
if (file1.Length > file2.Length)
{
return -1;
}
if (file1.Length < file2.Length)
{
return 1;
}
return 0;
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
// fill the source and target directory trees
FillDirectoryTree(tvwSource, true);
FillDirectoryTree(tvwTargetDir, false);
}
///
/// Fill the directory tree for either the Source or
/// Target TreeView.
///
private void FillDirectoryTree(
TreeView tvw, bool isSource)
{
// Populate tvwSource, the Source TreeView,
// with the contents of
// the local hard drive.
// First clear all the nodes.
tvw.Nodes.Clear();
// Get the logical drives and put them into the
// root nodes. Fill an array with all the
// logical drives on the machine.
string[] strDrives = Environment.GetLogicalDrives();
// Iterate through the drives, adding them to the tree.
// Use a try/catch block, so if a drive is not ready,
// e.g. an empty floppy or CD,
// it will not be added to the tree.
foreach (string rootDirectoryName in strDrives)
{
if (rootDirectoryName != @"C:\")
continue;
try
{
// Fill an array with all the first level
// subdirectories. If the drive is
// not ready, this will throw an exception.
DirectoryInfo dir =
new DirectoryInfo(rootDirectoryName);
dir.GetDirectories();
TreeNode ndRoot = new TreeNode(rootDirectoryName);
// Add a node for each root directory.
tvw.Nodes.Add(ndRoot);
// Add subdirectory nodes.
// If Treeview is the source,
// then also get the filenames.
if (isSource)
{
GetSubDirectoryNodes(
ndRoot, ndRoot.Text, true);
}
else
{
GetSubDirectoryNodes(
ndRoot, ndRoot.Text, false);
}
}
// Catch any errors such as
// Drive not ready.
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
} // close for FillSourceDirectoryTree
///
/// Gets all the subdirectories below the
/// passed in directory node.
/// Adds to the directory tree.
/// The parameters passed in at the parent node
/// for this subdirectory,
/// the full path name of this subdirectory,
/// and a Boolean to indicate
/// whether or not to get the files in the subdirectory.
///
private void GetSubDirectoryNodes(
TreeNode parentNode, string fullName, bool getFileNames)
{
DirectoryInfo dir = new DirectoryInfo(fullName);
DirectoryInfo[] dirSubs = dir.GetDirectories();
// Add a child node for each subdirectory.
foreach (DirectoryInfo dirSub in dirSubs)
{
// do not show hidden folders
if ( (dirSub.Attributes & FileAttributes.Hidden)
!= 0 )
{
continue;
}
///
/// Each directory contains the full path.
/// We need to split it on the backslashes,
/// and only use
/// the last node in the tree.
/// Need to double the backslash since it
/// is normally
/// an escape character
///
TreeNode subNode = new TreeNode(dirSub.Name);
parentNode.Nodes.Add(subNode);
// Call GetSubDirectoryNodes recursively.
GetSubDirectoryNodes(
subNode,dirSub.FullName,getFileNames);
}
if (getFileNames)
{
// Get any files for this node.
FileInfo[] files = dir.GetFiles();
// After placing the nodes,
// now place the files in that subdirectory.
foreach (FileInfo file in files)
{
TreeNode fileNode = new TreeNode(file.Name);
parentNode.Nodes.Add(fileNode);
}
}
}//close for private void GetSubDirectoryNodes
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.lblSource = new System.Windows.Forms.Label();
this.lblTarget = new System.Windows.Forms.Label();
this.btnClear = new System.Windows.Forms.Button();
this.btnCopy = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.chkOverwrite = new System.Windows.Forms.CheckBox();
this.txtTargetDir = new System.Windows.Forms.TextBox();
this.tvwSource = new System.Windows.Forms.TreeView();
this.tvwTargetDir = new System.Windows.Forms.TreeView();
this.lblStatus = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblSource
//
this.lblSource.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblSource.Location = new System.Drawing.Point(100, 25);
this.lblSource.Name = "lblSource";
this.lblSource.Size = new System.Drawing.Size(150, 23);
this.lblSource.TabIndex = 0;
this.lblSource.Text = "Source Files";
this.lblSource.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblTarget
//
this.lblTarget.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblTarget.Location = new System.Drawing.Point(400, 25);
this.lblTarget.Name = "lblTarget";
this.lblTarget.Size = new System.Drawing.Size(175, 23);
this.lblTarget.TabIndex = 1;
this.lblTarget.Text = "Target Directory";
this.lblTarget.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(125, 450);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(100, 23);
this.btnClear.TabIndex = 2;
this.btnClear.Text = "Clear";
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnCopy
//
this.btnCopy.Location = new System.Drawing.Point(550, 450);
this.btnCopy.Name = "btnCopy";
this.btnCopy.TabIndex = 3;
this.btnCopy.Text = "Copy";
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(550, 500);
this.btnDelete.Name = "btnDelete";
this.btnDelete.TabIndex = 4;
this.btnDelete.Text = "Delete";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(550, 550);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// chkOverwrite
//
this.chkOverwrite.Location = new System.Drawing.Point(425, 425);
this.chkOverwrite.Name = "chkOverwrite";
this.chkOverwrite.Size = new System.Drawing.Size(115, 24);
this.chkOverwrite.TabIndex = 6;
this.chkOverwrite.Text = "Overwrite if exists";
//
// txtTargetDir
//
this.txtTargetDir.Location = new System.Drawing.Point(350, 50);
this.txtTargetDir.Name = "txtTargetDir";
this.txtTargetDir.Size = new System.Drawing.Size(275, 20);
this.txtTargetDir.TabIndex = 7;
this.txtTargetDir.Text = "";
//
// tvwSource
//
this.tvwSource.CheckBoxes = true;
this.tvwSource.ImageIndex = -1;
this.tvwSource.Location = new System.Drawing.Point(50, 50);
this.tvwSource.Name = "tvwSource";
this.tvwSource.SelectedImageIndex = -1;
this.tvwSource.Size = new System.Drawing.Size(250, 375);
this.tvwSource.TabIndex = 8;
this.tvwSource.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.tvwSource_AfterCheck);
//
// tvwTargetDir
//
this.tvwTargetDir.ImageIndex = -1;
this.tvwTargetDir.Location = new System.Drawing.Point(350, 75);
this.tvwTargetDir.Name = "tvwTargetDir";
this.tvwTargetDir.SelectedImageIndex = -1;
this.tvwTargetDir.Size = new System.Drawing.Size(275, 350);
this.tvwTargetDir.TabIndex = 9;
this.tvwTargetDir.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvwTargetDir_AfterSelect);
//
// lblStatus
//
this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblStatus.Location = new System.Drawing.Point(50, 500);
this.lblStatus.Name = "lblStatus";
this.lblStatus.TabIndex = 0;
this.lblStatus.Text = "Status:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(667, 591);
this.Controls.Add(this.tvwTargetDir);
this.Controls.Add(this.tvwSource);
this.Controls.Add(this.txtTargetDir);
this.Controls.Add(this.chkOverwrite);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnCopy);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.lblTarget);
this.Controls.Add(this.lblSource);
this.Controls.Add(this.lblStatus);
this.Name = "Form1";
this.Text = "File Copier";
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// on cancel, exit
///
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
///
/// recursively set or clear check marks
///
private void SetCheck(TreeNode node, bool check)
{
// find all the child nodes from this node
foreach (TreeNode n in node.Nodes)
{
n.Checked = check; // check the node
// if this is a node in the tree, recurse
if (n.Nodes.Count != 0)
{
SetCheck(n,check);
}
}
}
///
/// Given a node and an array list
/// fill the list with the names of
/// all the checked files
///
// Fill the ArrayList with the full paths of
// all the files checked
private void GetCheckedFiles(TreeNode node,
ArrayList fileNames)
{
// if this is a leaf...
if (node.Nodes.Count == 0)
{
// if the node was checked...
if (node.Checked)
{
// get the full path and add it to the arrayList
string fullPath = GetParentString(node);
fileNames.Add(fullPath);
}
}
else // if this node is not a leaf
{
// if this node is not a leaf
foreach (TreeNode n in node.Nodes)
{
GetCheckedFiles(n,fileNames);
}
}
}
///
/// Given a node, return the
/// full path name
///
private string GetParentString(TreeNode node)
{
// if this is the root node (c:\) return the text
if(node.Parent == null)
{
return node.Text;
}
else
{
// recurse up and get the path then
// add this node and a slash
// if this node is the leaf, don't add the slash
return GetParentString(node.Parent) + node.Text +
(node.Nodes.Count == 0 ? "" : "\\");
}
}
///
/// shared by delete and copy
/// creates an ordered list of all
/// the selected files
///
private ArrayList GetFileList()
{
// create an unsorted array list of the full file names
ArrayList fileNames = new ArrayList();
// fill the fileNames ArrayList with the
// full path of each file to copy
foreach (TreeNode theNode in tvwSource.Nodes)
{
GetCheckedFiles(theNode, fileNames);
}
// Create a list to hold the FileInfo objects
ArrayList fileList = new ArrayList();
// for each of the file names we have in our unsorted list
// if the name corresponds to a file (and not a directory)
// add it to the file list
foreach (string fileName in fileNames)
{
// create a file with the name
FileInfo file = new FileInfo(fileName);
// see if it exists on the disk
// this fails if it was a directory
if (file.Exists)
{
// both the key and the value are the file
// would it be easier to have an empty value?
fileList.Add(file);
}
}
// Create an instance of the IComparer interface
IComparer comparer = (IComparer) new FileComparer();
// pass the comparer to the sort method so that the list
// is sorted by the compare method of comparer.
fileList.Sort(comparer);
return fileList;
}
/*
private void tvwSource_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
SetCheck(e.Node,e.Node.Checked);
Application.DoEvents();
}
*/
private void tvwSource_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
SetCheck(e.Node,e.Node.Checked);
}
///
/// Tell the root of each tree to uncheck
/// all the nodes below
///
private void btnClear_Click(object sender, System.EventArgs e)
{
// get the top most node for each drive
// and tell it to clear recursively
foreach (TreeNode node in tvwSource.Nodes)
{
SetCheck(node, false);
}
}
///
/// Create an ordered list of all
/// the selected files, copy to the
/// target directory
///
private void btnCopy_Click(object sender, System.EventArgs e)
{
// get the list
ArrayList fileList = GetFileList();
// copy the files
foreach (FileInfo file in fileList)
{
try
{
// update the label to show progress
lblStatus.Text = "Copying " + txtTargetDir.Text +
"\\" + file.Name + "...";
Application.DoEvents();
// copy the file to its destination location
file.CopyTo(txtTargetDir.Text + "\\" +
file.Name,chkOverwrite.Checked);
}
catch (Exception ex)
{
// you may want to do more than
// just show the message
MessageBox.Show(ex.Message);
}
}
lblStatus.Text = "Done.";
FillDirectoryTree(tvwSource, true);
FillDirectoryTree(tvwTargetDir, false);
Application.DoEvents();
}
///
/// check that the user does want to delete
/// Make a list and delete each in turn
///
private void btnDelete_Click(object sender, System.EventArgs e)
{
// ask them if they are sure
System.Windows.Forms.DialogResult result =
MessageBox.Show(
"Are you quite sure?", // msg
"Delete Files", // caption
MessageBoxButtons.OKCancel, // buttons
MessageBoxIcon.Exclamation, // icons
MessageBoxDefaultButton.Button2); // default button
// if they are sure...
if (result == System.Windows.Forms.DialogResult.OK)
{
// iterate through the list and delete them.
// get the list of selected files
ArrayList fileNames = GetFileList();
foreach (FileInfo file in fileNames)
{
try
{
// update the label to show progress
lblStatus.Text = "Deleting " +
txtTargetDir.Text + "\\" +
file.Name + "...";
Application.DoEvents();
// Danger Will Robinson!
file.Delete();
}
catch (Exception ex)
{
// you may want to do more than
// just show the message
MessageBox.Show(ex.Message);
}
}
lblStatus.Text = "Done.";
FillDirectoryTree(tvwSource, true);
FillDirectoryTree(tvwTargetDir, false);
Application.DoEvents();
}
}
///
/// Get the full path of the chosen directory
/// copy it to txtTargetDir
///
private void tvwTargetDir_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
// get the full path for the selected directory
string theFullPath = GetParentString(e.Node);
// if it is not a leaf, it will end with a back slash
// remove the backslash
if (theFullPath.EndsWith("\\"))
{
theFullPath = theFullPath.Substring(0,theFullPath.Length-1);
}
// insert the path in the text box
txtTargetDir.Text = theFullPath;
}
}//close for public class Form1 : System.Windows.Forms.Form
}//close for namespace FileCopier
/* <eof> */
-t-
-- modified at 22:25 Sunday 4th June, 2006
|
|
|
|
|
this is the error :System.Data.Odbc.OdbcException: ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
The query:
dbcComm.CommandText = "SELECT studenti.user_name,studenti.Nume, " +
"studenti.Prenume, cursuri.denumire_curs, note.nota, note.data " +
"FROM studenti INNER JOIN (cursuri INNER JOIN [note] ON " +
"cursuri.id_curs = note.id_curs) ON (studenti.id_stud = note.id_stud)"+
"WHERE studenti.user_name= "+user.ToString();
i want the query to select only those students(=studenti in my language :p) with that user name which i have store in my user variable
it works fine without the WHERE clause but it displays all students then
Thanks
TheBeginner
|
|
|
|
|
You need to add quotes around the user_name because it is a string:
dbcComm.CommandText = "SELECT studenti.user_name,studenti.Nume, " +
"studenti.Prenume, cursuri.denumire_curs, note.nota, note.data " +
"FROM studenti INNER JOIN (cursuri INNER JOIN [note] ON " +
"cursuri.id_curs = note.id_curs) ON (studenti.id_stud = note.id_stud)"+
"WHERE studenti.user_name= '"+user.ToString()+"'";
note:
It's much better to use parameterized query to avoid SQL injection.
|
|
|
|
|
Hi
You need to put quotes around your literal:
"WHERE studenti.user_name= '" + user.ToString() + "'";
You might consider actually using a parameter here - it's good practice
----------------------------
Be excellent to each other
EasiReports[^] My free reporting component for WinForms.
|
|
|
|
|
is there any way to read and write COM PORT through c#
please help me if any one know it
thanks
raj
india
link
|
|
|
|
|
i use "Select * from tableName Where joinDate >= '02/05/2006' And joinDate <= '15/05/2006'".
it does not work but why, i use sql server.
plz help me.
|
|
|
|
|
what do you mean by its not work ??
Tamimi - Code
|
|
|
|
|
i mean, it does not return any row but the table contains those type of row which satisfied this condition( joinDate >= '02/05/2006' And joinDate <= '15/06/2006')
|
|
|
|
|
try to excute your query in the query analyzer and see what happen.
if its work take a look to your code.
Tamimi - Code
|
|
|
|
|
sql won't recognise 02/05/2006! this is just a string and not a date.. Maybe this works 1900-01-04 00:00:00.000 but the safest way is to use an explicit cast like this CAST('1900-01-04 08:00' AS datetime)
|
|
|
|
|
sometimes the sql server configure the date as mm/dd/yy so 15/05/2006
is not a valid date
Tamimi - Code
|
|
|
|
|
You better use the yyyyMMdd format with SQL server for dates:
This :
foysal mamun wrote: Select * from tableName Where joinDate >= '02/05/2006' And joinDate <= '15/05/2006'
should be written like:
Select * from tableName Where joinDate >= '20060502' And joinDate <= '20060515'
|
|
|
|
|
Does the exact query entered into SQL Server through Query Analyzer return any rows?
|
|
|
|