|
Here's an example I just whipped up. The constructor populates a DataSet with some hierarchical data, and the Form.Load populates a TreeView with it.
There are many other ways this same data could be displayed in the TreeView, which is why there's no "automatic" way. You need to decide how you want your tree to look (this is one of the reasons I use a TreeView much more frequently than a DataGridView).
namespace PlayGround
{
public partial class Form1 : System.Windows.Forms.Form
{
private enum Gender { Male , Female } ;
private System.Data.DataSet ds = new System.Data.DataSet() ;
public Form1 ( string[] args )
{
InitializeComponent() ;
System.Data.DataRow dr ;
ds.Tables.Add ( new System.Data.DataTable ( "PetType" ) ) ;
ds.Tables.Add ( new System.Data.DataTable ( "Person" ) ) ;
ds.Tables.Add ( new System.Data.DataTable ( "Pet" ) ) ;
ds.Tables [ "PetType" ].Columns.Add ( new System.Data.DataColumn ( "Id" , typeof(int) ) ) ;
ds.Tables [ "PetType" ].Columns.Add ( new System.Data.DataColumn ( "Type" , typeof(string) ) ) ;
ds.Tables [ "PetType" ].Columns.Add ( new System.Data.DataColumn ( "DisplayColor" , typeof(string) ) ) ;
ds.Tables [ "Person" ].Columns.Add ( new System.Data.DataColumn ( "Id" , typeof(int) ) ) ;
ds.Tables [ "Person" ].Columns.Add ( new System.Data.DataColumn ( "Name" , typeof(string) ) ) ;
ds.Tables [ "Person" ].Columns.Add ( new System.Data.DataColumn ( "Gender" , typeof(string) ) ) ;
ds.Tables [ "Pet" ].Columns.Add ( new System.Data.DataColumn ( "Id" , typeof(int) ) ) ;
ds.Tables [ "Pet" ].Columns.Add ( new System.Data.DataColumn ( "PetType" , typeof(int) ) ) ;
ds.Tables [ "Pet" ].Columns.Add ( new System.Data.DataColumn ( "Owner" , typeof(int) ) ) ;
ds.Tables [ "Pet" ].Columns.Add ( new System.Data.DataColumn ( "Name" , typeof(string) ) ) ;
ds.Tables [ "Pet" ].Columns.Add ( new System.Data.DataColumn ( "Gender" , typeof(string) ) ) ;
ds.Tables [ "Pet" ].Columns.Add ( new System.Data.DataColumn ( "Age" , typeof(int) ) ) ;
ds.Relations.Add ( new System.Data.DataRelation ( "TypeOfPet" , ds.Tables [ "PetType" ].Columns [ "Id" ] , ds.Tables [ "Pet" ].Columns [ "PetType" ] ) ) ;
ds.Relations.Add ( new System.Data.DataRelation ( "OwnerOfPet" , ds.Tables [ "Person" ].Columns [ "Id" ] , ds.Tables [ "Pet" ].Columns [ "Owner" ] ) ) ;
dr = ds.Tables [ "PetType" ].NewRow() ;
dr [ "Id" ] = 0 ;
dr [ "Type" ] = "Dog" ;
dr [ "DisplayColor" ] = "Green" ;
ds.Tables [ "PetType" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "PetType" ].NewRow() ;
dr [ "Id" ] = 1 ;
dr [ "Type" ] = "Cat" ;
dr [ "DisplayColor" ] = "Blue" ;
ds.Tables [ "PetType" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Person" ].NewRow() ;
dr [ "Id" ] = 0 ;
dr [ "Name" ] = "Joe" ;
dr [ "Gender" ] = Gender.Male ;
ds.Tables [ "Person" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Person" ].NewRow() ;
dr [ "Id" ] = 1 ;
dr [ "Name" ] = "Mary" ;
dr [ "Gender" ] = Gender.Female ;
ds.Tables [ "Person" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Pet" ].NewRow() ;
dr [ "Id" ] = 0 ;
dr [ "PetType" ] = 0 ;
dr [ "Owner" ] = 0 ;
dr [ "Name" ] = "Spot" ;
dr [ "Gender" ] = Gender.Male ;
dr [ "Age" ] = 2 ;
ds.Tables [ "Pet" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Pet" ].NewRow() ;
dr [ "Id" ] = 1 ;
dr [ "PetType" ] = 0 ;
dr [ "Owner" ] = 0 ;
dr [ "Name" ] = "Rover" ;
dr [ "Gender" ] = Gender.Male ;
dr [ "Age" ] = 5 ;
ds.Tables [ "Pet" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Pet" ].NewRow() ;
dr [ "Id" ] = 2 ;
dr [ "PetType" ] = 1 ;
dr [ "Owner" ] = 0 ;
dr [ "Name" ] = "Fluffy" ;
dr [ "Gender" ] = Gender.Female ;
dr [ "Age" ] = 3 ;
ds.Tables [ "Pet" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Pet" ].NewRow() ;
dr [ "Id" ] = 3 ;
dr [ "PetType" ] = 1 ;
dr [ "Owner" ] = 1 ;
dr [ "Name" ] = "Gilligan" ;
dr [ "Gender" ] = Gender.Male ;
dr [ "Age" ] = 4 ;
ds.Tables [ "Pet" ].Rows.Add ( dr ) ;
dr = ds.Tables [ "Pet" ].NewRow() ;
dr [ "Id" ] = 4 ;
dr [ "PetType" ] = 1 ;
dr [ "Owner" ] = 1 ;
dr [ "Name" ] = "Ginger" ;
dr [ "Gender" ] = Gender.Female ;
dr [ "Age" ] = 4 ;
ds.Tables [ "Pet" ].Rows.Add ( dr ) ;
return ;
}
private void Form1_Load ( object sender , System.EventArgs e )
{
System.Windows.Forms.TreeNode person ;
System.Windows.Forms.TreeNode pettype ;
System.Windows.Forms.TreeNode pet ;
foreach ( System.Data.DataRow personrow in ds.Tables [ "Person" ].Rows )
{
person = new System.Windows.Forms.TreeNode ( string.Format ( "{0} {1} ({2})" , (int) personrow [ "Id" ] , (string) personrow [ "Name" ] , personrow [ "Gender" ] .ToString() ) ) ;
person.Tag = personrow ;
foreach ( System.Data.DataRow pettyperow in ds.Tables [ "PetType" ].Rows )
{
pettype = new System.Windows.Forms.TreeNode ( (string) pettyperow [ "Type" ] + "s" ) ;
pettype.Tag = pettyperow ;
pettype.ForeColor = System.Drawing.Color.FromName ( (string) pettyperow [ "DisplayColor" ] ) ;
ds.Tables [ "Pet" ].DefaultView.RowFilter = string.Format ( "Owner={0} and PetType={1}" , personrow [ "Id" ] , pettyperow [ "Id" ] ) ;
foreach ( System.Data.DataRowView petrow in ds.Tables [ "Pet" ].DefaultView )
{
pet = new System.Windows.Forms.TreeNode ( string.Format ( "{0} {1} {2} year old {3}" , (int) petrow [ "Id" ] , (string) petrow [ "Name" ] , (int) petrow [ "Age" ] , petrow [ "Gender" ] .ToString() ) ) ;
pet.Tag = petrow ;
pet.ForeColor = pettype.ForeColor ;
pettype.Nodes.Add ( pet ) ;
}
person.Nodes.Add ( pettype ) ;
}
treeView1.Nodes.Add ( person ) ;
}
}
}
}
|
|
|
|
|
Hi friends!
I have some problems with GUI in C#.Net ,Can anybody solve these?
First ,How can I save the image from the PictureBox to the Harddrive…. Is it wrong method or not for save the any image ? like as
//Save file
private void savebt_Click(object sender, System.EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filename =saveFileDialog1.FileName;
Stream filestream =File.OpenWrite(filename);
using(StreamWriter fwriter= new StreamWriter(filestream))
{ fwriter.Write(PictureBox.Image);}
}
}
Second , I use the following codes for drawing into the pictureBox2 by Pen
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 5);
g = pictureBox2.CreateGraphics();
if (e.Button == MouseButtons.Left)
{ g.DrawEllipse(p, e.X, e.Y, 5, 7); }
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 5);
g = pictureBox2.CreateGraphics();
if (e.Button == MouseButtons.Left)
{ g.DrawEllipse(p, e.X, e.Y, 5, 7); }
}
But there is a problem when I RUN the program and start the drawing if when I move the mouse fastly then such drawing break into small ellipse,I think you have understand
Third , I use follwing codes to draw the line into the pictureBox2 :
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{ Pen p = new Pen(Color.Red, 5);
g = pictureBox2.CreateGraphics();
g.DrawEllipse(p, e.X, e.Y, 2, 2);
mx = e.X;
my = e.Y;
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 5);
g = pictureBox2.CreateGraphics();
g.DrawLine(p, mx, my, e.X, e.Y);
}
But I confuse for that
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
???????????????????????????????
}
I want when I move the mouse arrow onto the pictureBox2 ,then the line move with respect to the mouse and not draw untill UP the mouse button.
I am waiting for your kindly response
Thanks
|
|
|
|
|
Hi,
1.
to save an image, use Image.Save()
2.
dont draw in MouseDown or any other non-paint event handler;
all drawing should be done in the paint handler.
Hence, use data structures to describe your drawing, modify the data as you
like (e.g. in OnMouseDown), but draw it in OnPaint().
There are many benefits now:
- the drawing gets redrawn when necessary (e.g. on Resize, restore from
minimized, etc)
- the drawing can be saved by saving the data structures
- better code structure.
|
|
|
|
|
Hi,
My first problem ...50% solved ,i used your hint like as
[Code]
private void button10_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image.Save(saveFileDialog1.FileName);
}
}[/code]
But i amazed when i save the image from pictureBox2 (after draw the some lines onto the pictureBox2).......when i opened the saved picture then.........there is no editing means image still unchanged !!!!!!!!!
I want that i open the any image in my c#program ,edit the such image(draw some lines etc on the image)and then save such image but with changing............................
OnPaint()???? How i can use this? Can you give a example?
how can i replace the "pictureBox2_MouseDown" with
Onpaint() handler?
[code]private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 5);
g = pictureBox2.CreateGraphics();
if (e.Button == MouseButtons.Left)
{ g.DrawEllipse(p, e.X, e.Y, 5, 7); }
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 5);
g = pictureBox2.CreateGraphics();
if (e.Button == MouseButtons.Left)
{ g.DrawEllipse(p, e.X, e.Y, 5, 7); }
}[/code]
Thanks !
|
|
|
|
|
Hi all;
I have a windows desktop application in which I have set as a background image, a graphic that I whipped up. Its nothing fancey, just some gradient colors, shadows, glow, that sort of stuff. I have tried different formats, jpg, png, gif, bmp, and with every one, when the application starts, and when I focus to a different window, then come back to the main one, it seems to redraw the screen slowly. Each control will flash on the screen, total time is maybe 1.5 - 2 seconds as all the controls flash. If I put regular system colors back in, there is no issue. Is there anything I can do to speed this up? the graphic files are not that big, 9 KB for the jpb image, 6 for the GIF. The BMP is large, but it doesnt seem to make a difference which one I use, the results are the same.
Thanks
I should note that the backgrounds are set on group boxes, and a few on some buttons.
|
|
|
|
|
I'm looking for the best way to scan (go through) all the files on the computer (for search purposes etc.). i tried some ways but there's certain problems i couldn't find a solution to, such as a "no access" exception when it gets to the "system volume information" dir.
so...what's the best way to do it?
|
|
|
|
|
You can't get access to the System Volumn Information folder anyway. It belongs to the system and only the SYSTEM has any rights to what's in there.
Strangely enough, you're saying that you can't get into this folder when you try to build an index of all the data and files on the drive, but yet, that's exactly what's in that folder. The Indexing Service already does this for you and stores it's index information in that folder.
You might want to get a little primer by reading Heath Stewerts article on it here[^]. There's a link in the article to the documentation on MSDN. I highly suggest following it.
|
|
|
|
|
uhh..that's ASP.NET
and i didn't say i WANTED access to System volume information...it just lists that directory along with all the others and when it tries to access it, it raises an exception and terminates the whole operation =\
|
|
|
|
|
So catch the exception and skip that directory !
|
|
|
|
|
sharpiesharpie wrote: uhh..that's ASP.NET
uhh... the EXACT same techniques are used for every other file on the system, not just ASP.NET files. Next time, try reading the ENTIRE article and following the links in it.
sharpiesharpie wrote: and i didn't say i WANTED access to System volume information
I didn't say you did either.
sharpiesharpie wrote: it just lists that directory along with all the others and when it tries to access it, it raises an exception and terminates the whole operation =\
It terminates because your code isn't written properly to handle errors where your code can't go.
I told you, the Indexing Service already does this. All you have to do is apply the techniques found in that article and the MSDN documentation and you have a solution that's alomst pre-written.
|
|
|
|
|
I am trying to determine primary key in a table wher Table is fetched from sql server database, but following code is not returning me Primary Key.
private void GetPrimaryKeys(DataTable myTable)
{
DataColumn[] pkSource;
pkSource = myTable.PrimaryKey;
for (int i = 0; i < pkSource.Length; i++)
{
Console.Write(pkSource[0].ColumnName[i]);
}
}
|
|
|
|
|
Make sure that you mytable contains information about the existing table fetched from sql server
|
|
|
|
|
I have a decimal value that I need to evaluate in an IF statement however I'm getting an error that reads "Operator '>' cannot be applied to operands of type 'decimal' and 'double'". What am I doing wrong?
Here's my code:
------------------
decimal totalRecPct;
totalRecPct = Decimal.Divide(4,7);
if(totalRecPct > .10)
{
//perform process here
}
----------------
Thanks.
-Goalie35
|
|
|
|
|
you need to cast one value to the other type.
if(totalRecPct > Decimal(.10))
|
|
|
|
|
Compare it to a decimal value:
if (totalRecPct > .10M)
---
single minded; short sighted; long gone;
|
|
|
|
|
Hi,
Please let me know how to populate the erst of the code where I have indicated with ????
In brief, I am looping through a hashtable which contains several hashtables and a dictionary.
The bit where indicated as ???? is where it goes through the items in the dictionary to retrieve ket and value.
Thanks
foreach (DictionaryEntry de in Globals.CacheHashTablesMain)
{
Hashtable subHT = de.Value as Hashtable;
if (subHT != null)
{
foreach (DictionaryEntry deSub in subHT)
{
string k = deSub.Key.ToString();
string h = deSub.Value.ToString();
}
}
else
{
????
}
}
|
|
|
|
|
You haven't stated what you're trying to do in that part of the code.
|
|
|
|
|
|
Greets @ all
I am writing a installer helper module for a program my company puts out. This module is kind of a dummy c#2005 module that just creates and sets the properties on an account for our program to use for it's service to use, while only having the rights it needs. I am having trouble getting the account properties set to their proper values, however. What I still need to do to this accounts it:
* make it not logon-able (when installed on xp i should see no entry on the welcome screen, server and xp without welcome screen should just disallow direct logon) but it of course, still needs to be usable for running a service under
* no profile directory created
* added to local Backup Operators group
Now, if i remember correctly, no profile is actually created until the first logon, so that should not be an issue.
Thanks for any insight anyone can provide
ccitt
|
|
|
|
|
So what i need to do is apply the SeDenyInteractiveLogonRight right to the account.
Does anyone know how to do this in C#? (preferably using DirectoryEntry)
Thanks again
ccitt
|
|
|
|
|
hi
i want to change color of some words in my richTextBox, and i write this code :
string str = this.richTextBox1.Text;<br />
char[] ch = new char[] { ' ' };<br />
int num = 0;<br />
foreach (string s in str.Split(ch))<br />
{<br />
if (s == "that")<br />
{<br />
int indexStart = this.richTextBox1.Text.IndexOf(s);<br />
this.richTextBox1.Select(indexStart, s.Length);<br />
this.richTextBox1.SelectionColor = Color.Red;<br />
num++;<br />
}<br />
}<br />
this.textBox2.Text = num.ToString();
but only change color of the first word that found, and not change color of all words that found, what is my problem and how to solve it ?
thanks
|
|
|
|
|
It's because you're finding the index of the first instance. Use one of the overloads of String.IndexOf[^] to specify a starting index that does not include the last found item.
|
|
|
|
|
Hey everyone,
I have a dataset which stores the results of a query that retrieves information from three tables . The thing is i bound it to a binding navigator and now i want to display the fields in text boxes or labels . My problem is, i dont know how to bind the navigator to the text boxes. The navigor has information in it, 3 records to be exact which was returned from my query, but now i dont know how to view it in a series of text boxes .
This is a code sample:
where bs is my binding source,
bngLoop is my binding Navigator,
and ds is my data set;
{
ds.Load(read, LoadOption.OverwriteChanges,
new string[{ "Result" });
bs.DataMember = "Result";
bs.DataSource = ds;
bngLoop.BindingSource = bs;
txtName.DataBindings.Add(new Binding("Text", bs, "Result.Client_Name", false));
}
i keep on getting this error:
DataMember Property 'Result' cannot be found on the DataSource.
Please help me
Phoenix
Phoenix
|
|
|
|
|
Hi,
I have a propertyholding object, that I use a PropertyGrid to view and edit.
The property I am having problems with is of type TimeSpan.
When the property is displayed in the PropertyGrid, it is done in the format hh:mm:ss, and nothing I do can convince the Grid, that I should be allowed to enter TimeSpans greater than 23:59:59!
If I programmatically enter a greater value and then bind to the grid, it correctly displays the value and even lets me edit it...
Please help out poor, simple noob!
Here's the (pseudo-)code of the object, I pass to the PropertyGrid:
public class PropertyHolder
{
Private TimeSpan _Duration;
PropertyHolder(){}
[...]
Using System;
Using System.ComponentModel;
[CategoryAttribute("Base Properties"), Browsable(true),
BindableAttribute(false), DescriptionAttribute("The time this Activity is planned to take.")]
public TimeSpan Duration
{
get { return _Duration; }
set { _Duration = (TimeSpan)value; }
}
}
Sincerely
Michael Barner
|
|
|
|
|
Hello Michael,
from what I can read in the docs, the format that will be understood when Parse is called is [-][d.]hh:mm:ss[.ff]
Anyway this is a very good case candidate for the new Smart FieldPackEditor component. I will allow edition of time durations in an intuitive manner.
|
|
|
|
|