|
By writing some code, how do you expect? I have shown you how to get your data into your DataGridView, but I have no idea what you want to do with that data. You need to work out which cells you need to check and which cells you need to modify, and write the code to accomplish it. All the methods that can help you can be found in the DataGridView class[^]
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
<pre lang="sql">
selLoc = ListBox1.SelectedIndex;
listBox1.Items.Remove(listBox1.SelectedItems[0]);
listBox1.Items.Remove(listBox1.Items[selLoc]);
|
|
|
|
|
Found solution
selLoc = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(selLoc);
|
|
|
|
|
5 because you found the answer I was about to give you, yourself
|
|
|
|
|
The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.
|
|
|
|
|
thanks for the explanation. I didn't feel I had an answer although the fix worked.
|
|
|
|
|
BobJanova wrote: I really don't think it should do that, but that's a separate discussion
... I agree, and you are correct about the equality check. It should use reference checking IMO but then that wouldn't work with boxed value types. Hmm..
|
|
|
|
|
Yeah! I will just add it to my list of MS idiosyncratic anomalies.
|
|
|
|
|
I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google
So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds.
A quick solution is to wrap a string in another class and use that in place of string.
public class MyString
{
private string value;
public MyString(string value)
{
this.value = value;
}
public static implicit operator string(MyString myString)
{
return myString.value;
}
public static implicit operator MyString(string value)
{
return new MyString(value);
}
public override string ToString()
{
return value;
}
}
MyString one = " ";
MyString two = "2";
MyString three = " ";
listBox.Items.Add(one);
listBox.Items.Add(two);
listBox.Items.Add(three);
Selecting the third item and calling listBox.Items.Remove(listBox.SelectedItem); successfully removes the third item.
|
|
|
|
|
Oh, my mistake then.
E: Although your test doesn't actually test what you think it does. The MyString class has no equality checking, so it will always use reference equality. Try adding Equals, ==, != and Hashcode to it and see what the result is.
|
|
|
|
|
Yes, that was deliberate as the OP was wanting reference checking so two identical strings were treated as different objects. Even with those added it behaves as expected - it's just the interning of the string class that screws it!
Example below. Warning: No null checking on value
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox.Items.Add(new MyString(" "));
listBox.Items.Add(new MyString("2"));
listBox.Items.Add(new MyString(" "));
}
private void buttonRemove_Click(object sender, EventArgs e)
{
if (listBox.SelectedItem != null)
listBox.Items.Remove(listBox.SelectedItem);
}
}
}
public class MyString : IEquatable<MyString>
{
private string value;
public MyString(string value)
{
this.value = value;
}
public static implicit operator MyString(string value)
{
return new MyString(value);
}
public static implicit operator string(MyString myString)
{
return myString.value;
}
public static bool operator ==(MyString first, MyString other)
{
if (object.ReferenceEquals(first, other))
return true;
if (object.ReferenceEquals(null, first) || object.ReferenceEquals(null, other))
return false;
return first.value == other.value;
}
public static bool operator !=(MyString first, MyString other)
{
return !(first == other);
}
public override bool Equals(object obj)
{
return Equals(obj as MyString);
}
public bool Equals(MyString other)
{
if (object.ReferenceEquals(null, other))
return false;
return value.Equals(other.value);
}
public override int GetHashCode()
{
return value.GetHashCode();
}
public override string ToString()
{
return value;
}
}
|
|
|
|
|
Since my list box is populated from several functions and has over 50,000 lines it would seem the simple "RemoveAt" solves the problem. But thanks for the clarification and example.
|
|
|
|
|
Use listBox1.Items.RemoveAt(ListBox1.SelectedIndex); in place of your code then it will remove your selected item from List Box.
|
|
|
|
|
As I discovered but the listBox1.Remove is logical. Why create this special case.
|
|
|
|
|
how to call base class constructor during creation of derive class object?
|
|
|
|
|
public class BaseClass
{
public BaseClass(string something)
{
}
}
public class DerivedClass : BaseClass
{
public DerivedClass(string something, int other)
: base(something)
{
}
}
This is real basic stuff, which you would pick up really easily with a good book. I can suggest anything by Jon Skeet.
|
|
|
|
|
Hi All
I am developing a windows application in C#. One of its module need to send and receive data from USB port.
How to do this in c#? Is there any Namespase or thrd party dll available?
Regards
Willington
|
|
|
|
|
Plenty[^] if you care to look.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi.. I have googled.. but couldn't find any solution... 
|
|
|
|
|
The first article in the list of links I found for you has sample code.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Perhaps the problem is in thinking that USB is a port. It's not. It's a bus, not unlike the expansion slots inside your computer. You canot treat the USB "ports" like they were Serial or Parallel ports.
|
|
|
|
|
You can treat them as Serial port (see my example I added as answer). But indeed it is very cause-dependent if you can use them as serial port or not.
I use USB as serial port in my Arduino-Example[^] and it turned out working well . Please tell me if I am wrong...
|
|
|
|
|
Marco Bertschi wrote: You can treat them as Serial port
PROVIDED you have a driver that exposes a device as a serial port. It does not convert the USB port to a Serial port. Why? Because you can drop a USB hub in between the Arduino and the computer and your Arduino will still work as a serial device and the USB port will still work with other non-serial devices attached to the same hub.
You're using the USB port, actually bus, to communicate with a device that is exposed as a serial device. You did not treat the USB port as a serial port. USB is mearly supplying the transport mechanism carrying your serial protocol.
|
|
|
|
|
Ooops. off course, I totally forgot that I had to install a driver for the Arduino board before I was able to use it
Nevermind 
|
|
|
|
|
willington.d wrote: How to do this in c#? Is there any Namespase or thrd party dll available?
You can use the SerialPort class which is available in the namespace System.IO.Ports.
You need the following variables to initialize a serial port (you can treat the USB port as a serial port).
private SerialPort _port;
const int BAUDRATE = 9600;
const string COMPORT = "COM8";
delegate void DisplayArduinoSerialOutput(string arduinoSerialOutput);
In the constructor of your App, you need to call the following method to initialize your serial port:
private void InitCOMPort()
{
_port = new SerialPort(COMPORT) { BaudRate = BAUDRATE };
_port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);
_port.Open();
}
Afterwards, your port is ready to use:
private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
DisplayArduinoSerialOutput displayArduinoSerialOutput = new DisplayArduinoSerialOutput(arduinoSerialOutputTextBox.AppendText);
SerialPort spL = (SerialPort)sender;
this.Dispatcher.Invoke(displayArduinoSerialOutput, spL.ReadExisting());
}
In my case, I invoke the UI to add the data to a text box using the Delegate located near my other variables.
Please note: Maybe you have to change the piece of code in the _port_DataReceived-Eventhandler (because I used WPF for this snippet).
However, hope I was able to answer your question.
|
|
|
|