I have one listBox named lstKosnicka and one ADD button named btnDodadi.
protected void btnDodadi_Click(object sender, EventArgs e)
{
DodadiVoKosnicka kosnicka = new DodadiVoKosnicka();
ListItem stavka = new ListItem();
List<ListItem> lista = new List<ListItem>();
stavka = kosnicka.novaStavka(lstPredlog.SelectedItem.Text);
lstKosnicka.DataSource = Session["kosnicka"] as List<ListItem>;
lstKosnicka.Items.Add(stavka);
lstKosnicka.DataBind();
Session["kosnicka"] = lstKosnicka;
lstPredlog.SelectedIndex = -1;
}
There is one more webForm with other listbox named lstKosnickaNajava, and I want to fill this list wtih the same items as lstKosnicka using Session["kosnicka"], but something is wrong. here is Najava.aspx code:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie kolace = Request.Cookies["korisnik"];
if (kolace != null)
{
lblNajavenKorisnik.Text = "Најавен коринсик " + kolace["KorisnickoIme"];
}
else
{
lblNajavenKorisnik.Text = "Нема најавени корисници";
}
if (!IsPostBack)
{
lstKosnickaNajava.DataSource = Session["kosnicka"] as List<ListItem>;
lstKosnickaNajava.DataBind();
}
}
but when I go from the first page to Najava.aspx the lstKosnickaNajava is empty.
Maybe you should try in btnDodadi_Click()
this code
List<string> lista;
if (Session["kosnicka"] == null)
{
lista = new List<string>();
}
else
{
lista=(List<string>)Session["kosnicka"];
}
if you add only text the list item can be string.
I forget to ask maybe you do this in PAge_Load method??
Related
I have a WinForms project with a main topmost form from which a non-modal dialog is displayed. I need to hide (not close) the dialog if it loses the input focus - no matter what was the reason (the user clicked the main form, switched to another app, etc). The following bare part of the project source code shows what is going on:
public partial class MainForm : Form
{
Form _dialog = new Form();
public MainForm()
{
InitializeComponent();
this.TopMost = true;
this.Text = "Main Form";
_dialog.Text = "Dialog";
_dialog.Owner = this;
_dialog.TopMost = true;
_dialog.Deactivate += Dialog_Deactivate;
_dialog.FormClosing += Dialog_FormClosing;
}
private void Dialog_Deactivate(object sender, EventArgs e)
{
_dialog.Hide();
}
private void Dialog_FormClosing(object sender, FormClosingEventArgs e)
{
_dialog.Hide();
e.Cancel = true;
}
private void ButtonShowDialog_Click(object sender, EventArgs e)
{
_dialog.Show();
}
}
The main problem I am trying to solve is the following. If the user opened the dialog and clicks the main form like I depicted on the following screenshot
, the dialog becomes hidden as expected, but the main form loses the focus and another app that was previously active becomes active in the background - the Windows Explorer on the next screenshot:
Is it a known issue in Windows or WinForms? How to cause the main form not to lose the focus in this construction?
The issue seems to be that when the Main Form is clicked, it triggers a WM_WINDOWPOSCHANGING event. Since the child dialog is open, the hwndInsertAfter handle is the child dialog. But then in the Dialog_Deactivate the child dialog is hidden, causing the Main Form to fall behind all the other windows because the hwndInsertAfter handle is no longer a visible window.
Possible solutions are 1) set the child dialog owner to null before calling Hide() or 2) use a different event, like LostFocus, i.e:
public class MainForm3 : Form {
Form _dialog = null;
public MainForm3() {
this.Text = "Main Formmmmmmmm";
Button btn = new Button { Text = "Show" };
btn.Click += ButtonShowDialog_Click;
this.Controls.Add(btn);
}
bool b = false;
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
if (_dialog != null && _dialog.Visible)
b = true;
if (b)
Debug.WriteLine(m);
int WM_WINDOWPOSCHANGING = 0x46;
if (b && m.Msg == WM_WINDOWPOSCHANGING) {
var wp = Marshal.PtrToStructure<WINDOWPOS>(m.LParam);
Debug.WriteLine("hwnd: " + wp.hwnd + " " + GetWindowText(wp.hwnd));
Debug.WriteLine("hwndInsertAfter: " + wp.hwndInsertAfter + " " + GetWindowText(wp.hwndInsertAfter));
Debug.WriteLine("flags: " + wp.flags);
}
}
private void Dialog_Deactivate(object sender, EventArgs e) {
_dialog.Owner = null; // solution 1
_dialog.Hide();
}
private void _dialog_LostFocus(object sender, EventArgs e) { // solution 2
_dialog.Hide();
}
private void Dialog_FormClosing(object sender, FormClosingEventArgs e) {
if (_dialog.Visible) {
_dialog.Hide();
e.Cancel = true;
}
}
private void ButtonShowDialog_Click(object sender, EventArgs e) {
if (_dialog == null) {
_dialog = new Form();
_dialog.Text = "Dialoggggggg";
//_dialog.Deactivate += Dialog_Deactivate;
_dialog.LostFocus += _dialog_LostFocus; // solution 2, use LostFocus instead
_dialog.FormClosing += Dialog_FormClosing;
}
_dialog.Owner = this;
_dialog.Show();
}
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS {
public IntPtr hwnd, hwndInsertAfter;
public int x, y, cx, cy;
public SWP flags;
}
[Flags]
public enum SWP : uint {
SWP_ASYNCWINDOWPOS = 0x4000,
SWP_DEFERERASE = 0x2000,
SWP_DRAWFRAME = 0x0020,
SWP_FRAMECHANGED = 0x0020,
SWP_HIDEWINDOW = 0x0080,
SWP_NOACTIVATE = 0x0010,
SWP_NOCOPYBITS = 0x0100,
SWP_NOMOVE = 0x0002,
SWP_NOOWNERZORDER = 0x0200,
SWP_NOREDRAW = 0x0008,
SWP_NOREPOSITION = 0x0200,
SWP_NOSENDCHANGING = 0x0400,
SWP_NOSIZE = 0x0001,
SWP_NOZORDER = 0x0004,
SWP_SHOWWINDOW = 0x0040
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private static String GetWindowText(IntPtr hWnd) {
StringBuilder sb = new StringBuilder(256);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
}
Try calling Activate() on the main form after the dialog is hidden, i.e:
private void Dialog_Deactivate(object sender, EventArgs e)
{
_dialog.Hide();
this.Activate();
}
After long efforts, I managed to pull data from the api;
When I compile and click on ListVeiw1 screen, I can't get ip or name,
What is the solution?
Thanks.
<ListView SelectionMode="Single" ItemSelected="ListView1_ItemSelected" x:Name="ListView1".....
private async void Button_Clicked(object sender, EventArgs e)
{
List<TodoItem> itemsNew = new List<TodoItem>();
using (var ic = new HttpClient())
{
using (var response = await ic.GetAsync("http://adress.com/api/items"))
{
var content = await response.Content.ReadAsStringAsync();
itemsNew = JsonConvert.DeserializeObject<List<TodoItem>>(content);
ListView1.ItemsSource = itemsNew;
}
}
}
private void ListView1_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
string myname = e.SelectedItem.ToString();
}
you need to cast the SelectedItem to the correct type before you can access its properties
private void ListView1_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (TodoItem)e.SelectedItem;
// now you can access any properties of item
}
I'm new to android development and right now I'm trying to parse an J SON array into object and then save each object into a array list then display the array list with a array adapter. But, right now I can only get the array adapter to display the last element.
Here is the main class
public class Venue extends Activity {
Venue_Listing venue = new Venue_Listing();
ArrayList<Venue_Listing> venueList;
ListView listview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.list);
new GetVenus().execute(URL);
venueList = new ArrayList<Venue_Listing>();
}
private class GetVenus extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
String data;
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(Venue.this);
dialog.setMessage("Loading, please wait");
dialog.show();
dialog.setCancelable(false);
}
protected Boolean doInBackground(String... url) {
try {
HttpGet httppost = new HttpGet(url[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
data = EntityUtils.toString(entity);
}
return true;
}
catch (ParseException e1) {
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
Venue_Listing_Adapter adapter = new
Venue_Listing_Adapter(getApplicationContext(),R.layout.venue_list_layout, venueList);
try {
JSONArray jarray = new JSONArray(data);
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
if (venue != null) {
venue.SET_ID(object.getString("venue_ID"));
venue.SET_NAME(object.getString("name"));
listview.setAdapter(adapter);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
}
This class holds all the J SON parse data
public class Venue_Listing {
private String ID;
private String NAME;
public Venue_Listing() {
}
public Venue_Listing(String ID, String NAME) {
super();
this.ID = ID;
this.NAME = NAME;
}
// setter & getter....
And here is what I have for my array adapter
public class Venue_Listing_Adapter extends ArrayAdapter<Venue_Listing> {
private ArrayList<Venue_Listing> objects;
public Venue_Listing_Adapter(Context context, int resource, ArrayList<Venue_Listing> objects) {
super(context, resource, objects);
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.venue_list_layout, null);
}
Venue_Listing i = objects.get(position);
if (i != null) {
TextView id = (TextView) v.findViewById(R.id.id);
TextView name = (TextView) v.findViewById(R.id.name);
if (id != null ){
id.setText(i.GET_ID());
}
if (name != null) {
name.setText(i.GET_NAME());
}
}
return v;
}
}
Any help would be appreciated
Thank you
Please print and check the values of 'venueList'.
You have not added values in 'venueList' object.
You need to create 'venue' object every time and add object 'venue' in 'venueList'. Hence you are not getting all items in list view.
The Code may go this way: -
Venue_Listing venue;
try {
JSONArray jarray = new JSONArray(data);
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
venue = new Venue_Listing();
if (object != null) {
venue.SET_ID(object.getString("venue_ID"));
venue.SET_NAME(object.getString("name"));
venueList.add(venue);
}
}
Venue_Listing_Adapter adapter = new Venue_Listing_Adapter(getApplicationContext(),R.layout.venue_list_layout, venueList);
listview.setAdapter(adapter);
}
catch (JSONException e) {
e.printStackTrace();
}
I am trying to move between 3 forms. 1 is main form and 2 other simple forms.
I have commands in the soft keys but they are not working...
below is my code...
public class checkOutComponents extends MIDlet implements ActionListener
{
private Form appForm;
private Form f1;
private Form f2;
Command GoTof1 = new Command("GoTof1");
Command GoTof2 = new Command("GoTof2");
Command GoToMainForm = new Command("GoToMainForm");
public void startApp()
{
Display.init(this);
appForm = new Form("Check These Components!! ");
appForm.setLayout(new BorderLayout());
appForm.addCommand(GoTof1);
appForm.addCommand(GoTof2);
appForm.addComponent(BorderLayout.CENTER, formContainer);
appForm.show();
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void actionPerformed(ActionEvent event)
{
Command eventCmd = event.getCommand();
Form f = Display.getInstance().getCurrent();
boolean sentido = false;
if (eventCmd == GoTof1)
{
sentido = true;
Image i1 = null;
try
{
i1 = Image.createImage("/hello/1.jpeg");
}
catch (IOException ex)
{
ex.printStackTrace();
}
Label lab1 = new Label(i1);
f1.addComponent(lab1);
f1.addCommand(GoTof2);
f1.addCommand(GoToMainForm);
f.setTransitionOutAnimator(Transition3D.createCube(300, sentido));
f1.show();
}
else if (eventCmd == GoTof2)
{
sentido = false;
Image i2 = null;
try
{
i2 = Image.createImage("/hello/2.jpeg");
}
catch (IOException ex)
{
ex.printStackTrace();
}
Label lab2 = new Label(i2);
f1.addComponent(lab2);
f1.addCommand(GoTof1);
f1.addCommand(GoToMainForm);
f.setTransitionOutAnimator(Transition3D.createCube(300, sentido));
f2.show();
}
else if(eventCmd == GoToMainForm)
{
appForm.showBack();
}
}
}
Kindly help regarding this.
Thanks in advance and regards,
Swati
Add command listener to the form appForm.
appForm.addCommandListener(this);
I need drag from window1.listbox drop in window2.panel.
A write:
public partial class Form1 : Form
{
Routers r = new Routers();
public Form1()
{
InitializeComponent();
r.Show();
panel1.DragOver += new DragEventHandler(panel1_DragOver);
panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
panel1.AllowDrop = true;
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
throw new NotImplementedException();
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
if (isDrop == false)
{
isDrop = true;
Button b = new Button();
b.Text = (string)e.Data.GetData(DataFormats.StringFormat);
b.Location = new Point(e.X, e.Y);
this.panel1.Controls.Add(b);
}
}
void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (isDrop)
{
isDrop = false;
}
}
bool isDrop = false;
void panel1_DragEnter(object sender, DragEventArgs e)
{
isDrop = false;
if (e.Data.GetDataPresent(DataFormats.StringFormat))
e.Effect = DragDropEffects.None;
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.None;
}
}
write window2
public partial class Routers : Form
{
public Routers()
{
InitializeComponent();
this.listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
this.listBox1.DragOver += new DragEventHandler(listBox1_DragOver);
}
private void listBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
string item = this.listBox1.SelectedItem.ToString();
this.listBox1.DoDragDrop(item, DragDropEffects.Move);
}
}
}
DragDrop event not work.
and does not change the cursor when dragging
You need to set e.Effect to something other than None when dragging over the destination.
See this topic for the solution:
DragDrop event not raised
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}