How to set the selection items for CComboBox to be the CString array all at once? - visual-c++

I am look for the combobox to display 4 rows where the first row shows "a", 2nd row shows "b"..."c"..."d"
cb1 = new CComboBox;
cb1->Create( WS_VSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_BORDER, CRect(20,200,200, 300), this, 30 );
CString itemSet[] = {"a","b","c","d"};
//I am to set the array all at once with out doing each itme ??
cb1.AddString(itemSet); //fails

There's no function to do so in one go. You could do as Jeeva suggest, a simple loop traversing your array:
CString itemSet[] = {"a","b","c","d"};
for (int i = 0; i < _countof(itemSet); ++i)
{
cb1.AddString(itemSet[i]);
}
However, if you are going to use it often, you could create your own CCombobox derived class and add a function that does it.
class CMyCombo : public CCombobox
{
public:
CMyCombo();
void AddStrings(const CString* strings, int num);
// ...
}
void CMyCombo::AddStrings(const CString* strings, int num)
{
for (int i = 0; i < num; ++i)
{
cb1.AddString(strings[i]);
}
}
Actually, I would probably use a container, such as std::vector or CStringArray, but you get the idea.
By the way, if you are using strings that could be localized, you should not rely on strings only. A better approach can be found here.
One last thing: there's usually no need to create controls on the fly. It's usually easier to create member variables for them.

Do something like this
CString arr[2] = {_T("A"),_T("B")};
for(int i =0 ;i <2; i++)
{
m_ctrlCombo.AddString(arr[i]);
}

Related

Understanding Graph, Weighted method

Okay, so what does the SET stand for in the second line? Why is the second string in<>, ?
public Weighted(In in, String delimiter) {
st = new ST<String, SET<String>>();
while (!in.isEmpty()) {
String line = in.readLine();
String[] names = line.split(delimiter);
for (int i = 1; i < names.length; i++) {
addEdge(names[0], names[i]);
}
}
}
With the little information you gave, I will assume that SET is an abstract data type. An abstract data type can store any values without any particular order and with no duplicates. By telling <String> after SET you are telling you want to store Strings inside your SET.
You can learn more about SETs here: https://en.wikipedia.org/wiki/Set_(abstract_data_type)

CComboBox FindString empty

The MFC CComboBox allows a person to do an AddString of an empty string. I just proved that by doing a GetCount() before and another after the AddString; Count was 0, then it became 1; and the GUI also seems to reflect it, as its list was an huge empty box, and when adding it became a one-liner.
I also proved it further by doing
int a = m_combo.GetCount();
CString sx= _T("Not empty string");
if(a == 1)
m_combo.GetLBText(0, sx);
TRACE(_T("Start<%s>End"), sx);
and the Output window displays
File.cpp(9) : atlTraceGeneral - Start<>End
so we conclude the sx variable is empty.
Then I do a FindString having a CString m_name variable which is empty:
int idx= m_combo.FindString(-1, m_name);
And it returns CB_ERR!
Is it standard behavior for empty string entries? Official documentation doesn't say anything about it!
If it is, what is the simplest way to override it? Is there some parameter or change in the resources to change the behaviour? If there is not, I am thinking about deriving or composing a class just for the case where the string is Empty!
I did it manually for the empty string and it works!
CString sItem;
int idx_aux= CB_ERR;
// need it because FindString, FindStringExact and SelectSring return CB_ERR when we provide an empty string to them!
if(m_name.IsEmpty())
{
for (int i=0; i<m_combo.GetCount(); i++)
{
m_combo.GetLBText(i, sItem);
if(sItem.IsEmpty())
{
idx_aux= i;
break;
}
}
}
else
idx_aux= m_combo.FindString(-1, m_name);

QT. Is any way to multiplicate string several times?

In python i can write
s = "dad" * 3
Result will be: s = "daddaddad"
I want to append "tabs" to my string. Something like:
QString tabs = "\t" * count;
What would be a simple, idiomatic way to do it?
You can do it quite simply with a loop:
QString mystring("somestring");
QString output;
for (int i = 0; i < 3; ++i)
output.append(mystring);
//'output' will contain the result string
Please note that the code I provide is in C++, not Python, but the concept still applies (and should be easily ported).
EDIT:
If you need to concatenate single characters, you could do it more easily like this:
int size = 5;
QString output(size, QChar('\t'));
//'output' contains 5 tab characters
Or, if you need to assign to another string (output is already created):
int size = 5;
output.fill(QChar('\t'), size);
//'output' contains 5 tab characters
#include <QString>
QString s;
for(int i = 0; i < 3; ++i)
{
s << "dad";
}

