|
Hi all,
I have a ToolStripMenuItem in my MenuStrip . I've added a few ToolStripMenuItem s as dropdown items at design time. Now I'd like to copy that list of dropdown items to a ToolStripDropDownButton at runtime but I seem to move the items from the ToolStripMenuItem to the ToolStripDropDownButton , not duplicate it.
Here is the code I'm using:
ToolStripItem[] newitems = new ToolStripItem[myToolStripMenuItem.DropDownItems.Count];
myToolStripMenuItem.DropDownItems.CopyTo(newitems, 0);
myToolStripDropDownButton.DropDownItems.AddRange(newitems);
But the problem is that as soon as the last line executes the DropDownItems collection for the ToolStripMenuItem becomes empty. Does anyone have advice for me? Thanks in advance.
|
|
|
|
|
I think your issue is that your array is essentially storing references of each button - so your last line is in effect adding the exact same buttons (and will automatically become a "move")
What you should do is...
- Iterate the existing list of items
- create new items based on the existing ones
- add the new one to where you want them
this should provide a "copy" for you
foreach(ToolStripItem item in myToolStripMenuItem.DropDownItem){
ToolStripItem newItem = new ToolStripItem();
myToolStripDropDownButton.DropDownItems.Add(newItem);
}
return 5;
|
|
|
|
|
First of all, the ToolStripItem() constructor is protected so your first line in the foreach loop is already going to cause a problem. Secondly, it's that comment line of yours that is the big headache. It means I have to copy the event handler, the tooltip text and everything.
Is it a fact that a ToolStripItem can belong to only one DropDownItem collection?
|
|
|
|
|
The code was more for a idea than actual use - I have not worked with them for a while and forget the actual usage but I have created them dynamically is the past so I know you can create an instance somehow.
Again, its been a while so I didn't know if there was a 'clone' type of function perhaps. But considering it is a button surely you will only need to copy a few key values such as Text, Icon (if any) and the Click eventHandler.
ToolStripItem is a Control, and a Control can only have one parent Control - Basically what you have inadvertently ended up doing is changing the parent control - this is why it appears to "Move" (I don't know much about how this happens 'behind the scenes' thou)
return 5;
|
|
|
|
|
Had a little play now.. are your items all the same type? i.e. ToolStripButton as you can create all those controls.
This is pretty messy but just an idea of what I was playing with...
foreach (ToolStripItem item in toolStripDropDownButton1.DropDownItems)
{
ToolStripItem newItem = new ToolStripMenuItem();
if (item is ToolStripComboBox)
newItem = new ToolStripComboBox();
else if (item is ToolStripTextBox)
newItem = new ToolStripTextBox();
newItem.Text = item.Text;
toolStripDropDownButton2.DropDownItems.Add(newItem);
}
return 5;
|
|
|
|
|
Hi
i want to implement deisgner at runTime in my app, to do this i 've create a simple win app and write this code to my form :
public partial class Form1 : Form
{
IServiceContainer isc;
DesignSurface ds;
IDesignerHost idh;
ISelectionService iss;
IToolboxUser itu;
MyToolBox toolbox;
public Form1()
{
isc = new ServiceContainer();
ds = new DesignSurface(isc);
ds.BeginLoad(typeof(Form));
idh = (IDesignerHost)ds.GetService(typeof(IDesignerHost));
iss = (ISelectionService)ds.GetService(typeof(ISelectionService));
itu = (IToolboxUser)idh.GetDesigner(idh.RootComponent);
InitializeComponent();
iss.SelectionChanged += new EventHandler(iss_SelectionChanged);
}
private void Form1_Load(object sender, EventArgs e)
{
toolbox = new MyToolBox();
toolbox.Dock = DockStyle.Fill;
splitContainer1.Panel1.Controls.Add(toolbox);
isc.AddService(typeof(IToolboxService), toolbox);
Control c = ds.View as Control;
c.Parent = this.splitContainer2.Panel1;
c.Dock = DockStyle.Fill;
}
private void iss_SelectionChanged(object sender, EventArgs e)
{
propertyGrid1.SelectedObject = iss.PrimarySelection;
}
}
and also create a new class named 'ToolboxServiceImp' which inherits from ListBox and IToolboxService as follow :
Please download my sample app from http://cid-1490cc59dd735cbf.office.live.com/self.aspx/.Public/Designer2.rar
my problem is that, at runTime when i drag & drop a control from my toolbox on the form, i got this error :
Error
---------------------------
Unable to cast object of type 'System.RuntimeType' to type 'System.Drawing.Design.ToolboxItem'.
---------------------------
OK
also, when i click on form, i got this error and designer become corrupt :
Error
---------------------------
The control System.Windows.Forms.Form has thrown an unhandled exception in the designer and has been disabled.
Exception:
Unable to cast object of type 'System.RuntimeType' to type 'System.Drawing.Design.ToolboxItem'.
Stack trace:
---------------------------
OK
where is my problem and how to solve it ?
thanks in advance
|
|
|
|
|
I don't plan to download your app, but I can guess that somewhere in your code you are trying to cast some object to a ToolboxItem , that does not have that class in its object hierarchy. You need to create the object of the correct type first.
I must get a clever new signature for 2011.
|
|
|
|
|
|
hdv212 wrote: do u have problem to download from this link ?
Firstly, I do not download from external links. Secondly, I cannot unpack .rar files.
hdv212 wrote: i found several example and source codes to demonstrate this, but all of them create a some new classes and write more complex code.
I don't know what your experience level is, but mine is nowhere good enough to advise on the Microsoft sample.
I must get a clever new signature for 2011.
|
|
|
|
|
Hi
do u have problem to download my sample file ?
do u have live account ?
|
|
|
|
|
No to both questions. But as I said above, I don't have anything else to suggest to you.
I must get a clever new signature for 2011.
|
|
|
|
|
If you are interested in this sort of thing, you might want to look at the source for SharpDevelop[^] to see how they do it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!
|
|
|
|
|
|
hdv212 wrote: all of them create a some new classes and write more complex code.
It is a complex topic.
If you are looking for some simpler implementations, then maybe look at things like UML designers or Flow Chart designers. There are a fair few of those out there.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!
|
|
|
|
|
Hi!!
i'm getting this error when i try to share screen from my notebook connected to the router using wifi n my pc by ethernet
"System.ArgumentException: Value does not fall within the expected range.
at RDPCOMAPILib.IRDPSRAPIViewer.Connect(String bstrConnectionString, String bstrName, String bstrPassword)
at AxRDPCOMAPILib.AxRDPViewer.Connect(String bstrConnectionString, String bstrName, String bstrPassword)"
this error comes each time i try to connect it
axRDPviewer.connect(Connstring,"user1", "");
I cant get this corrected...
It works perfect when i do it on my pc
but aint working when try to share my notebook screen...
the Connstring is being sent by Tcp message(Send and receive)...
Can u tell me where is the source of the problem... How can i take care of this???
|
|
|
|
|
Can you connect to your notebook when you use Microsoft's Remote Desktop? That's a good way to find out where the problem is located: e.g. remote access not allowed on your notebook, empty passwords for remote access not accepted...
|
|
|
|
|
when im using that RDPCOMAPILib, the aero style dosen't working ! how can we solve that problem!? can somebody help me ?
|
|
|
|
|
Hello Experts,
I would like to ask if what's wrong with my program. I have created 1 client and 1 server that will accept the message
of the client but I got this error.
Cross-thread operation not valid: Control 'lbConnections' accessed from a thread other than the thread it was created on.
For more details here is my code for the Client that send message to server.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
namespace UDP_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UdpClient udpClient = new UdpClient();
udpClient.Connect(tbHost.Text, 8080);
Byte[] sendBytes = Encoding.ASCII.GetBytes("Hello World?");
udpClient.Send(sendBytes, sendBytes.Length);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Here also is the code to my server which were I got the error thats is mention above.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace UDP_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void serverThread()
{
UdpClient udpClient = new UdpClient(8080);
while (true)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
lbConnections.Items.Add(RemoteIpEndPoint.Address.ToString() + ":" +
returnData.ToString()
);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thdUDPServer = new Thread(new
ThreadStart(serverThread));
thdUDPServer.Start();
}
}
}
Any comments or suggestion to help is kindly appreciated.
Thanks,
DAN
|
|
|
|
|
|
I have 600 xml files and I need to search through 50000 xml files to see which of those 600 exist in the 50000.
I have tried directory.getfiles and it's taking so long I could do it faster by hand I believe.
Does anyone have any ideas on how to do this efficiently? Thanks for reading.
|
|
|
|
|
Hi, I suggest you:
1. use Directory.EnumerateFiles() to avoid the full list in memory
2. which requires .NET 4.0
3. calculate a hash value for each of those files in the small set, storing them in a Dictionary<filename,hashvalue>
4. iterate over the files in the large set, calculating their hash value, then comparing the file content with the files from the small set that have matching hash values.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hi Luc,
Thanks for the reply, I can't use 4.0 yet, only 3.5
|
|
|
|
|
Without .NET 4.0 you have two choices:
1. use Directory.GetFiles()
2. use Win32 functions (GetFirstFile, GetNextFile) through P/Invoke.
I would opt for 1 until proven too inefficient.
There is actually a third way: assuming all filenames start with a letter and all letters have comparable probability, use a loop and the filtered version of GetFiles, i.e. first get all names matching a*, then b*, etc; that way you never have all of them at once.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Good thinking!
return 5;
|
|
|
|
|
A quick test on my system showed Directory.GetFiles took only 215 milliseconds to get 50000 files and Array.Contains took 277 milliseconds to find 600 filenames. Perhaps you have something else happening.
This was assuming you were searching for matched files names not content.
I know the language. I've read a book. - _Madmatt
|
|
|
|