怎么得到一个ListBox的内容?

newhua 2003-08-19 08:06:01
新手求助
我想在一个ComboBox里显示另一个ListBox里的内容
一个是CComboBox,一个是CListBox
...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyamw 2003-08-19
  • 打赏
  • 举报
回复
CListBox *pList=(CListBox*)GetDlgItem(IDC_YOURLIST);//IDC_YOURLIST是你的ListBox控件ID
CComboBox *pCombo=(CComboBox*)GetDlgItem(IDC_YOURCOMBO);//IDC_YOURCOMBO是你的ComboBox控件的ID

char sTemp[256];

for(int i=0;i<pList->GetCount();i++)
{
pList->GetText(i,sTemp);
pCombo->AddString(sTemp);
}

当然,如果你已经给这两个控件添加了成员变量的话,你也可以用楼上hanzp的方法
hanzp 2003-08-19
  • 打赏
  • 举报
回复
CString strText;

for(int i = 0; i < lsBox.GetCount(); i ++ )
{
lsBox.GetText(i, strText);
comboBox.AddString(strText);
}

可以吗?
优秀班委选举系统 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 优秀班委选举 { public partial class Form1 : Form { private ListBox listBox; public Form1() { InitializeComponent(); } public Form1(ListBox listBox) { InitializeComponent(); this.listBox = listBox; } private void button1_Click(object sender, EventArgs e) { if (listBox1.Items.Count > 0) { if (listBox1.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取左边listBox1中选中的内容 string cont = listBox1.SelectedItem.ToString(); //左边listBox1中删除选中的内容 listBox1.Items.Remove(cont); //把获得的内容加入到右边的listBox2中 listBox2.Items.Add(cont); } } } private void button2_Click(object sender, EventArgs e) { if (listBox2.Items.Count > 0) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取右边listBox2中选中的内容 string cont = listBox2.SelectedItem.ToString(); //右边listBox2中删除选中的内容 listBox2.Items.Remove(cont); //把获得的内容加入到左边的listBox1中 listBox1.Items.Add(cont); } } } //退出 private void button5_Click(object sender, EventArgs e) { Application.Exit(); } //显示结果 private void button4_Click(object sender, EventArgs e) { //传递listBox2到Form2窗口中 Form2 form2 = new Form2(listBox2); form2.Show(); } //修改名单 private void button3_Click(object sender, EventArgs e) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { string cont = listBox2.SelectedItem.ToString(); int index = listBox2.SelectedIndex; Form3 form3 = new Form3(cont, index, listBox2); form3.Show(); this.Hide(); } } //加载窗口 private void Form1_Load(object sender, EventArgs e) { if (listBox != null) { foreach (string str in listBox.Items) { listBox2.Items.Add(str); } } } } }
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, NMUDP, Buttons, Menus, ComCtrls,WinSock; type TForm1 = class(TForm) Panel1: TPanel; Edit1: TEdit; Label1: TLabel; NMUDP1: TNMUDP; BitBtn1: TBitBtn; Panel2: TPanel; Label2: TLabel; Label3: TLabel; Button1: TButton; Button2: TButton; GroupBox1: TGroupBox; ListBox1: TListBox; GroupBox2: TGroupBox; Memo1: TMemo; procedure BitBtn1Click(Sender: TObject); procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;FromIP: String; Port: Integer); procedure Edit1KeyPress(Sender: TObject; var Key: Char); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; ComputerName: array[0..127] of Char; implementation {$R *.dfm} procedure TForm1.BitBtn1Click(Sender: TObject); var MyStream: TMemoryStream; TmpStr: String; i:integer; Begin if Edit1.Text<>'' then //如果所说的内容不为空,则发送。 begin NMUDP1.ReportLevel := Status_Basic; NMUDP1.RemotePort :=8888;//端口为:8888,可以自己定义,但必须与LocalPort相一致。 if ListBox1.Items[ListBox1.ItemIndex]=ComputerName then Edit1.Text:=ComputerName+'自言自语道:'+Edit1.Text //如果和自己对话. Else Edit1.Text:=ComputerName+'对'+ListBox1.Items[listbox1.itemindex]+'说:'+Edit1.Text; TmpStr :=Edit1.text; MyStream := TMemoryStream.Create; try MyStream.Write(TmpStr[1], Length(Edit1.Text)); if ListBox1.ItemIndex=0 then begin for i:=1 to ListBox1.Items.Count-1 do //如果选择"大家",则对所有的网友发送信息 begin NMUDP1.RemoteHost :=ListBox1.Items[i];//远程主机的名称或地址. NMUDP1.SendStream(MyStream);//发送信息. End; end else begin NMUDP1.RemoteHost :=ListBox1.Items[ListBox1.itemindex]; //仅对所选中的网友. NMUDP1.SendStream(MyStream); End; finally MyStream.Free; end; Edit1.Text:=''; Edit1.SetFocus; end else Edit1.SetFocus; end; procedure TForm1.NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer); var MyStream: TMemoryStream; TmpStr: String; begin MyStream := TMemoryStream.Create; try NMUDP1.ReadStream(MyStream); SetLength(TmpStr,NumberBytes); MyStream.Read(TmpStr[1],NumberBytes); Memo1.Lines.Add(TmpStr); //显示对话的内容. finally MyStream.Free; end; end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); var MyStream: TMemoryStream; TmpStr: String; i:integer; Begin if (key=#13) and (Edit1.Text<>'') then //如果所说的内容不为空,且最后一个按键为"Enter",则发送。 begin NMUDP1.ReportLevel := Status_Basic; NMUDP1.RemotePort :=8888; if ListBox1.Items[ListBox1.ItemIndex]=ComputerName then Edit1.Text:=ComputerName+'自言自语道:'+Edit1.Text else Edit1.Text:=ComputerName+'对'+ListBox1.Items[listbox1.itemindex]+'说:'+Edit1.Text; TmpStr :=Edit1.text; MyStream := TMemoryStream.Create; Memo1.Lines.Add(TmpStr); //显示对话的内容. try MyStream.Write(TmpStr[1], Length(Edit1.Text)); if ListBox1.ItemIndex=0 then begin for i:=1 to ListBox1.Items.Count-1 do begin NMUDP1.RemoteHost :=ListBox1.Items[i]; NMUDP1.SendStream(MyStream); end; end else begin NMUDP1.RemoteHost :=ListBox1.Items[ListBox1.itemindex]; NMUDP1.SendStream(MyStream); end; finally MyStream.Free; end; Edit1.Text:=''; edit1.SetFocus; end else Edit1.SetFocus; end; procedure TForm1.Button1Click(Sender: TObject); var InputString:String; begin //增加网友,输入的可以是IP地址或计算机名称。 InputString:=InputBox('增加人员', 'IP地址或计算机名', ''); if Inputstring<>'' then ListBox1.Items.Add(Inputstring); ListBox1.ItemIndex:=0; end; procedure TForm1.Button2Click(Sender: TObject); begin //删除当前选中的网友,但"大家"不能被删除. if ListBox1.ItemIndex<>0 then ListBox1.Items.Delete(ListBox1.ItemIndex); end; procedure TForm1.FormCreate(Sender: TObject); var sz: dword; begin memo1.Text:=' '; sz := SizeOf(Computername); GetComputerName(ComputerName, sz);//得到本机的标识 ListBox1.Items.Clear; ListBox1.Items.Add('大家');//在网友清单中,增加"大家"和 ListBox1.Items.Add(ComputerName);//本机名称 ListBox1.ItemIndex:=0; end; end.

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