|
See here[^] maybe it is some helpful to you
|
|
|
|
|
Hello Everyone,
I've got a little problem that I can't seem to find a straight answer to so I'm throwing it out there for you guys to take a stab at it.
I have a UserControl being developed that has two panel controls.
I have already set the designer attribute to allow child controls at design time
<br />
[DesignerCategoryAttribute("Component")]<br />
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]<br />
[ToolboxBitmap(typeof(XPanel),"Image1.bmp")]<br />
public partial class XPanel : UserControl<br />
now i can drag a control on it as is no problem.
what i need to do is when a control is added send it to one of the internal panel's controls collection.
i've tried using the ControlAdded and ControlRemoved events to accomplish this
<br />
private void XPanel_ControlAdded(object sender, ControlEventArgs e)<br />
{<br />
this.DockingPanel.Controls.Add(e.Control);<br />
}<br />
<br />
private void XPanel_ControlRemoved(object sender, ControlEventArgs e)<br />
{<br />
this.DockingPanel.Controls.Remove(e.Control);<br />
}<br />
when i drag a control on to it now i get a pop up in visual studio that says
'child' is not a child control of this parent
if someone could tell me what i'm doing wrong or what i'm missing it would be greatly appreciated.
Ryan Strope
|
|
|
|
|
Hello,
I think the problem is the ControlRemoved EventHandler.
If you add a Control the code in the ControlAdded EventHandler has the effect, that also the code in ControlRemoved is going to be worked threw.
Maybe the solution is to set a boolean flag in the ControlAdded code.
private bool comesfromdesigner=true;
Try that:
private void XPanel_ControlAdded(object sender, ControlEventArgs e)
{
comesfromdesigner = false;
this.XPanel.Controls.Remove(e.Control);
this.DockingPanel.Controls.Add(e.Control);
comesfromdesigner = true;
}
private void XPanel_ControlRemoved(object sender, ControlEventArgs e)
{
if(comesfromdesigner)
this.DockingPanel.Controls.Remove(e.Control);
}
But I'm not sure if you need the ControlRemove code at all!
All the best,
Martin
|
|
|
|
|
Thanks for the effort but no dice there,
tried that and it still didn't work.
Sorry for the short response on the way into the office
Ryan Strope
|
|
|
|
|
How can i delete a file created by FileStream
sAqIb
|
|
|
|
|
You can delete all files in the same way:
System.IO.File.Delete("path");
When you ask specifically about files created by a FileStream, then I guess you have a problem with the file being locked.
This can happen if an exception occurs while you are working with the filestream, or if you haven't released the file from the stream again before deleting it.
You should always use Structured Exception Handling when using I/O operations in code:
using System;
using System.IO;
namespace Testing
{
public class Test
{
public static void Main()
{
FileStream fs = null;
try
{
fs = new FileStream(@"c:\test.txt", FileMode.Create);
byte[] content = new System.Text.UTF8Encoding(true).GetBytes("Hi there!");
fs.Write(content, 0, 9);
}
catch (IOException ioe)
{
Console.WriteLine("Something happened");
if(fs != null)
fs.Close();
}
}
}
}
In this case the file will always be closed, and you should be able to delete it.
Kind regards - Jakob
*********************************************
Three kinds of people in the world:
- Those who can count..
- Those who can't!
10 kinds of people in the world:
- Those who understand binary
- Those who don't
|
|
|
|
|
Actually, this code won't work. Catch only runs when an exception occurs, I believe you meant to put the fs.Close code in a finally block ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hehe... that's what you get for replying to a forum post before the first cup'o'mocca in the morning
You're right of course
Corrected code reads:
using System;
using System.IO;
namespace Testing{
public class Test
{
public static void Main()
{
FileStream fs = null;
try
{
fs = new FileStream(@"c:\test.txt", FileMode.Create);
byte[] content = new System.Text.UTF8Encoding(true).GetBytes("Hi there!");
fs.Write(content, 0, 9);
}
catch (IOException ioe)
{
Console.WriteLine("Something happened");
}
finally
{
if(fs != null)
fs.Close();
}
}
}
}
Thx for the correction Christian!
Kind regards - Jakob
*********************************************
Three kinds of people in the world:
- Those who can count..
- Those who can't!
10 kinds of people in the world:
- Those who understand binary
- Those who don't
|
|
|
|
|
thanks buddy.
System.IO.File.Delete("path"); worked for me.
sAqIb
|
|
|
|
|
Great!
Kind regards - Jakob
*********************************************
Three kinds of people in the world:
- Those who can count..
- Those who can't!
10 kinds of people in the world:
- Those who understand binary
- Those who don't
|
|
|
|
|
how can i create mobile projects in C#?
|
|
|
|
|
|
Hi,
I want to transfer the file from client to server.
Just using IPadress only, here i dont use asp and web services.
Any one kindly guide me.
|
|
|
|
|
|
Hi All
We are using te code below for XML parsing, Its a bit slow, anybody knows fater XML parsing in c#?
As I am using for PocketPC, its taking for ever to parse.
please help.
public static ArrayList ParseURL(string strUrl)
{
ArrayList searchItems = new ArrayList();
mbitSearchItem currentSearchItem = new mbitSearchItem();
string lastElementName = "";
try
{
XmlTextReader reader = new XmlTextReader(strUrl);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text:
switch (lastElementName)
{
case "id":
currentSearchItem.id = Convert.ToInt32(reader.Value);
break;
case "fsz":
currentSearchItem.size = Convert.ToInt32(reader.Value);
break;
case "fnm":
currentSearchItem.name = reader.Value;
break;
case "typ":
currentSearchItem.type = Convert.ToInt32(reader.Value);
break;
case "seed":
currentSearchItem.seeders = Convert.ToInt32(reader.Value);
break;
}
break;
case XmlNodeType.Element:
lastElementName = reader.Name;
//Console.WriteLine(lastElementName);
Hashtable attributes = new Hashtable();
string strURI = reader.NamespaceURI;
string strName = reader.Name;
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
attributes.Add(reader.Name, reader.Value);
}
}
StartElement(strURI, strName, strName, attributes);
break;
case XmlNodeType.EndElement:
if (reader.Name.Equals("item"))
{
searchItems.Add(currentSearchItem);
currentSearchItem = new mbitSearchItem();
}
break;
default:
break;
}
}
}
catch (Exception e)
{
Console.WriteLine("error occured: " + e.Message);
}
|
|
|
|
|
You may want to try to changing the xml data type of your integer values to be xsd:integer (or xsd:decimal) instead of converting the text types yourself. I've found the Convert functions to be fairly slow. In .NET 2 you can try
XmlReader.ReadContentAsInt Method. This, or something else to get rid of the ToInt32 calls, may be faster.
-- Bob
|
|
|
|
|
Hi Bob
I am implementing XmlReader.ReadContentAsInt Method.Now my application builds faster.
Thanks a lot
|
|
|
|
|
Perhaps I have a problem with public/private?
I have a Form generated in C#, the second Form called "MainForm" is included in the Project and visible in the projectexplorer, like a normaly generated Form.
But on de Code of Form1 or so, I can't access to "MainForm"? Its declared as public. I don't understand why MainForm.show etc doesnt work?
Any suggestions?
|
|
|
|
|
Because MainForm is a class name. You can't show a class, only an instance of a class.
private MainForm form;
...
form.Show();
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
That means i have to instanciate (or how its called in english) the class?
1. Has the class MainForm to be public or is that not necesarry?
2. How can i access to MainForm? Do I have to do this like :
Mainformfoo Mainform = new Mainformfoo();
I've read http://www.csharp-station.com/Tutorials/Lesson07.aspx[^] but I didn't see the solution... perhaps I'll change to VB
|
|
|
|
|
The class needs to be visible to where you're calling it from. The instance does not have to be public, no.
Yes, that's how you create an instance of the MainformFoo class. Remember, this is one instance, if you create OTHER instances, interacting with those will not effect the instance you created before. So if you want to change some property on your form, you can't just do the above to get an instance and call methods, that will not change any other instance in your code.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi,
I am trying to read a rss feed and create a linklabel for the items within a panel on a form. I have a class called LinkLabelArray and that has the following:
public void AddNewLinkLabel()
{
LinkLabel aLabel = new LinkLabel();
this.List.Add(aLabel);
Form1.ActiveForm.panel1.Controls.Add(aLabel);
//set linklabel properties
aLabel.AutoSize = true;
aLabel.LinkVisited = true;
aLabel.Visible = true;
aLabel.Top = 5;
aLabel.Height = 15;
aLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// handler for clicked event of news title label
aLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(ClickHandler);
}
I can't get the linkLabel to be added to the panel on my form (Form1). I have tried everything I can think of and I still get this error - 'System.Windows.Forms.Form' does not contain a definition for 'panel1' even though I have a panel on Form1 called panel1 and it's modifier value is set to public. public System.Windows.Forms.Panel panel1;.
Can someone please help me!
Thanks,
Al
|
|
|
|
|
The ActiveForm property returns an instance of System.Windows.Forms.Form class which contains no panel1 property as the error message clearly says. To access the panel1 variable you will need to cast the return value to your Form1 type but be sure that ActiveForm always returns a Form1 object.
((Form1)Form1.ActiveForm).panel1.Controls.Add(aLabel);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
I'm trying to create an object that will deserialize xml into a dictionary. So far I have most of the stuff working however when I do something like
this.myList.Add(myItem);
I expect to get a new item that in my list that is equal to myItem. However, for some reason the List updates all of the containing items in the List to equal the most recently added item.
Any idea what's going on?
Here is a the serializable class in question if it helps:
public class WeatherCities : IXmlSerializable<br />
{<br />
[XmlElement]<br />
public List<city> City;<br />
public WeatherCities()<br />
{<br />
}<br />
<br />
public class city<br />
{<br />
private string cityName;<br />
private string cityCode;<br />
<br />
[XmlElement]<br />
public string CityName<br />
{<br />
get { return cityName; }<br />
set { cityName = value; }<br />
}<br />
<br />
[XmlElement]<br />
public string CityCode<br />
{<br />
get { return cityCode; }<br />
set { cityCode = value; }<br />
}<br />
<br />
const string NS = "http://www.develop.com/xml/serialization";<br />
<br />
[XmlElement]<br />
public IDictionary<string, string> LocalizedNames;<br />
<br />
}<br />
<br />
public void ReadXml(XmlReader r)<br />
{<br />
this.City = new List<city>();<br />
while (r.Read())<br />
{<br />
city myCity = new city();<br />
myCity.LocalizedNames = new Dictionary<string, string>();<br />
<br />
while (r.Name == "City")<br />
{<br />
if (r.NodeType == XmlNodeType.EndElement)<br />
r.Read();<br />
else<br />
{<br />
r.Read();<br />
bool bLocalizedNameFound = false;<br />
myCity.CityCode = "";<br />
myCity.CityName = "";<br />
myCity.LocalizedNames.Clear();<br />
while (!bLocalizedNameFound)<br />
{<br />
switch (r.Name)<br />
{<br />
case "CityName":<br />
myCity.CityName = r.ReadElementString();<br />
break;<br />
case "CityCode":<br />
myCity.CityCode = r.ReadElementString();<br />
break;<br />
case "LocalizedNames":<br />
bLocalizedNameFound = true;<br />
break;<br />
}<br />
}<br />
<br />
string locale = "";<br />
string localizedname = "";<br />
bool localefound = false;<br />
bool endlocalnamefound = false;<br />
<br />
r.Read();
r.Read();
<br />
while (r.NodeType != XmlNodeType.EndElement && !endlocalnamefound)<br />
{<br />
switch (r.Name)<br />
{<br />
case "Locale":<br />
locale = r.ReadElementString();<br />
localefound = true;<br />
break;<br />
case "LocalizedName":<br />
localizedname = r.ReadElementString();<br />
break;<br />
}<br />
<br />
if (localefound == true && localizedname != "")<br />
{<br />
myCity.LocalizedNames.Add(locale, localizedname);<br />
localefound = false;<br />
localizedname = "";<br />
}<br />
<br />
if (r.NodeType == XmlNodeType.EndElement && r.Name == "LocalName")<br />
{<br />
r.Read();
if (r.Name == "LocalizedNames")<br />
endlocalnamefound = true;
r.Read();
}<br />
}<br />
<br />
this.City.Add(myCity);<br />
}<br />
}<br />
}<br />
}<br />
<br />
public void WriteXml(XmlWriter w)<br />
{<br />
}<br />
public System.Xml.Schema.XmlSchema GetSchema()<br />
{<br />
return LoadSchema();<br />
}<br />
private System.Xml.Schema.XmlSchema LoadSchema()<br />
{<br />
return null;<br />
}<br />
<br />
}<br />
}
|
|
|
|
|
I think the problem is that you're always adding the same object or to be more specific a reference to the same object. Try moving the creation of the city object into the second while loop.
while (r.Read())
{
while (r.Name == "City")
{
city myCity = new city();
myCity.LocalizedNames = new Dictionary();
...
}
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|