jeudi 26 février 2015

Hide Node in Treeview List. in C#

I am using VS 2005 C#, working on an unfinished WinForm.


I have parsed an XML to a treeview listing but I'm encountering some problems. I wanna know if there's a way to hide/filter/remove a certain node containing a "_this_text" in its name without having to depend on a textbox.


This is what I have for the program (kudos to those who helped me develop this):



#region "***** XML Parsing *****"
private void Form1_Load_1(object sender, EventArgs e)
{
// Initialize the controls and the form.
//label1.Text = "File Path";
textBox2.Text = Application.StartupPath + "\\Continental.vsysvar";
//button6.Text = "XML";
//this.Text = "Software Validation";
}

private TreeNode selectedNode = null;

private void button6_Click(object sender, EventArgs e)
{
try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();
dom.Load(textBox2.Text);

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
/*treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];*/

foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}

treeView1.ExpandAll();
}
/* catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}*/
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void LoadTreeFromXmlDocument(XmlDocument dom)
{
try
{
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();

// SECTION 3. Populate the TreeView with the DOM nodes.
foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}

treeView1.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

static string GetAttributeText(XmlNode inXmlNode, string name)
{
XmlAttribute attr = (inXmlNode.Attributes == null ? null : inXmlNode.Attributes[name]);
return attr == null ? null : attr.Value;
}

private void AddNode(TreeNodeCollection nodes, XmlNode inXmlNode)
{
if (inXmlNode.HasChildNodes)
{
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = inXmlNode.Name;
TreeNode newNode = nodes.Add(text);
XmlNodeList nodeList = inXmlNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xNode = inXmlNode.ChildNodes[i];
AddNode(newNode.Nodes, xNode);
}
}
else
{
// If the node has an attribute "name", use that. Otherwise display the entire text of the node.
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = (inXmlNode.OuterXml).Trim();
TreeNode newNode = nodes.Add(text);
}
}
#endregion


And here's an excerpt of the XMl file since it's quite long:



<?xml version="1.0" encoding="utf-8"?>
<systemvariables version="4">
<namespace name="" comment="">
<namespace name="_01_Test_Preparation" comment="">
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_02_Shipping_Status_Check" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_02_Shipping_Status_Check_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_01_Get_Dem_ID" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_01_Get_Dem_ID_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_04_ECU_Version_Check_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_03_Test_Run_Init" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_04_ECU_Version_Check" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_05_DEM_Reader" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_03_Test_Run_Init_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_05_DEM_Reader_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
</namespace>


What I really want to accomplish is to hide or filter the nodes containing this text "_start" found in their name. So all nodes containing "_this_start" in their name would be hidden from the user. I've read that it is not technically possible to enable or disable visibility but instead gray out the text and whatnot.


Thank you in advance.


XtraReport merge cell vertically in devexpress 12.2.7?

Is there anyway to merge cell vertically in devexpress 12.2.7 xtratable?


I can do it horizontally by deleting cells but not with vertically


Why when downloading html content and saving it as html file on my hard disk the file dosent contain all the content?

I did a simple project for testing using once with httpwebrequest and once with webclient in both cases the html file on my hard disk is 77KB size. While the file size should be abour 363KB.


In the website in chrome when i'm doing right click save as and save it as html file the file size is 363KB.


But in my program the file size is only 77KB.



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 System.Net;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string appDir = Path.GetDirectoryName(@"C:\Users\chocolade1972\test");
string htmlsTargetDirectory = "Tapuz Htmls";
string imagesTargetDirectory = "Tapuz Images";
string combinedHtmlsDir;
string combinedImagesDir;
String targetHtmls;

public Form1()
{
InitializeComponent();

/*combinedImagesDir = Path.Combine(appDir, imagesTargetDirectory);
if (!Directory.Exists(combinedImagesDir))
{
Directory.CreateDirectory(combinedImagesDir);
}
combinedHtmlsDir = Path.Combine(appDir, htmlsTargetDirectory);
if (!Directory.Exists(combinedHtmlsDir))
{
Directory.CreateDirectory(combinedHtmlsDir);
}

for (int i = 1; i < 49; i++)
{
WebClient wc = new WebClient();
targetHtmls = (combinedHtmlsDir + "\\Html" + i + ".html");
wc.DownloadFile("http://ift.tt/1whXSS6" + i,
targetHtmls);
wc.Dispose();
}*/
getsource();
}

