This is a common exception. It happens when you do something like:
ListItem li1 = new ListItem();
li1.Text = "Item 1";
li1.Value = "id1";
li1.Selected = true;
ListItem li2 = new ListItem();
li2.Text = "Item 2";
li2.Value = "id2";
li2.Selected = true;
ddTest.Items.Add(li1);
ddTest.Items.Add(li2);
Let's explain what is going on.
We created a ListItem, set its name and value and make it to be selected.
Then we created another ListItem, again set its value and name and set it to be selected.
| When we add both ListItems the DropDownList confuses about which one should be selected and throws an exception. But why? Because there is another control - ListBox, which allows multiple selection. If we create a ListBox on the form and add the following code in the code behind: |
ListItem li1 = new ListItem();
li1.Text = "Item 1";
li1.Value = "id1";
li1.Selected = true;
ListItem li2 = new ListItem();
li2.Text = "Item 2";
li2.Value = "id2";
li2.Selected = true;
lb1.SelectionMode = ListSelectionMode.Multiple;
lb1.Items.Add(li1);
lb1.Items.Add(li2);
We will run with no errors and both items will be selected. The problem with the DropDownList control is that shares almost the same functionality with the ListBox and for that reason they both inherit the same base objects.
But the DropDownList doesn't have the ability to display multiple items so ASP.NET team decided to throw an exception when such situation occurs.
| This can be confusing in more complicated code, but it is definatelly better than selecting the last item which was marked as "selected" or the first one or something like that. Luckily there are safer ways to select an item without having to worry if there is already selected item or not.
|
You can use DropdownList.SelectedIndex or DropdownList.SelectedValue properties to mark selected item in a safe manner. So if in the firs example we wanted to select the item with id2 we can use the following code:
ListItem li1 = new ListItem(); li1.Text = "Item 1"; li1.Value = "id1";
ListItem li2 = new ListItem(); li2.Text = "Item 2"; li2.Value = "id2";
ddTest.Items.Add(li1); ddTest.Items.Add(li2);
ddTest.SelectedValue = "id2";
I commonly use this as in most cases I know which value should I select but don't know which index it has. Now, there is a situation in which you may want to select by index and this in my imagination is the following situation: Imagine you have the above DropDownList but you want it to be optional. This means that you need to have an item with empty value or value that means "null" to you. Imagine that this value is already inserted in the DropDownList. You know it will always be the first item. Here is the snippet:
ListItem liNull = new ListItem(); liNull.Text = "-- Please Select an Item --"; liNull.Value = "";
ListItem li1 = new ListItem(); li1.Text = "Item 1"; li1.Value = "id1";
ListItem li2 = new ListItem(); li2.Text = "Item 2"; li2.Value = "id2";
// Someone selected item in the code: ddTest.SelectedValue = "id1";
ddTest.Items.Add(liNull); ddTest.Items.Add(li1); ddTest.Items.Add(li2);
// We will select the first one which is "-- Please Select an Item --" ddTest.SelectedIndex = 0;
But in such cases we can take advantage of the default DropDownList behaviour (the DropDownList will select the first item if there are no items selected explicitly). So if you need the first item selected you can simply clear the selection and ASP.NET will select it for you. But how? By using the ClearSelection() method. It does what it says - it clears the selected item. Here is the last snippet:
ListItem liNull = new ListItem(); liNull.Text = "-- Please Select an Item --"; liNull.Value = "";
ListItem li1 = new ListItem(); li1.Text = "Item 1"; li1.Value = "id1";
ListItem li2 = new ListItem(); li2.Text = "Item 2"; li2.Value = "id2";
// Someone selected item in the code: ddTest.SelectedValue = "id1";
ddTest.Items.Add(liNull); ddTest.Items.Add(li1); ddTest.Items.Add(li2);
// We will be sure that everything is clean // and will expect ASP.NET to select the first item // by default: ddTest.ClearSelection();
| I've been very busy this month. All the exams (for microsoft certification, in the faculty and so on), my extended work day (from 9 to 20 as I was absent |
from work for about a month and need to do some extra work to catch my collegues), some ongoing projects I was stupid enough to take and so on... However, I've found some time to start two new blogs which I consider more as experiment than real blogs: Sql Exceptionsand : .NET ExceptionsBoth blogs currently contain about 40 posts with Sql and .NET Exceptions which you may find helpful after time. I am saying after time as I plan to automate those blogs so the information there is posted by software and not by myself. Also users are welcome to post comments. Soon I will publish special format of posts which you can use in order to give your opinion on specific exception and this exception will be automatically published in the post. Unfortunatelly there is a limitation - Blogger allow me to only publish about 40 posts daily so I wasn't able to create posts about all the exceptions in SQL and .NET Framework. I will batch post each day (may miss some of the days like weekends for example ;) so in few weeks both blogs will be up to date.
| Well, a lot of people don't consider showing someone on the official asp.net site a big deal. There is a box "Community recognition program" where the most active members as well as yesterday's most active members are shown. I had 102 points for the previous day so I appeared. It really isn't big deal, but I consider it a big start, because it gave me motivation to continue to contribute. It is not about the recognition. In asp.net site you can find a lot of friends also. It is great to help someone and then see something like : "Dude, you're |
awesome, you pointed me in the right direction." or "Dude, you saved me a lot of hours" and so on. There are a lot of things I would like to learn about asp.net and I think this forum is a great place to start. Just post a question and in most cases in few minutes some of the experts there will answer you. Here is a screenshot I will keep close to my heart :D
| Asuming you have InternetExplorerClass instance, named ie, you can do the following to obtain screenshot:
int screenWidth = ie.Width; int screenHeight = ie.Height;
#region Get IE Bitmap
Bitmap b = new Bitmap(ie.Width, ie.Height); Graphics g = Graphics.FromImage(b); IntPtr hdc = g.GetHdc();
bool result = PrintWindow((IntPtr)ie.HWND, hdc, 0);
g.ReleaseHdc(); g.Flush();
|
/> b.Save( @"filename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
You will also need to add the PrintWindow function (it is windows API function):
[DllImport("user32.dll")] private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
Not that hard, but not well documented. If you don't know how to get a pointer to a running Internet Explorer class, you can do the following:
1. Add reference to ShDocVw (it is in the COM tab and it should be named "Microsoft Objects Automation library" or something like this) 2. Add the following code:
SHDocVw.ShellWindowsClass windows = new ShellWindowsClass();
foreach (SHDocVw.InternetExplorer explorer in windows) { if (explorer.LocationURL == string.Empty) { // this is our guy! ie = explorer; } } NOTE: please be sure to use your own if statement. This one will get an instance to the last found Internet explorer which has an empty string as Location.
Happy C# programming!
| I needed to do this few days ago. I struggled to find out how.
Hope this will help to other guys in the same situation (it's so simple that you may never figure it out until someone tell you :D ):
var t; function myButtonToTimeOut(id) { // We will call the onTimeOut function in a setTimeOut() // and will pass the id as a parameter: t = setTimeout(function(){onTimeOut(id);}, 2000); } function onTimeOut(id) { alert('The caller with ' + id + ' was clicked 2 seconds ago.'); clearTimeout(t); }
what do we do here? Well, we have a global variable which will hold |
the timeOutID (required to clear the timeout). We have a target function which we want to call in let's say 2 seconds. But we want to pass some parameters to it (for example the sender of the event). What we do is to create another function (myButtonToTimeOut) in the function body we call the setTimeOut, and pass as parameter a new function which contains our desired call: function(){onTimeOut(id);}
Voilla. The other things (such as the global variable t) are here only to make this thing work. If you want to test this snippet simply add it to the head of an html page, add few buttons and set their onclick as this one:
onclick="myButtonToTimeOut(this.id);"
When you click on the button, after two seconds you should receive an alert showing you which button was pressed.
Please note: As the code was intended to only show you how to call setTimeOut with parameters, it may have some bugs in it (for example I am not quite sure what will happen if you click two times quickly on the button, or on two diferrent buttons, as t is holding the timeOutId it may get overriden and as a result, some of the alerts may continue to appear).
Have a nice programming!
| Some of you may remember the CRobots game. It is a game in which you program your own robot (in C) and put it to fight against other robots. This game is available in 3D variant and I think it may be very userful to improve your programming skills.
Try it from here:
http://antru.ru/crobots3d/
I also built my own robot, I named it Kamikadze because all it does is to spin around and shout itself :D
Documentation can be found here:
http://crobots.deepthought.it/html/manual.html#8
Happy playing! |
>
| I needed to do this (actually I wanted). I was curious if you can obtain an instance of a Internet Explorer which was started in debugging session. Here is a snipet to do this:
_applicationObject.Debugger.Go(false); SHDocVw.ShellWindowsClass windows = new ShellWindowsClass(); foreach (SHDocVw.InternetExplorer explorer in windows) { if (explorer.LocationURL == string.Empty) { // this is our guy! ie = explorer; } }
Note : this snipet should be added somewhere where you have access to the _applicationObject. |
This is the variable which is declared automatically when you create new instance. I suggest you to put it into the exec method of the AddIn: public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { _applicationObject.Debugger.Go(false); SHDocVw.ShellWindowsClass windows = new ShellWindowsClass(); foreach (SHDocVw.InternetExplorer explorer in windows) { if (explorer.LocationURL == string.Empty) { // this is our guy! ie = explorer; } } }
You also need to add reference to ShDocVw (Microsoft Shell And Automation Library). Enjoy!
| I came on this yesterday, I was bored because all the projects I am working on are to some degree "standard". No dev chalenge. I wanted to create, to do something interesting. So I started to browse CodePlex and found this SDK.
Overview : Touchless SDK is a software development kit to help developers create applications which can be controlled without the standard peripheral devices (mouse, keyboard and so on). It enable you to create a applications which are controlled without the need to touch anything at all, just gestures in the air. How is this done? Well, I think there was a lot of research on this topic. We already have programs to detect motion from your video camera, why not try to extract information from those motions? This is |
exactly what Touchless does. It captures an info from your camera and if it finds something that is known to be marker - it raises events, to which you can subscribe and do something. The better news is that Touchless is Open Source project and can be obtained from CodePlex for free. You can also whach a video on Touchless:
I downloaded this SDK and in less than a minute I made a small program which let's you move your cursor with fingers (I still can't find a good enough way to left and right click ;). I hope I will have enough time this week to post this simple project so you can get deeper with Touchless as it seems promising technology to me. Limitations: Well every good thing has drawbacks. Don't get it wrong. Touchless isn't the standard in this area. It has problems (however I think after it is open source a lot of people can work on it and improve it). So what are the limitations? First of all - Touchless is very very very sensitive to light and to colors. It is because the way it works. In order to work, you need to specify a "marker". Marker is the thing the program looks for. So if you have a red tape on your finger and define it as marker, you will be able to command your computer with it. But if there is a object with the same color on the screen and this object is about the same size, it may be found as another marker. The second problem (at least for the sample project I did) is that most web cameras are not sensitive enough. They are (most of them offcourse) with 640 x 480 pixels resolution. If you are about to move your cursor and your screen resolutions is 1280 x 1024 you will be only able to move the cursor 640 pixels horizontally and 480 pixels vertically (this can be software corrected by moving the cursor 3 or 4 pixels on each pixel that comes from the camera but for sensitive operations (resizing, drawing) it will not be good enough. However I think the things above will be resolved and we will have another great way to use our computers for almost no extra charge. I do really wish those guys luck and I will definatelly watch how this project goes and even if I can I will help a bit. I really fell in love with this toy!
| Here is one more way to load string in Web Browser Control (C#). This is in case you are using the managed control.
// Fourth way to write html in WebBrowserControl with C#: wb.DocumentText = strHtml;
I can tell you (just guessing) that this will be the best one as it became available in the managed control (at least I didn't found it in the old COM versions). That is why I think it was reviewed and probably optimized. |
>
| I was thinking about this long time ago. I recently found this post:
http://www.xtremevbtalk.com/showthread.php?t=167531
So how to load a string in WebBrowser Control with C#?
First - navigate to "about:blank" (as the web browser needs a document to operate on) Then use WebBrowser.Document.Write(string text) to write the string you want to load in the WebBrowser control. Kind of tricky but ...
Another way is to write something like:
about:your html here
And the last one is to write the document on the hard disk with StreamWriter for example, and then to navigate to it.
Here is a code to show you all methods discussed:
WebBrowser wb = new WebBrowser(); // First way to |
write html in WebBrowserControl with C#: string strHtml = "you html here"; wb.Navigate("about:" + strHtml);
// Second way to write html in WebBrowserControl with C#: wb.Navigate("about:blank"); wb.Document.Write(strHtml);
// Third way to write html in WebBrowserControl with C#: const string strAddress = "test.html"; using (StreamWriter sw = new StreamWriter(strAddress))
{ sw.Write(strHtml); } wb.Navigate("file://" + Application.StartupPath + "\\" + strAddress);
|