|
What UI are you writing for?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
MyCroft is right - we need to know which UI you are working with, because there are different ListView controls for WinForms, WPF, websites, etc. and any solution will depend on exactly which one you are trying to use.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm looking for an example of highlighting cell the listView of winform or an example of an employee timestamp based on the listView cell color, you can see the attached image file above.
|
|
|
|
|
I have a problem with making a simple SelectedIndex to work with the SelectionChanged.
The same piece of code worked perfectly well with the button click as the event handler but it doesn't work with the selection changed...
All I want is to have a ComboBox with a default value selected,
TextBox displaying this value below, and changing every time I change the selection.
<ComboBox x:Name="BetonCombo" Margin="5" Grid.Column="0" Padding="2" HorizontalAlignment="Center" Width="150" SelectedValuePath="Content" SelectionChanged="BetonCombo_SelectionChanged">
<ComboBoxItem>C16/20 (B20)</ComboBoxItem>
<ComboBoxItem>C20/25 (B25)</ComboBoxItem>
<ComboBoxItem>C25/30 (B30)</ComboBoxItem>
<ComboBoxItem IsSelected="True">C30/37 (B37)</ComboBoxItem>
<ComboBoxItem>C35/45 (B45)</ComboBoxItem>
</ComboBox>
private void BetonCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int mBetonIndex = BetonCombo.SelectedIndex;
double[] Fck = new double[5];
Fck[0] = 20;
Fck[1] = 25;
Fck[2] = 30;
Fck[3] = 37;
Fck[4] = 45;
double mFck = Fck[mBetonIndex];
fck.Text = mFck.ToString() + " MPa";
}
I am getting this exception
System.NullReferenceException: 'Object reference not set to an instance of an object.' with the last line of code highlighted as the problem (fck.Text = mFck.ToString() + " MPa";).
I don't understand how I could get a null reference if the exact same code worked perfectly fine in the button click event handler? Do I need to set a default value of the ComboBox differently than with a IsSelected=true?
|
|
|
|
|
You get a null reference, probably because C# is case sensitive.
So fck is not the same as Fck and the chances are that when you SelectionChanged event happens, the rest of your code hasn't done anything useful with fck yet. But we can't see the code, so we can't help.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Haha, thanks Debugging is actually the next thing I was about to get into.
But actually I found out what was the problem with my code, but it's very strange.
When I removed the IsSelected="true" from the ComboBox item parameters, everything works just fine.
It looks like the SelectionChanged event handler is fired by itself at the start of the program when the IsSelected parameter is resolved...
but now I have no default value in my combo box at the start of the program tho
|
|
|
|
|
Well yes, of course it is!
And it comes in with an SelectedIndex value of -1 to tell you "there is nothing selected"
So reset the property, and try checking for a negative index!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Looks like code from TMS?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Eddy Vluggen wrote: Looks like code from TMS
Teenage Mutant Sparrows?
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
A company that I used to work which automated concrete-factories; didn't have much coding-guidelines in those days, but terms like "f***" were forbidden because it looks sh*tty during a presentation.
So, one of the coworkers started using variable names like "fck" and "yf" (standing for yuck fou).
Good memories
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
It's not for any company, just my own little project to make life easier
In general the purpose of this program is going to be calculating the stirrup spacing and longitudinal reinforcement in a concrete beam subjected to shear and torsion.
Btw, I have another problem to solve... is there any easy way to create method inside a class that would automatically add a property of this instance to a list?
For example I have a class:
class Beton
{
public string Label { get; set; }
public double Fck { get; set; }
public double Fcm { get; set; }
public double Fctm { get; set; }
public double Ecm { get; set; }
{
}
}
Now I want to make 3 instances of this class:
Beton B20 = new Beton();
B20.Label = "C16/20 (B20)";
B20.Fck = 20;
Beton B25 = new Beton();
B25.Label = "C20/25 (B25)";
B25.Fck = 25;
Beton B30 = new Beton();
B30.Label = "C25/30 (B30)";
B30.Fck = 25;
and I want that the list below would be automatically created when I instantiate a class:
List<string> comboBoxList = new List<string>();
comboBoxList.Add(B20.Label);
comboBoxList.Add(B25.Label);
comboBoxList.Add(B30.Label);
comboBoxList.Add(B37.Label);
Is this possible?
|
|
|
|
|
Yes:
public class Beton
{
private static List<Beton> all = new List<Beton>();
public Beton()
{
...
all.Add(this);
}
} Because the collection is static there is only one instance of teh collection which is shared by all instances of the Beton Class.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you for the answer.
It looks like I am still missing something, I got the error that my list doesn't exist in the current context in the main window.
Is something more supposed to be in the "..." place in your snippet?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Beton B20 = new Beton();
B20.Nazwa = "C16/20 (B20)";
B20.Fck = 20;
Beton B25 = new Beton();
B25.Nazwa = "C20/25 (B25)";
B25.Fck = 25;
Beton B30 = new Beton();
B30.Nazwa = "C25/30 (B30)";
B30.Fck = 25;
Beton B37 = new Beton();
B37.Nazwa = "C30/37 (B37)";
B37.Fck = 37;
BetonCombo.ItemsSource = comboBoxList;
public class Beton
{
public string Label { get; set; }
public double Fck { get; set; }
public double Fcm { get; set; }
public double Fctm { get; set; }
public double Ecm { get; set; }
private static List<Beton> comboBoxList = new List<Beton>();
public Beton()
{
comboBoxList.Add(this);
}
}
|
|
|
|
|
That's because it's a part of the Beton class, not the MainWindow class.
If you want to access the collection, then I'd provide a get-only property (or a GetAll method) which returns a copy of the list as part of the Beton Class:
public class Beton
{
public static List<Beton> All
{ get { return new List<Beton>(all);} }
...
} That way, the outside world can't mess with your collection!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 4-Aug-18 10:09am.
|
|
|
|
|
I tried to get this to work, but I had to give up after like 3 hours.
For now I left a manually declared list after instantiating the class.
In general I got the "cannot implicitly convert Beton to String", or "Beton to Int", or "List<string> to string", etc, whie trying different ways of writing this.
I don't really know what should be the output of using class name as the List Type (as in List < Beton>) and adding .Add(This) to it. Is it supposed to add every property of the instantiated class as an object regardless of its type to the List?
I am looking for some good toutorial about types accepted by the List object, etc. Is there something you could recommend? For now I didn't find anything thorough enough.
Anyway, thanks for posting, I will do some more search on it in free time.
modified 4-Aug-18 18:17pm.
|
|
|
|
|
C# is a "strongly typed language": it doesn't try to do (most) conversions for you, and if a List contains Beton objects, then accessing the collection returns an instance of a Beton, not the individual properties of that class.
Think of it this way: A Car Park contains instances of Cars, each located in a specific bay. If you model that, then the CarPark object is a Collection of ParkingSpaces each of which can contain a single Car:
public class Car
{
public string Registration { get; set; }
public Color Color { get; set; }
}
public class ParkingSpace
{
public Car Car { get; set; }
public int Level { get; set; }
public int Row { get; set; }
public int Column { get; set; }
}
...
List<ParkingSpace> carPark = new List<ParkingSpace>();
...
foreach (ParkingSpace ps in carPark)
{
Car c = ps.Car;
Console.WriteLine("{0} is a {1} car, on floor {2}, Row {3}, Bay {4}.",
c.Registration,
c.Color,
ps.Level,
ps.Row,
ps.Column);
} You can drive a car away and park a new one in the same space without changing the collection, or the actual spaces - you just set the new value of the ParkingSpace.Car property.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you, it works perfectly well now, it just cut like 20% of my code
The only thing left for me is to properly assing the scope for variables and collections.
I can't access my collections in the event handlers in main.
I tried to just set them to public or to make a new list with a {get} setting that returns them, but none of that works for me.
What is generally the best way to make variables and collections availible to all scopes in main window? Should I try to do it with the {get} settings on all variables and collections?
|
|
|
|
|
Look at the example I gave you for the All property, and it's static - which means you access it with just the class name from anywhere in your code that can access the Beton class:
List<Beton> allInstances = Beton.All; And it returns you a copy of the current collection.
Don't make the "raw" collection public - that lets the outside world remove instances which is a bad idea!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: Don't make the "raw" collection public - that lets the outside world remove instances which is a bad idea! List(T).AsReadOnly Method (System.Collections.Generic)[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Good idea. Possibly a little advanced for the OP though!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I already abstained from suggesting an interface to an abstract beton-class called concrete.
..and then implementing a concrete concrete
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Why is this code not working?
I am trying to set a {get} property to return a copy of any list.
Just so that I would understand how this syntax is supposed to work.
To me this code looks exactly the same as in every toutorial on the internet.
namespace WpfApp5
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string a = "aaaaaa";
string b = "bbbbbb";
List<string> ab = new List<string>
{
a,
b
};
public List<string> Ab
{ get { return new List<string>(ab); } }
}
}
}
and it returns error that "ab" doesn't exist in the current context in the {get} line...
this also doesn't work:
public List<string> Ab
{ get { return ab; } }
modified 6-Aug-18 15:26pm.
|
|
|
|
|
Because it genuinely doesn't exist.
You can't just go copying code from the internet - you have to understand this stuff!
When you write this:
return new List<string>(ab); The compiler looks for a variable called "ab" in the current scope - which in this case means the class MainWindow and doesn;t find it. Since it can't find anything to use to initialise the collection, it complains.
And yes, I know you wrote this:
List<string> ab = new List<string>
{
a,
b
}; earlier - but that's a local variable - it's scope is limited to the closest pair of curly brackets:
public void MyMethod(string a}
{
Console.WriteLine(a);
{
string b = "hello";
Console.WriteLine(a);
Console.WriteLine(b);
if (a == "World")
{
string c = "!";
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
public void MyOtherMethod()
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
ok, but after ab I also defined a public variable Ab (the capital A) that was supposed to give access to a private variable ab through the get accessor isn't it?
The whole problem here is how to access a variable out of it's scope. I dont know how to do it. I thought the get accessor is supposed to do that, but it's in the curly brackets, so does it mean it's also out of scope........?
If methods have curly brackets, classes have curly brackets, and accessors have curly brackets, then how do I ever access my variable out of scope without defining it as public ?
|
|
|
|
|