Noob, creating string method's indexof and substring

As an assignment we are supposed to create methods that copy what string methods do. We are just learning methods and I understand them, but am having trouble getting it to work.
given:
private String st = "";
public void setString(String p){
st = p;
}
public String getString(){
return st;
}
I need to create public int indexOf(char index){}, and public String substring(int start, int end){} I've succesfuly made charAt, and equals but I need some help. We are only allowed to use String methods charAt(), and length(), and + operator. No arrays or anything more advanced either. This is how I'm guessing you start these methods:
public int indexOf(char index){
for(int i = 0; i < st.length(); i++){
return index;
}
return 0;
}
public String substring(int start, int end){
for(int i = 0; i < st.length(); i++){
}
return new String(st + start);
}
thanks!
here's my two working methods:
public boolean equals(String index){
for(int a = 0; a < index.length() && a < st.length(); a++){
if(index.charAt(a) == st.charAt(a) && index.length() == st.length()){
return true;
}
else{
return false;
}
}
return false;
}
public char charAt(int index){
if(index >= 0 && index <= st.length() - 1)
return st.charAt(index);
else
return 0;
}
For your indexOf method, you're on the right track. You'll want to modify the code in the loop. Since you're looping through the whole String, and you only have two methods available, which will help you most to get the characters from the String? Look to your other methods (equals and charAt) to see how you did them, it might give a hint. Remember, you want find a single character in your String and print out the index in which you found it.
For your substring method what you need to do is get all the characters that are represented beginning at start index and go up until end index. A loop is a good start, but you will need a base String to hold your progress in (you will need an empty String). The beginning and end point of your loop need a looking at. For substring, you want to get everything starting at start and everything before end. For instance, if I do the following:
String myString = "Racecar";
String sub = myString.substring(1, 4);
System.out.println(sub);
I should get the output ace.
I would give you the answer, but I think helping guide your reasoning will give you more benefit. Enjoy your assignment!

Count the number of frequency for different characters in a string

i am currently tried to create a small program were the user enter a string in a text area, clicks on a button and the program counts the frequency of different characters in the string and shows the result on another text area.
E.g. Step 1:- User enter:- aaabbbbbbcccdd
Step 2:- User click the button
Step 3:- a 3
b 6
c 3
d 1
This is what I've done so far....
public partial class Form1 : Form
{
Dictionary<string, int> dic = new Dictionary<string, int>();
string s = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
s = textBox1.Text;
int count = 0;
for (int i = 0; i < s.Length; i++ )
{
textBox2.Text = Convert.ToString(s[i]);
if (dic.Equals(s[i]))
{
count++;
}
else
{
dic.Add(Convert.ToString(s[i]), count++);
}
}
}
}
}
Any ideas or help how can I countinue because till now the program is giving a run time error when there are same charachter!!
Thank You
var lettersAndCounts = s.GroupBy(c=>c).Select(group => new {
Letter= group.Key,
Count = group.Count()
});
Instead of dic.Equals use dic.ContainsKey. However, i would use this little linq query:
Dictionary<string, int> dict = textBox1.Text
.GroupBy(c => c)
.ToDictionary(g => g.Key.ToString(), g => g.Count());
You are attempting to compare the entire dictionary to a string, that doesn't tell you if there is a key in the dictionary that corresponds to the string. As the dictionary never is equal to the string, your code will always think that it should add a new item even if one already exists, and that is the cause of the runtime error.
Use the ContainsKey method to check if the string exists as a key in the dictionary.
Instead of using a variable count, you would want to increase the numbers in the dictionary, and initialise new items with a count of one:
string key = s[i].ToString();
textBox2.Text = key;
if (dic.ContainsKey(key)) {
dic[key]++;
} else {
dic.Add(key, 1);
}
I'm going to suggest a different and somewhat simpler approach for doing this. Assuming you are using English strings, you can create an array with capacity = 26. Then depending on the character you encounter you would increment the appropriate index in the array. For example, if the character is 'a' increment count at index 0, if the character is 'b' increment the count at index 1, etc...
Your implementation will look something like this:
int count[] = new int [26] {0};
for(int i = 0; i < s.length; i++)
{
count[Char.ToLower(s[i]) - int('a')]++;
}
When this finishes you will have the number of 'a's in count[0] and the number of 'z's in count[25].

Resources