private void getsource()
{
/*combinedHtmlsDir = Path.Combine(appDir, htmlsTargetDirectory);
using (var client = new CookiesAwareWebClient())
{
for (int i = 1; i < 49; i++)
{
targetHtmls = (combinedHtmlsDir + "\\Html" + i + ".html");
client.DownloadFile("http://ift.tt/1whXSS6" + i,
targetHtmls);
}
}*/

string urlAddress = "http://ift.tt/1ByXoy3";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;

if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}

string data = readStream.ReadToEnd();

response.Close();
readStream.Close();
combinedHtmlsDir = Path.Combine(appDir, htmlsTargetDirectory);
File.WriteAllText(combinedHtmlsDir + "\\Html1.html", data);
}
}




public class CookiesAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; private set; }

public CookiesAwareWebClient()
{
CookieContainer = new CookieContainer();
}

protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
((HttpWebRequest)request).CookieContainer = CookieContainer;
return request;
}
}


private void Form1_Load(object sender, EventArgs e)
{

}
}
}


The code is bit long since i have here all the tests i did httpwebrequest webclient and also with coockies container. And all results give a file name size 77KB.


In this case i'm now the httpwebrequest. And the url is: http://ift.tt/1ByXoy3


Url


I can't figure out why when doing right click and save as or view source i see the whole source of the html but when saving to the hard disk it's only 77KB. It seems like when i'm saving it to the hard disk it's show only part or different source content of the html.


I also posted before when tried to use webbrowser and there for some reason sometimes it's saving the html file code complete 363KB and sometimes only 77KB or 90KB.


What could be the problem ? Is there any way to solve it ?


How to C# WinForm DataGridView Background Color Transparent?

Similar Thread Set datagrid view background to transparent


DataGridView VB Transparent Code:



Imports System.ComponentModel

Public Class TransparentDGV
Inherits DataGridView

Private _DGVHasTransparentBackground As Boolean

<Category("Transparency"), Description("Select whether the control has a Transparent Background.")> Public Property DGVHasTransparentBackground As Boolean
Get
Return _DGVHasTransparentBackground
End Get
Set(ByVal value As Boolean)
_DGVHasTransparentBackground = value
If _DGVHasTransparentBackground Then
SetTransparentProperties(True)
Else
SetTransparentProperties(False)
End If
End Set
End Property

Public Sub New()
DGVHasTransparentBackground = True
End Sub

Private Sub SetTransparentProperties(ByRef SetAsTransparent As Boolean)
If SetAsTransparent Then
MyBase.DoubleBuffered = True
MyBase.EnableHeadersVisualStyles = False
MyBase.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent
MyBase.RowHeadersDefaultCellStyle.BackColor = Color.Transparent
SetCellStyle(Color.Transparent)
Else
MyBase.DoubleBuffered = False
MyBase.EnableHeadersVisualStyles = True
MyBase.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
MyBase.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control
SetCellStyle(Color.White)
End If
End Sub

Protected Overrides Sub PaintBackground(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal gridBounds As System.Drawing.Rectangle)
MyBase.PaintBackground(graphics, clipBounds, gridBounds)

If _DGVHasTransparentBackground Then
If Not IsNothing(MyBase.Parent.BackgroundImage) Then
Dim rectSource As New Rectangle(MyBase.Location, MyBase.Size)
Dim rectDest As New Rectangle(0, 0, rectSource.Width, rectSource.Height)

Dim b As New Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height)
graphics.FromImage(b).DrawImage(MyBase.Parent.BackgroundImage, Parent.ClientRectangle)
graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel)
Else
Dim myBrush As New SolidBrush(MyBase.Parent.BackColor)
Dim rectDest As New Region(New Rectangle(0, 0, MyBase.Width, MyBase.Height))
graphics.FillRegion(myBrush, rectDest)
End If
End If
End Sub

