Hi
I need help completing the last part of this program. I have one textbox, one combobox and 1 treeview with checkboxes. Now here is what I want to do.
The comboBox has a list of food items, and the textbox allows the users to enter in the quantity of the items.
In the treeview the user selects the correct product category and product, for example Fruit (parent node) >>> Apple (child node).
Provided that the text selected in the combobox matches the selection, so if the user selects Apple in the combobox and Fruit (parent node) >>> Apple (child node) is checked the quantity should appear next to it.
If the quantity textbox is empty 1 should automactically appear next to the selected item, otherwise whatever was placed into the quantity textbox should appear. The user is also allowed to switch the quantity as well, provided that they select the proper product in the comboxbox and category.
Loading
theLizardPosted Mar 12, 2010, 4:26 AM
Hopefully you will get the idea, just find the point where you call your sub form.
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;
namespace tree
{
public partial class Form1 : Form
{
TreeNode parent = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tv.CheckBoxes = true;
parent = tv.Nodes.Add("Root");
parent.Nodes.Add("Apples");
parent.Nodes.Add("Pears");
parent.Nodes.Add("Plums");
//parent.Text = "Child :" + (n.Checked ? "True" : "False");
}
private void cb_SelectedIndexChanged(object sender, EventArgs e)
{
if (!findNode(cb.SelectedItem.ToString()))
{
parent.Nodes.Add(cb.Text);
if(!cb.Items.Contains(cb.Text))
cb.Items.Add(cb.Text);
}
}
private bool findNode(string what)
{
bool found = false;
for (int i = 0; i < tv.Nodes.Count; i++)
{
TreeNode n = tv.Nodes[i];
for (int j = 0; j < n.Nodes.Count; j++)
{
string s = n.Nodes[j].Text;
if (s.IndexOf(":") > 0)
s = s.Substring(0, s.IndexOf(":"));
if (s == what)
{
//you know what you want to check for checked items!!! you will need to play around here
if (n.Nodes[j].Checked && (parent = n.Nodes[j]).Checked)
{
found = true;
break;
}
}
}
}
return(found);
}
private void cb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
te.Focus();
/*
if (!findNode(cb.Text))
{
parent.Nodes.Add(cb.Text);
cb.Items.Add(cb.Text);
}
*/
}
}
private void te_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string s = cb.Text;
if(s.IndexOf(":") > 0)
s = cb.Text.Substring(0, cb.Text.IndexOf(":"));
if (!findNode(s))
{
parent.Nodes.Add(s + ":" + te.Text);
if(!cb.Items.Contains(cb.Text))
cb.Items.Add(cb.Text);
}
else
{
parent.Text = s + ":" + te.Text;
}
}
}
}
}
Sam HobbsPosted Mar 12, 2010, 3:17 AM
Sam HobbsPosted Mar 12, 2010, 2:56 AM
Is it possible for an item in the combobox to be in multiple categories in the treeview? I think that question is very fundamental to the design of your data and is therefore highly relevant to how the data is shown. If the answer is that no, an item must be in just one category, then what I would do is to automatically synchronize the two controls, the combobox and the treeview. I can understand that you don't want to bother with that, but you might be able to make everything easier by binding the data source to the form, and then the requirement could probably be solved easier.
theLizardPosted Mar 12, 2010, 2:26 AM
at the point where you determine if the node exists you can test for a value but I think you need to look for a value separator like a ":" to get the sub string and test for a value,
if there is no separator you then know that you need to add the value of txtQty otherwise test to see if the value is greater or lesser than txtQty
selectedNod.Text += ":" + txtQty.Text;
If a node does not exist after you enter a new value in the combobox, at this point you should know who the parent is because you have checked the tree nodes for a match and you can do
selectedNode = parentNode.Nodes.Add("Combobox.Text"); //parent node would also be a temp global object, but make sure that these are null before you loop through the tree nodes.
at this point selectedNode is the node that relates to the new item in the combo box.
if you already have a qty you can add it in the Add.(...) or
selectedNod.Text += ":" + txtQty.Text;
then do you otherstuff
remember I am working blind here so I hope this is not confusing.
whatever you do to the selectedNode object will affect the actual tree node.
CharlesPosted Mar 12, 2010, 2:03 AM
theLizardPosted Mar 12, 2010, 2:01 AM
CharlesPosted Mar 12, 2010, 1:54 AM
theLizardPosted Mar 12, 2010, 1:51 AM
If so, you can set a global TreeNode to the node selected and use this for your validation and other processes
eg if you click on Apple
in the NodeMouseClick event you can say TreeNode node = e.Node
at this point you have access to the node parent, the node and be able to add to the node like
TreeNode newNode = node.Add(combox. Text + ":" + qty.Text);
Do you understand what I mean?
CharlesPosted Mar 12, 2010, 1:39 AM
theLizardPosted Mar 12, 2010, 1:35 AM
Adding the qty value to the node text and extracting it is is simple enough but I am not sure what you are doing with the sub form.
CharlesPosted Mar 11, 2010, 6:33 PM
theLizardPosted Mar 11, 2010, 4:10 PM
root--|
Fruit
|
|
Apples ( 1 ) or a text box?