|
I don't know why still does work ?
in my program:
I store create an arraylist to store up the some string of ListView item data, and I assign that arraylist as ListViewItem.Tag
Finally, when I multiple selected the ListViewItem, I use a new ArrayList called "Final" to collect selected Item.Tag:
for(int i=0; i< List.Items.Count; i++)
{
Final.Add( (ArrayList) List.Items[i].Tag );
}
I want to check if "Final" contains any Tags new ListView;
ArrayList selectedItems = Final;
if(selectedItems.Contains(Items[i].Tag))
Items[i].BackColor = Color.Orange;
I have used debug window to check, they contains same data inside actually, but seems wrong data type; the selectedItems only shows that it contains "system.object", however, Items[i].Tag contains "System.Collections.ArrayList"??
Is it a mistake that I made?
Please help ~~ Thank you
|
|
|
|
|
First of all, some suggestions.
this line:
Final.Add( (ArrayList) List.Items[i].Tag );
Don't bother casting the Tag object to arraylist as ArrayList Items are of the System.Object Class. What you are doing is casting from Object to Arraylist, and then back to object.
azusakt wrote:
I want to check if "Final" contains any Tags new ListView;
I'm not sure I understand this sentence. Do you mean that the you now have a different ListView? If so, you should remember that ArrayList.Contains checks for reference, not values. For example:
a.Add("123");
b.Add("123");
c.Add(a);
The expression c.Contains(b) would return false even thought a and b contains the same value, just because a and b not the same reference.
I hope I'm not confusing you
"if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.
Support Bone
|
|
|
|
|
Hello,
I want to focus a perticular cell in a datagrid control. Its not a difficult task, but i don't want to surf my time, so if anyone knows this thing, please reply soon.
Thanks.
|
|
|
|
|
So you think posting a simple question to the forums is faster than looking at the properties and methods for the DataGrid class? DataGrid.CurrentCell should stick out like a sore thumb.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hello,
Infact, i already tried it. But, its not focusing on a perticular cell when i do this,
DataGrid.CurrentCell.RowNumber = RowNumber;
DataGrid.CurrentCell.ColumnNumber = ColumnNumber;
It gives me an exception. I want to focus a perticular cell during runtime.
|
|
|
|
|
The property type (DataGridCell ) is a struct, not a reference type, so you just can't change the properties/fields of it because you actually are changing the values for a copy of the DataGridCell . Instead, you must do the following:
DataGridCell cell = new DataGridCell(row, column);
dataGrid1.CurrentCell = cell; This was also done in a similar way in the example code for the SDK documentation of the DataGrid.CurrentCell property.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hay,
Thank u for your reply to my simple questions. Now, let me tell u my exact problem.
I have four columns in a datagrid. When i finishes my entries in a a perticular row and press Enter. By default the DataGrid creates a new empty row. I want to focus the second column cell of that new row whenever i press Enter.
I have already tested this,
DataGridCell cell = new DataGridCell(int rownum, int colnum);
DataGrid.CurrentCell = cell;
I tried this thing in a 'RowChanged' event of the dataset binded with that DataGrid.
Dataset.Table.RowChanged += new EventHandler(DataSet_RowChanged);
It creates Two or Three empty rows and then focus the last new line cell.
Try this thing, if you have understood my problem and reply soon.
Thanks.
|
|
|
|
|
Because the new row isn't actually a row. Especially, the event handler you created on the DataSet won't work because a row hasn't been added until you actually start entering data for the new row (or edit an existing row) and commit it.
You might be interested in getting a spreadsheet-type control, such as the one from FarPoint Technologies[^].
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Does anyone know an easy way to Print a form with controls in C# ?
Paul
|
|
|
|
|
There's no easy way to do it. You'll have to P/Invoke many different native methods from the Gdi32.dll library, such as BitBlt , GetDeviceCaps , CreateCompatibleDC , CreateCompatibleBitmap , and SelectObject . Some others might be necessary, too. Just google for keywords like HDC, screen, printer, and bitblt. Most examples are in C++ but - with the power of P/Invoking - you should be able to do what you want.
Here's a few hints: you can get the HDC for a Graphics object by calling Graphics.GetHdc() . Both the Form.Paint and PrintDocument.PrintPage events pass a Graphics object in the second parameter (as a property). In the PrintDocument.PrintPage event, you can call Form.CreateGraphics() (actually inherited from the Control class) to get a Graphics object at that point.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
/****
Here is a sample form with a Print button and couple of controls. When you hit print button, it prints the form with the controls and the contents if any on the control.
To check it, start a new C# windows aplication project and replace Form1.cs with following contents.
Things to be noted are :-
Functions:
btnPrint_Click
CaptureScreen
printDocument1_PrintPage
Objects and declaration
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private System.Drawing.Printing.PrintDocument printDocument1;
If u have any questions, let me know.
Ruchi
****/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnPrint;
private System.Drawing.Printing.PrintDocument printDocument1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnPrint = new System.Windows.Forms.Button();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.SuspendLayout();
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(48, 72);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.TabIndex = 0;
this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.Location = new System.Drawing.Point(32, 128);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// btnPrint
//
this.btnPrint.Location = new System.Drawing.Point(192, 216);
this.btnPrint.Name = "btnPrint";
this.btnPrint.TabIndex = 2;
this.btnPrint.Text = "Print";
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnPrint,
this.textBox1,
this.dateTimePicker1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void showmessage()
{
MessageBox.Show("Testing 123");
}
private void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)
{
//MessageBox.Show("Testing 123");
showmessage();
//System.Console.WriteLine("Testing 123");
}
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font f = new Font("Arial",12);
e.Graphics.DrawString("Opetion Message if I want to be printed", f, Brushes.Black, 300, 50);
e.Graphics.DrawImage(memoryImage,75, 75);
}
private void btnPrint_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
//printDocument1.Print();
}
}
}
|
|
|
|
|
what if the form's height is more than the screen height ?
And how can I print a DataGrid with scrolling items all to be displayed ?
Paul
|
|
|
|
|
I have code which uses multi-threaded operations and am currently using ISyncronizeInvoke, with a Form to synchronize the threads and pass data along the windows message queue.
The problem occurs when an exception is thrown inside the Invoke()'d method, this exception is passed back down to the method that called the Invoke() command, and not inside the method that actually caused the exception.
Does anyone know if there is a way for me to keep the exceptions thrown inside the Invoke, where they are (so to speak)?
Or even a better way to synchronize the threads?
Thanks..
EDIT: Forgot to say, the main idea behind this is that I want to raise events from a thread, on my main process. So that when a thread has something that needs attention, an event is then raised on the main process, so that it can do something funky.
--
Rob Hutchinson.
|
|
|
|
|
Not unless you can modify the invoked method implementation, either by changing the original source or overriding a method and putting the base.Method call in a try-catch. Otherwise, just catch and ignore the exception when you call Invoke or handle the AppDomain.UnhandledException event.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I have an ImageList that stores Icons pulled from the system. When a new file extention is looked up, the icon is pulled from the system, and compared with all the current icons through a pixel by pixel comparison.
The problem I have is that, the images stored in the imageList are stored at a different color depth to the one that comes fresh from the system.
I'd like to set the Colour Depth on the imagelist to that of the system, or alternatively, set the imported image colour depth to that of the image list.
However, I am lost as to how to do this.
Any help would be appreciated.
Cata
|
|
|
|
|
Then as a previous user suggested, create a class that inherits from Page and make sure you change the base class in each of your code-behind source files to use that class. Each page should override the property (so make sure the property in the derivative class is marked as virtual ) and return the string you want with each page.
Alternatively, if your pages are named correctly, you actually could use the Type name. In your user control, you could do something like:
string name = Page.GetType().FullName;
name = name.Substring(name.LastIndexOf('.') + 1); ...and name would contain the class name of the page. Of course, this would only work if your page class names matched up with what you want for your user control. It might work, but the first solution is a better object-oriented solution.
Note, too, that if you create a base Page class (that inherits from Page), you should probably also change your Web.config file so that it uses that page (in case a file has no code-behind source file). See the <pages> section, specifically the pageBaseType attribute.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I'm trying to open a file for writing in a server process. I do not want any other process to be able to write to the file while I have it open, but I do want to allow other programs to read from it. Seems simple enough. I think I've done this with CFile in MFC. I'm getting a problem trying to use the .NET FileStream class. Can any of you shed light on why I'm getting a 'File is in use' IO exception with this:
try
{
String Nm = "c:\\testfile.txt";
//simulate the server opening the file for writing:
FileStream str = File.Open(Nm, FileMode.Append, FileAccess.Write, FileShare.Read);
//simulate some other process opening the file for reading: (exception thrown here)
FileStream sss = File.OpenRead(Nm);
}
catch (IOException e)
{
System.Console.WriteLine(e.Message);
}
|
|
|
|
|
Actually, you're aren't opening it in a different process so it's not a good test. According to the documentation for the FileShare enum, the file has to be opened in a different process or in a different thread. I tried your code and, of course, got the same error. I open the file in a separate thread and it works fine. Also, pay attention to my finally block in the code below. You should use the finally block in a try-catch (or forget the catch if you want the exception to be thrown to the caller) to make sure things are cleaned up, whether or not an exception is thrown, or even return is called (it is not executed only when the CLR is unloaded, like calling Environment.Exit ):
using System;
using System.IO;
using System.Threading;
public class FileTest
{
public static void Main()
{
FileStream s = null;
try
{
s = File.Open("FileTest.cs", FileMode.Open,
FileAccess.ReadWrite, FileShare.Read);
new Thread(new ThreadStart(ReadFile)).Start();
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
finally
{
if (s != null) s.Close();
}
}
private static void ReadFile()
{
FileStream s = null;
try
{
s = File.OpenRead("FileTest.cs");
Stream os = Console.OpenStandardOutput(4096);
int read = 0;
byte[] buffer = new byte[4096];
do
{
read = s.Read(buffer, 0, buffer.Length);
os.Write(buffer, 0, read);
} while (read == buffer.Length);
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
finally
{
if (s != null) s.Close();
}
}
}
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks for the quick response Heath. Try adding a Thread.Sleep after you start that other thread so that the original thread still has the file open for writing. Before moving my stuff to the over simple example I submitted, I had it in different programs altogther. Any other suggestions?
I tried the exact same thing with CFile and it works:
try
{
//open for exclusive write
CStdioFile file;
BOOL bOpen1 = file.Open("C:\\timtest2.txt", CFile::modeWrite | CFile::modeCreate | CFile::shareDenyWrite);
file.WriteString("hi");
file.Flush();
//open a reader
CStdioFile f2;
BOOL bOpen2 = f2.Open("c:\\timtest2.txt", CFile::modeRead | CFile::shareDenyNone);
CString str;
f2.ReadString(str);
//try to open another writer --- this open fails
CStdioFile file3;
BOOL bOpen3 = file3.Open("C:\\timtest2.txt", CFile::modeWrite | CFile::modeCreate | CFile::shareDenyWrite);
file3.WriteString("hi");
file3.Flush();
}
catch (CFileException* pe)
{
pe->ReportError();
pe->Delete();
}
Argh!!
|
|
|
|
|
Good point - hadn't thought of that.
Have you tried accessing the file from two different processes? I mean, I would think the same process could access it, but maybe there is a problem related to that.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Well I originally coded it in two processes and that's
when I noticed the problem to begin with. The weird
thing is that other applications (notepad) can open
the files (and they come up in readonly like I would
expect).
I'm totally stumped on what's wrong. I really don't
want to do interop for something so silly.
|
|
|
|
|
Heath, I was able to get around this by resorting to interop. I just wrapped a call to the CreateFile function and then instantiated a FileStream object using the file handle (IntPtr) returned from CreateFile. Once I took this route everything works fine. It looks like there's a bug in the FCL when the FileStream class goes to open the files.
Anyway, just an FYI.
thanks again
Tim
|
|
|
|
|
This thread applies to this board and the XML board, so i tossed a coin and it came here.
I am attempting to create an XML document in C# by parsing values out of a dataset, unfortunately I cannot simply use dataset.WriteXml() as it isnt powerful enough to handle what I need to do.
I understand the basics of using XmlDocument but I am having trouble navigating around the nodes. So if anybody has any guidance or links to related tutorials the assistance would be greatly appreciated.
The following is a basic summary of my problem :
The dataset I am using (and have no control over) has the follow structure :
type value
2 valA
1 valAA
1 valAB
2 valB
1 valBA
1 valBB
1 valBC
2 valC
Now this is structured in such a way that, all the type 2 values are parents, and all the type 1 values directly under it are children of that parent.
Now the XML document I am attempting to create should have the following format :
<data>
<parent>
<value></value>
<child>
<value></value>
</child>
<parent>
</data>
So if I was to produce an XML document using that template and the dataset above I would have the resulting XML :
<data>
<parent>
<value>valA</value>
<child>
<value>valAA</value>
</child>
<child>
<value>valAB</value>
</child>
<parent>
<parent>
<value>valB</value>
<child>
<value>valBA</value>
</child>
<child>
<value>valBB</value>
</child>
<child>
<value>valBC</value>
</child>
<parent>
<parent>
<value>valC</value>
<child>
<value>valCA</value>
</child>
<parent>
</data>
I have managed to get as far as adding the parents to the XML doc, but I am failing when I try to add children to the parent node.
post.mode = signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
As usual, I "imaginered" a solution about 10 seconds after asking the question, but I thought I would run it pass you guys as I have a nack for not coming up with the most elegant solutions.
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml("<?xml version='1.0' encoding='UTF-8'?><data></data>");
XmlNode xnode = xdoc.DocumentElement;
XmlElement xelement;
XmlElement parent = null;
foreach(DataRow row in ds.Tables[0].Rows)
{
if ((int)row["type"] == 2)
{
xelement = xdoc.CreateElement("parent");
xelement.InnerXml = string.Format("<value>{0}</value>", row["value"].ToString());
xnode.AppendChild(xelement);
parent = xelement;
}
else
{
xelement = xdoc.CreateElement("child");
xelement.InnerXml = xelement.InnerXml = string.Format("<value>{0}</value>", row["value"].ToString());
parent.AppendChild(xelement);
}
}
post.mode = signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
<
>
|
|
|
|
|
That's about what I'd do, although you should use the XmlNode methods for creating elements, creating their attributes and assign them values, and assign the values for the elements rather than using string.Format . It works, but there is no validation and it won't take into account namespaces should you ever use them.
You could also create an instance of an XmlTextWriter and use it's methods like WriteStartDocument , WriteStartElement , ... WriteEndElement , and WriteEndDocument . These methods are fully namespace-aware. It basically amounts to writing-out the XML document itself instead of creating one in memory and then writing it.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|