Protected Overrides Sub OnColumnAdded(ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs)
MyBase.OnColumnAdded(e)
If _DGVHasTransparentBackground Then
SetCellStyle(Color.Transparent)
End If
End Sub

Private Sub SetCellStyle(ByVal cellColour As Color)
For Each col As DataGridViewColumn In MyBase.Columns
col.DefaultCellStyle.BackColor = cellColour
col.DefaultCellStyle.SelectionBackColor = cellColour
Next
End Sub

End Class


Convert C# Code ? Help !!

OR



protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds) {
base.PaintBackground(graphics, clipBounds, gridBounds);
Rectangle rectSource = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
Rectangle rectDest = new Rectangle(0, 0, rectSource.Width, rectSource.Height);

Bitmap b = new Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height);
Graphics.FromImage(b).DrawImage(this.Parent.BackgroundImage, Parent.ClientRectangle);


graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel);
SetCellsTransparent(); }


public void SetCellsTransparent() {
this.EnableHeadersVisualStyles = false;
this.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent;
this.RowHeadersDefaultCellStyle.BackColor = Color.Transparent;


foreach (DataGridViewColumn col in this.Columns)
{
col.DefaultCellStyle.BackColor = Color.Transparent;
col.DefaultCellStyle.SelectionBackColor = Color.Transparent;
} }


Example Project Please or code

I cannot run this code.


Getting Form2 variable to Form1 (User text input in TextBox)

The plan: I want to ask the user with second Form2 to input some text. When this Form2 is closed, i want to display the input text in a textbox on Form1...


On a button event, on Form2 i can reach Form1's textbox:



Form1 form1 = new Form1();


And:



form1.myText = "Test Name";


And then i close Form2:



this.Close();


But the value "Test Name" does not appear in form1's textbox... I don't get an error.


How do I add a timestamp to a gridview

How do I add a timestamp to a gridview that shows the last time a row was edited? I've been searching Google but haven't found anything helpful yet.


I tried to create a method which will update the time when you edit or add a column. but i dont know how to take it further like parsing it to each and every column.


my table columns



private DataTable GetDataTableFromDataGridview(DataGridView _grid)
{
{
var _oDataTable = new DataTable();
object[] cellValues = new object[_grid.Columns.Count];
//clearTable();
_oDataTable.Columns.Add("Name", typeof(string));
_oDataTable.Columns.Add("Value", typeof(string));
_oDataTable.Columns.Add("Font", typeof(string));
_oDataTable.Columns.Add("DateStamp", typeof(string));
_oDataTable.Columns.Add("Comment", typeof(string));
foreach (DataGridViewRow row in _grid.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
_oDataTable.Rows.Add(cellValues.ToArray());
}
return _oDataTable;
}

}


my method



private DateTime _dateTime;
string _DateTimeFormat = "yyyy/dd/MM HH:mm:ss";
public void UpdateTheCurrentTime()
{
_dateTime = DateTime.Now;
_dateTime.ToString(_DateTimeFormat, CultureInfo.InvariantCulture);
}

Per Pixel Collision Detection Algorithm for WindowsForms

I´m searching for a per pixel collision detection algorithm/ method for Windows Forms.


I have searched for it but I only find one for XNA (as you could see below). Isn´t such an algorithm agreeable with the concept of Windows Forms?!



/// <summary>
/// Determines if there is overlap of the non-transparent pixels
/// between two sprites.
/// </summary>
/// <param name="rectangleA">Bounding rectangle of the first sprite</param>
/// <param name="dataA">Pixel data of the first sprite</param>
/// <param name="rectangleB">Bouding rectangle of the second sprite</param>
/// <param name="dataB">Pixel data of the second sprite</param>
/// <returns>True if non-transparent pixels overlap; false otherwise</returns>
static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
Rectangle rectangleB, Color[] dataB)
{
// Find the bounds of the rectangle intersection
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);

// Check every point within the intersection bounds
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
// Get the color of both pixels at this point
Color colorA = dataA[(x - rectangleA.Left) +
(y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) +
(y - rectangleB.Top) * rectangleB.Width];

// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
{
// then an intersection has been found
return true;
}
}
}


The Problem with that one is, that I don´t know how to initialize the Color[] Arrays.